Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 16 | #include "SemaInit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 42 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 43 | ICC_Qualification_Adjustment, |
| 44 | ICC_Promotion, |
| 45 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 46 | ICC_Promotion, |
| 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 54 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 55 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 71 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 72 | ICR_Promotion, |
| 73 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 74 | ICR_Promotion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 82 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 83 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 92 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 97 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Qualification", |
| 99 | "Integral promotion", |
| 100 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 101 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 102 | "Integral conversion", |
| 103 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 104 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 106 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | "Pointer conversion", |
| 108 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 109 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 110 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 111 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 112 | }; |
| 113 | return Name[Kind]; |
| 114 | } |
| 115 | |
Douglas Gregor | 60d62c2 | 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 | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 125 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 126 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 146 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 149 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 150 | |
| 151 | // Note that FromType has not necessarily been transformed by the |
| 152 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 153 | // check for their presence as well as checking whether FromType is |
| 154 | // a pointer. |
| 155 | if (ToType->isBooleanType() && |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 156 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 157 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 158 | return true; |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 163 | /// isPointerConversionToVoidPointer - Determines whether this |
| 164 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 165 | /// used as part of the ranking of standard conversion sequences (C++ |
| 166 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 168 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 170 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 171 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 172 | |
| 173 | // Note that FromType has not necessarily been transformed by the |
| 174 | // array-to-pointer implicit conversion, so check for its presence |
| 175 | // and redo the conversion to get a pointer. |
| 176 | if (First == ICK_Array_To_Pointer) |
| 177 | FromType = Context.getArrayDecayedType(FromType); |
| 178 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 179 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 180 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 181 | return ToPtrType->getPointeeType()->isVoidType(); |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | /// DebugPrint - Print this standard conversion sequence to standard |
| 187 | /// error. Useful for debugging overloading issues. |
| 188 | void StandardConversionSequence::DebugPrint() const { |
| 189 | bool PrintedSomething = false; |
| 190 | if (First != ICK_Identity) { |
| 191 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 192 | PrintedSomething = true; |
| 193 | } |
| 194 | |
| 195 | if (Second != ICK_Identity) { |
| 196 | if (PrintedSomething) { |
| 197 | fprintf(stderr, " -> "); |
| 198 | } |
| 199 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 200 | |
| 201 | if (CopyConstructor) { |
| 202 | fprintf(stderr, " (by copy constructor)"); |
| 203 | } else if (DirectBinding) { |
| 204 | fprintf(stderr, " (direct reference binding)"); |
| 205 | } else if (ReferenceBinding) { |
| 206 | fprintf(stderr, " (reference binding)"); |
| 207 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 208 | PrintedSomething = true; |
| 209 | } |
| 210 | |
| 211 | if (Third != ICK_Identity) { |
| 212 | if (PrintedSomething) { |
| 213 | fprintf(stderr, " -> "); |
| 214 | } |
| 215 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 216 | PrintedSomething = true; |
| 217 | } |
| 218 | |
| 219 | if (!PrintedSomething) { |
| 220 | fprintf(stderr, "No conversions required"); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 225 | /// error. Useful for debugging overloading issues. |
| 226 | void UserDefinedConversionSequence::DebugPrint() const { |
| 227 | if (Before.First || Before.Second || Before.Third) { |
| 228 | Before.DebugPrint(); |
| 229 | fprintf(stderr, " -> "); |
| 230 | } |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 231 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | if (After.First || After.Second || After.Third) { |
| 233 | fprintf(stderr, " -> "); |
| 234 | After.DebugPrint(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 239 | /// error. Useful for debugging overloading issues. |
| 240 | void ImplicitConversionSequence::DebugPrint() const { |
| 241 | switch (ConversionKind) { |
| 242 | case StandardConversion: |
| 243 | fprintf(stderr, "Standard conversion: "); |
| 244 | Standard.DebugPrint(); |
| 245 | break; |
| 246 | case UserDefinedConversion: |
| 247 | fprintf(stderr, "User-defined conversion: "); |
| 248 | UserDefined.DebugPrint(); |
| 249 | break; |
| 250 | case EllipsisConversion: |
| 251 | fprintf(stderr, "Ellipsis conversion"); |
| 252 | break; |
| 253 | case BadConversion: |
| 254 | fprintf(stderr, "Bad conversion"); |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | fprintf(stderr, "\n"); |
| 259 | } |
| 260 | |
| 261 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 262 | // overload of the declarations in Old. This routine returns false if |
| 263 | // New and Old cannot be overloaded, e.g., if New has the same |
| 264 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 265 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 266 | // it does return false, MatchedDecl will point to the decl that New |
| 267 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 268 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 269 | // |
| 270 | // Example: Given the following input: |
| 271 | // |
| 272 | // void f(int, float); // #1 |
| 273 | // void f(int, int); // #2 |
| 274 | // int f(int, int); // #3 |
| 275 | // |
| 276 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 278 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 279 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 280 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 281 | // (since they have different signatures), so this routine returns |
| 282 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 283 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 284 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 285 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 286 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 287 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 288 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 289 | // point to the FunctionDecl for #2. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 290 | Sema::OverloadKind |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 291 | Sema::CheckOverload(FunctionDecl *New, const LookupResult &Old, |
| 292 | NamedDecl *&Match) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 293 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 294 | I != E; ++I) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 295 | NamedDecl *OldD = (*I)->getUnderlyingDecl(); |
| 296 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 297 | if (!IsOverload(New, OldT->getTemplatedDecl())) { |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 298 | Match = *I; |
| 299 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 300 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 301 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 302 | if (!IsOverload(New, OldF)) { |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 303 | Match = *I; |
| 304 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 305 | } |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 306 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 307 | // We can overload with these, which can show up when doing |
| 308 | // redeclaration checks for UsingDecls. |
| 309 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 310 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 311 | // Optimistically assume that an unresolved using decl will |
| 312 | // overload; if it doesn't, we'll have to diagnose during |
| 313 | // template instantiation. |
| 314 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 315 | // (C++ 13p1): |
| 316 | // Only function declarations can be overloaded; object and type |
| 317 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 318 | Match = *I; |
| 319 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 320 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 321 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 322 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 323 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old) { |
| 327 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 328 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 329 | |
| 330 | // C++ [temp.fct]p2: |
| 331 | // A function template can be overloaded with other function templates |
| 332 | // and with normal (non-template) functions. |
| 333 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 334 | return true; |
| 335 | |
| 336 | // Is the function New an overload of the function Old? |
| 337 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 338 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 339 | |
| 340 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 341 | // determine whether they are overloads. If we find any mismatch |
| 342 | // in the signature, they are overloads. |
| 343 | |
| 344 | // If either of these functions is a K&R-style function (no |
| 345 | // prototype), then we consider them to have matching signatures. |
| 346 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 347 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 348 | return false; |
| 349 | |
| 350 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 351 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 352 | |
| 353 | // The signature of a function includes the types of its |
| 354 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 355 | // of the ellipsis; see C++ DR 357). |
| 356 | if (OldQType != NewQType && |
| 357 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 358 | OldType->isVariadic() != NewType->isVariadic() || |
| 359 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 360 | NewType->arg_type_begin()))) |
| 361 | return true; |
| 362 | |
| 363 | // C++ [temp.over.link]p4: |
| 364 | // The signature of a function template consists of its function |
| 365 | // signature, its return type and its template parameter list. The names |
| 366 | // of the template parameters are significant only for establishing the |
| 367 | // relationship between the template parameters and the rest of the |
| 368 | // signature. |
| 369 | // |
| 370 | // We check the return type and template parameter lists for function |
| 371 | // templates first; the remaining checks follow. |
| 372 | if (NewTemplate && |
| 373 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 374 | OldTemplate->getTemplateParameters(), |
| 375 | false, TPL_TemplateMatch) || |
| 376 | OldType->getResultType() != NewType->getResultType())) |
| 377 | return true; |
| 378 | |
| 379 | // If the function is a class member, its signature includes the |
| 380 | // cv-qualifiers (if any) on the function itself. |
| 381 | // |
| 382 | // As part of this, also check whether one of the member functions |
| 383 | // is static, in which case they are not overloads (C++ |
| 384 | // 13.1p2). While not part of the definition of the signature, |
| 385 | // this check is important to determine whether these functions |
| 386 | // can be overloaded. |
| 387 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 388 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 389 | if (OldMethod && NewMethod && |
| 390 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 391 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 392 | return true; |
| 393 | |
| 394 | // The signatures match; this is not an overload. |
| 395 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 398 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 399 | /// from the given expression (Expr) to the given type (ToType). This |
| 400 | /// function returns an implicit conversion sequence that can be used |
| 401 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 402 | /// |
| 403 | /// void f(float f); |
| 404 | /// void g(int i) { f(i); } |
| 405 | /// |
| 406 | /// this routine would produce an implicit conversion sequence to |
| 407 | /// describe the initialization of f from i, which will be a standard |
| 408 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 409 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 410 | // |
| 411 | /// Note that this routine only determines how the conversion can be |
| 412 | /// performed; it does not actually perform the conversion. As such, |
| 413 | /// it will not produce any diagnostics if no conversion is available, |
| 414 | /// but will instead return an implicit conversion sequence of kind |
| 415 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 416 | /// |
| 417 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 418 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 419 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 420 | /// permitted. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 421 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 422 | /// no matter its actual lvalueness. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 423 | /// If @p UserCast, the implicit conversion is being done for a user-specified |
| 424 | /// cast. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 425 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 426 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 427 | bool SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 428 | bool AllowExplicit, bool ForceRValue, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 429 | bool InOverloadResolution, |
| 430 | bool UserCast) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 431 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 432 | OverloadCandidateSet Conversions; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 433 | OverloadingResult UserDefResult = OR_Success; |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 434 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 435 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 436 | else if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 437 | (UserDefResult = IsUserDefinedConversion(From, ToType, |
| 438 | ICS.UserDefined, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 439 | Conversions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 440 | !SuppressUserConversions, AllowExplicit, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 441 | ForceRValue, UserCast)) == OR_Success) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 442 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 443 | // C++ [over.ics.user]p4: |
| 444 | // A conversion of an expression of class type to the same class |
| 445 | // type is given Exact Match rank, and a conversion of an |
| 446 | // expression of class type to a base class of that type is |
| 447 | // given Conversion rank, in spite of the fact that a copy |
| 448 | // constructor (i.e., a user-defined conversion function) is |
| 449 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 451 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 453 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 454 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 455 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 0d6d12b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 456 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 457 | // Turn this into a "standard" conversion sequence, so that it |
| 458 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 459 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 460 | ICS.Standard.setAsIdentityConversion(); |
| 461 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 462 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 463 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 464 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 465 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 466 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 467 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 468 | |
| 469 | // C++ [over.best.ics]p4: |
| 470 | // However, when considering the argument of a user-defined |
| 471 | // conversion function that is a candidate by 13.3.1.3 when |
| 472 | // invoked for the copying of the temporary in the second step |
| 473 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 474 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 475 | // ellipsis conversion sequences are allowed. |
| 476 | if (SuppressUserConversions && |
| 477 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 478 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 479 | } else { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 480 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 481 | if (UserDefResult == OR_Ambiguous) { |
| 482 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 483 | Cand != Conversions.end(); ++Cand) |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 484 | if (Cand->Viable) |
| 485 | ICS.ConversionFunctionSet.push_back(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 488 | |
| 489 | return ICS; |
| 490 | } |
| 491 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 492 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 493 | /// conversion that strips "noreturn" off the nested function type. |
| 494 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 495 | QualType ToType, QualType &ResultTy) { |
| 496 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 497 | return false; |
| 498 | |
| 499 | // Strip the noreturn off the type we're converting from; noreturn can |
| 500 | // safely be removed. |
| 501 | FromType = Context.getNoReturnType(FromType, false); |
| 502 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 503 | return false; |
| 504 | |
| 505 | ResultTy = FromType; |
| 506 | return true; |
| 507 | } |
| 508 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 509 | /// IsStandardConversion - Determines whether there is a standard |
| 510 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 511 | /// expression From to the type ToType. Standard conversion sequences |
| 512 | /// only consider non-class types; for conversions that involve class |
| 513 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 514 | /// contain the standard conversion sequence required to perform this |
| 515 | /// conversion and this routine will return true. Otherwise, this |
| 516 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | bool |
| 518 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 519 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 521 | QualType FromType = From->getType(); |
| 522 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 523 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 524 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 525 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 526 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 527 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 528 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 530 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 532 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 533 | if (getLangOptions().CPlusPlus) |
| 534 | return false; |
| 535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 539 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 540 | // array-to-pointer conversion, or function-to-pointer conversion |
| 541 | // (C++ 4p1). |
| 542 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 544 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 545 | // converted to an rvalue. |
| 546 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 548 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 549 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 550 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 551 | |
| 552 | // If T is a non-class type, the type of the rvalue is the |
| 553 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 554 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 555 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 556 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 557 | } else if (FromType->isArrayType()) { |
| 558 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 559 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 560 | |
| 561 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 562 | // bound of T" can be converted to an rvalue of type "pointer to |
| 563 | // T" (C++ 4.2p1). |
| 564 | FromType = Context.getArrayDecayedType(FromType); |
| 565 | |
| 566 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 567 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 568 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 569 | |
| 570 | // For the purpose of ranking in overload resolution |
| 571 | // (13.3.3.1.1), this conversion is considered an |
| 572 | // array-to-pointer conversion followed by a qualification |
| 573 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 574 | SCS.Second = ICK_Identity; |
| 575 | SCS.Third = ICK_Qualification; |
| 576 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 577 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 578 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 579 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 580 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 581 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 582 | |
| 583 | // An lvalue of function type T can be converted to an rvalue of |
| 584 | // type "pointer to T." The result is a pointer to the |
| 585 | // function. (C++ 4.3p1). |
| 586 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | } else if (FunctionDecl *Fn |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 588 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 589 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 590 | SCS.First = ICK_Function_To_Pointer; |
| 591 | |
| 592 | // We were able to resolve the address of the overloaded function, |
| 593 | // so we can convert to the type of that function. |
| 594 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 595 | if (ToType->isLValueReferenceType()) |
| 596 | FromType = Context.getLValueReferenceType(FromType); |
| 597 | else if (ToType->isRValueReferenceType()) |
| 598 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 599 | else if (ToType->isMemberPointerType()) { |
| 600 | // Resolve address only succeeds if both sides are member pointers, |
| 601 | // but it doesn't have to be the same class. See DR 247. |
| 602 | // Note that this means that the type of &Derived::fn can be |
| 603 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 604 | // base class, even if it was brought into the derived class via a |
| 605 | // using declaration. The standard isn't clear on this issue at all. |
| 606 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 607 | FromType = Context.getMemberPointerType(FromType, |
| 608 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 609 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 610 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 611 | } else { |
| 612 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 613 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // The second conversion can be an integral promotion, floating |
| 617 | // point promotion, integral conversion, floating point conversion, |
| 618 | // floating-integral conversion, pointer conversion, |
| 619 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 620 | // For overloading in C, this can also be a "compatible-type" |
| 621 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 622 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 623 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 624 | // The unqualified versions of the types are the same: there's no |
| 625 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 626 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 627 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 629 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 630 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 631 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 632 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 633 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 634 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 635 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 636 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 637 | SCS.Second = ICK_Complex_Promotion; |
| 638 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 639 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 640 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 641 | // Integral conversions (C++ 4.7). |
| 642 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 643 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 644 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 645 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 646 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 647 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 648 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 649 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 650 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 651 | SCS.Second = ICK_Complex_Conversion; |
| 652 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 653 | } else if ((FromType->isFloatingType() && |
| 654 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 655 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 657 | ToType->isFloatingType())) { |
| 658 | // Floating-integral conversions (C++ 4.9). |
| 659 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 660 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 661 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 662 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 663 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 664 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 665 | SCS.Second = ICK_Complex_Real; |
| 666 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 667 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 668 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 669 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 670 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 671 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 672 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 673 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 674 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 675 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 676 | } else if (ToType->isBooleanType() && |
| 677 | (FromType->isArithmeticType() || |
| 678 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 679 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 680 | FromType->isBlockPointerType() || |
| 681 | FromType->isMemberPointerType() || |
| 682 | FromType->isNullPtrType())) { |
| 683 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 684 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 685 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 687 | Context.typesAreCompatible(ToType, FromType)) { |
| 688 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 689 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 690 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 691 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 692 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 693 | } else { |
| 694 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 695 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 698 | QualType CanonFrom; |
| 699 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 700 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 701 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 702 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 703 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 704 | CanonFrom = Context.getCanonicalType(FromType); |
| 705 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 706 | } else { |
| 707 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 708 | SCS.Third = ICK_Identity; |
| 709 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 711 | // [...] Any difference in top-level cv-qualification is |
| 712 | // subsumed by the initialization itself and does not constitute |
| 713 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 714 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 716 | if (CanonFrom.getLocalUnqualifiedType() |
| 717 | == CanonTo.getLocalUnqualifiedType() && |
| 718 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 719 | FromType = ToType; |
| 720 | CanonFrom = CanonTo; |
| 721 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | // If we have not converted the argument type to the parameter type, |
| 725 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 726 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 727 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 728 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 729 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 730 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 734 | /// expression From (whose potentially-adjusted type is FromType) to |
| 735 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 736 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 738 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 739 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 740 | if (!To) { |
| 741 | return false; |
| 742 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 743 | |
| 744 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 745 | // unsigned short int can be converted to an rvalue of type int if |
| 746 | // int can represent all the values of the source type; otherwise, |
| 747 | // the source rvalue can be converted to an rvalue of type unsigned |
| 748 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 749 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 750 | if (// We can promote any signed, promotable integer type to an int |
| 751 | (FromType->isSignedIntegerType() || |
| 752 | // We can promote any unsigned integer type whose size is |
| 753 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 755 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 756 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 759 | return To->getKind() == BuiltinType::UInt; |
| 760 | } |
| 761 | |
| 762 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 763 | // can be converted to an rvalue of the first of the following types |
| 764 | // that can represent all the values of its underlying type: int, |
| 765 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 766 | |
| 767 | // We pre-calculate the promotion type for enum types. |
| 768 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 769 | if (ToType->isIntegerType()) |
| 770 | return Context.hasSameUnqualifiedType(ToType, |
| 771 | FromEnumType->getDecl()->getPromotionType()); |
| 772 | |
| 773 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 774 | // Determine whether the type we're converting from is signed or |
| 775 | // unsigned. |
| 776 | bool FromIsSigned; |
| 777 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 778 | |
| 779 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 780 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 781 | |
| 782 | // The types we'll try to promote to, in the appropriate |
| 783 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | QualType PromoteTypes[6] = { |
| 785 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 786 | Context.LongTy, Context.UnsignedLongTy , |
| 787 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 788 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 789 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 790 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 791 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 793 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 794 | // We found the type that we can promote to. If this is the |
| 795 | // type we wanted, we have a promotion. Otherwise, no |
| 796 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 797 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 803 | // rvalue of type int if int can represent all the values of the |
| 804 | // bit-field; otherwise, it can be converted to unsigned int if |
| 805 | // unsigned int can represent all the values of the bit-field. If |
| 806 | // the bit-field is larger yet, no integral promotion applies to |
| 807 | // it. If the bit-field has an enumerated type, it is treated as any |
| 808 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 809 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 810 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 811 | using llvm::APSInt; |
| 812 | if (From) |
| 813 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 814 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 815 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 816 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 817 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 818 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 820 | // Are we promoting to an int from a bitfield that fits in an int? |
| 821 | if (BitWidth < ToSize || |
| 822 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 823 | return To->getKind() == BuiltinType::Int; |
| 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 826 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 827 | // that fits into an unsigned int? |
| 828 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 829 | return To->getKind() == BuiltinType::UInt; |
| 830 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 832 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 833 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 834 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 836 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 837 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 838 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 839 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 840 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 841 | |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 846 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 847 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 849 | /// An rvalue of type float can be converted to an rvalue of type |
| 850 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 851 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 852 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 853 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 854 | ToBuiltin->getKind() == BuiltinType::Double) |
| 855 | return true; |
| 856 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 857 | // C99 6.3.1.5p1: |
| 858 | // When a float is promoted to double or long double, or a |
| 859 | // double is promoted to long double [...]. |
| 860 | if (!getLangOptions().CPlusPlus && |
| 861 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 862 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 863 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 864 | return true; |
| 865 | } |
| 866 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 870 | /// \brief Determine if a conversion is a complex promotion. |
| 871 | /// |
| 872 | /// A complex promotion is defined as a complex -> complex conversion |
| 873 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 874 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 875 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 876 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 877 | if (!FromComplex) |
| 878 | return false; |
| 879 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 880 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 881 | if (!ToComplex) |
| 882 | return false; |
| 883 | |
| 884 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 885 | ToComplex->getElementType()) || |
| 886 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 887 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 890 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 891 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 892 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 893 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 894 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | static QualType |
| 896 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 897 | QualType ToPointee, QualType ToType, |
| 898 | ASTContext &Context) { |
| 899 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 900 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 901 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
| 903 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 904 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 905 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 906 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 907 | return ToType; |
| 908 | |
| 909 | // Build a pointer to ToPointee. It has the right qualifiers |
| 910 | // already. |
| 911 | return Context.getPointerType(ToPointee); |
| 912 | } |
| 913 | |
| 914 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 915 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 916 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 917 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 920 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 921 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 922 | /// not have the right set of qualifiers. |
| 923 | static QualType |
| 924 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 925 | QualType ToType, |
| 926 | ASTContext &Context) { |
| 927 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 928 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 929 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 930 | |
| 931 | // Exact qualifier match -> return the pointer type we're converting to. |
| 932 | if (CanonToType.getLocalQualifiers() == Quals) |
| 933 | return ToType; |
| 934 | |
| 935 | // Just build a canonical type that has the right qualifiers. |
| 936 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 937 | } |
| 938 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 940 | bool InOverloadResolution, |
| 941 | ASTContext &Context) { |
| 942 | // Handle value-dependent integral null pointer constants correctly. |
| 943 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 944 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 945 | Expr->getType()->isIntegralType()) |
| 946 | return !InOverloadResolution; |
| 947 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 948 | return Expr->isNullPointerConstant(Context, |
| 949 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 950 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 953 | /// IsPointerConversion - Determines whether the conversion of the |
| 954 | /// expression From, which has the (possibly adjusted) type FromType, |
| 955 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 956 | /// 4.10). If so, returns true and places the converted type (that |
| 957 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 958 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 959 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 960 | /// This routine also supports conversions to and from block pointers |
| 961 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 962 | /// pointers to interfaces. FIXME: Once we've determined the |
| 963 | /// appropriate overloading rules for Objective-C, we may want to |
| 964 | /// split the Objective-C checks into a different routine; however, |
| 965 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 966 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 967 | /// set if the conversion is an allowed Objective-C conversion that |
| 968 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 969 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 970 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 971 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 973 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 974 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 975 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 976 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 978 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 979 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 980 | ConvertedType = ToType; |
| 981 | return true; |
| 982 | } |
| 983 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 984 | // Blocks: Block pointers can be converted to void*. |
| 985 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 986 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 987 | ConvertedType = ToType; |
| 988 | return true; |
| 989 | } |
| 990 | // Blocks: A null pointer constant can be converted to a block |
| 991 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 993 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 994 | ConvertedType = ToType; |
| 995 | return true; |
| 996 | } |
| 997 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 998 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 999 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1001 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1002 | ConvertedType = ToType; |
| 1003 | return true; |
| 1004 | } |
| 1005 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1006 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1007 | if (!ToTypePtr) |
| 1008 | return false; |
| 1009 | |
| 1010 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1011 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1012 | ConvertedType = ToType; |
| 1013 | return true; |
| 1014 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1015 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1016 | // Beyond this point, both types need to be pointers |
| 1017 | // , including objective-c pointers. |
| 1018 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1019 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1020 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1021 | ToType, Context); |
| 1022 | return true; |
| 1023 | |
| 1024 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1025 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1026 | if (!FromTypePtr) |
| 1027 | return false; |
| 1028 | |
| 1029 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1031 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1032 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1033 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1034 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1036 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1037 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1038 | return true; |
| 1039 | } |
| 1040 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1041 | // When we're overloading in C, we allow a special kind of pointer |
| 1042 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1044 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1046 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1048 | return true; |
| 1049 | } |
| 1050 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1051 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1053 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1054 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1055 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1056 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1057 | // necessitates this conversion is ill-formed. The result of the |
| 1058 | // conversion is a pointer to the base class sub-object of the |
| 1059 | // derived class object. The null pointer value is converted to |
| 1060 | // the null pointer value of the destination type. |
| 1061 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1062 | // Note that we do not check for ambiguity or inaccessibility |
| 1063 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1064 | if (getLangOptions().CPlusPlus && |
| 1065 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1066 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1067 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1069 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1070 | ToType, Context); |
| 1071 | return true; |
| 1072 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | /// isObjCPointerConversion - Determines whether this is an |
| 1078 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1079 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1081 | QualType& ConvertedType, |
| 1082 | bool &IncompatibleObjC) { |
| 1083 | if (!getLangOptions().ObjC1) |
| 1084 | return false; |
| 1085 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1086 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1087 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1089 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1090 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1091 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1092 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1093 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1094 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1095 | ConvertedType = ToType; |
| 1096 | return true; |
| 1097 | } |
| 1098 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1100 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1102 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1103 | ConvertedType = ToType; |
| 1104 | return true; |
| 1105 | } |
| 1106 | // Objective C++: We're able to convert from a pointer to an |
| 1107 | // interface to a pointer to a different interface. |
| 1108 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1109 | ConvertedType = ToType; |
| 1110 | return true; |
| 1111 | } |
| 1112 | |
| 1113 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1114 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1115 | // interfaces, which is permitted. However, we're going to |
| 1116 | // complain about it. |
| 1117 | IncompatibleObjC = true; |
| 1118 | ConvertedType = FromType; |
| 1119 | return true; |
| 1120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1122 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1123 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1124 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1125 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1126 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1127 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1128 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1129 | return false; |
| 1130 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1131 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1132 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1133 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1134 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1135 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1136 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1137 | return false; |
| 1138 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1139 | // If we have pointers to pointers, recursively check whether this |
| 1140 | // is an Objective-C conversion. |
| 1141 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1142 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1143 | IncompatibleObjC)) { |
| 1144 | // We always complain about this conversion. |
| 1145 | IncompatibleObjC = true; |
| 1146 | ConvertedType = ToType; |
| 1147 | return true; |
| 1148 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1149 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1150 | // differences in the argument and result types are in Objective-C |
| 1151 | // pointer conversions. If so, we permit the conversion (but |
| 1152 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1154 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1155 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1156 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1157 | if (FromFunctionType && ToFunctionType) { |
| 1158 | // If the function types are exactly the same, this isn't an |
| 1159 | // Objective-C pointer conversion. |
| 1160 | if (Context.getCanonicalType(FromPointeeType) |
| 1161 | == Context.getCanonicalType(ToPointeeType)) |
| 1162 | return false; |
| 1163 | |
| 1164 | // Perform the quick checks that will tell us whether these |
| 1165 | // function types are obviously different. |
| 1166 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1167 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1168 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1169 | return false; |
| 1170 | |
| 1171 | bool HasObjCConversion = false; |
| 1172 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1173 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1174 | // Okay, the types match exactly. Nothing to do. |
| 1175 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1176 | ToFunctionType->getResultType(), |
| 1177 | ConvertedType, IncompatibleObjC)) { |
| 1178 | // Okay, we have an Objective-C pointer conversion. |
| 1179 | HasObjCConversion = true; |
| 1180 | } else { |
| 1181 | // Function types are too different. Abort. |
| 1182 | return false; |
| 1183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1185 | // Check argument types. |
| 1186 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1187 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1188 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1189 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1190 | if (Context.getCanonicalType(FromArgType) |
| 1191 | == Context.getCanonicalType(ToArgType)) { |
| 1192 | // Okay, the types match exactly. Nothing to do. |
| 1193 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1194 | ConvertedType, IncompatibleObjC)) { |
| 1195 | // Okay, we have an Objective-C pointer conversion. |
| 1196 | HasObjCConversion = true; |
| 1197 | } else { |
| 1198 | // Argument types are too different. Abort. |
| 1199 | return false; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (HasObjCConversion) { |
| 1204 | // We had an Objective-C conversion. Allow this pointer |
| 1205 | // conversion, but complain about it. |
| 1206 | ConvertedType = ToType; |
| 1207 | IncompatibleObjC = true; |
| 1208 | return true; |
| 1209 | } |
| 1210 | } |
| 1211 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1212 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1215 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1216 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1217 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1218 | /// conversions for which IsPointerConversion has already returned |
| 1219 | /// true. It returns true and produces a diagnostic if there was an |
| 1220 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1221 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1222 | CastExpr::CastKind &Kind, |
| 1223 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1224 | QualType FromType = From->getType(); |
| 1225 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1226 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1227 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1228 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1229 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1231 | if (FromPointeeType->isRecordType() && |
| 1232 | ToPointeeType->isRecordType()) { |
| 1233 | // We must have a derived-to-base conversion. Check an |
| 1234 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1235 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1236 | From->getExprLoc(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1237 | From->getSourceRange(), |
| 1238 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1239 | return true; |
| 1240 | |
| 1241 | // The conversion was successful. |
| 1242 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1246 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1248 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1249 | // Objective-C++ conversions are always okay. |
| 1250 | // FIXME: We should have a different class of conversions for the |
| 1251 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1252 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1253 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1254 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1255 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
| 1258 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1259 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1260 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1261 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1262 | /// If so, returns true and places the converted type (that might differ from |
| 1263 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1264 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1265 | QualType ToType, |
| 1266 | bool InOverloadResolution, |
| 1267 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1268 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1269 | if (!ToTypePtr) |
| 1270 | return false; |
| 1271 | |
| 1272 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1273 | if (From->isNullPointerConstant(Context, |
| 1274 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1275 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1276 | ConvertedType = ToType; |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| 1280 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1281 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1282 | if (!FromTypePtr) |
| 1283 | return false; |
| 1284 | |
| 1285 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1286 | // where D is derived from B (C++ 4.11p2). |
| 1287 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1288 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1289 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1290 | |
| 1291 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1292 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1293 | ToClass.getTypePtr()); |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
| 1297 | return false; |
| 1298 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1299 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1300 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1301 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1302 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1303 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1304 | /// true and produces a diagnostic if there was an error, or returns false |
| 1305 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1307 | CastExpr::CastKind &Kind, |
| 1308 | bool IgnoreBaseAccess) { |
| 1309 | (void)IgnoreBaseAccess; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1310 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1311 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1312 | if (!FromPtrType) { |
| 1313 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1314 | assert(From->isNullPointerConstant(Context, |
| 1315 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1316 | "Expr must be null pointer constant!"); |
| 1317 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1318 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1319 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1320 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1321 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1322 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1323 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1324 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1325 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1326 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1327 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1328 | // FIXME: What about dependent types? |
| 1329 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1330 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1331 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1332 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1333 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1334 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1335 | assert(DerivationOkay && |
| 1336 | "Should not have been called if derivation isn't OK."); |
| 1337 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1338 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1339 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1340 | getUnqualifiedType())) { |
| 1341 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1342 | Paths.clear(); |
| 1343 | Paths.setRecordingPaths(true); |
| 1344 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1345 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1346 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1347 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1348 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1349 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1350 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1351 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1352 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1354 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1355 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1356 | << FromClass << ToClass << QualType(VBase, 0) |
| 1357 | << From->getSourceRange(); |
| 1358 | return true; |
| 1359 | } |
| 1360 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1361 | // Must be a base to derived member conversion. |
| 1362 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1363 | return false; |
| 1364 | } |
| 1365 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1366 | /// IsQualificationConversion - Determines whether the conversion from |
| 1367 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1368 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | bool |
| 1370 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1371 | FromType = Context.getCanonicalType(FromType); |
| 1372 | ToType = Context.getCanonicalType(ToType); |
| 1373 | |
| 1374 | // If FromType and ToType are the same type, this is not a |
| 1375 | // qualification conversion. |
| 1376 | if (FromType == ToType) |
| 1377 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1379 | // (C++ 4.4p4): |
| 1380 | // A conversion can add cv-qualifiers at levels other than the first |
| 1381 | // in multi-level pointers, subject to the following rules: [...] |
| 1382 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1383 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1384 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1385 | // Within each iteration of the loop, we check the qualifiers to |
| 1386 | // determine if this still looks like a qualification |
| 1387 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1388 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1389 | // until there are no more pointers or pointers-to-members left to |
| 1390 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1391 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1392 | |
| 1393 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1394 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1395 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1396 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1398 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1399 | // every cv for 0 < k < j. |
| 1400 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1401 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1402 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1404 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1405 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1407 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1408 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1409 | |
| 1410 | // We are left with FromType and ToType being the pointee types |
| 1411 | // after unwrapping the original FromType and ToType the same number |
| 1412 | // of types. If we unwrapped any pointers, and if FromType and |
| 1413 | // ToType have the same unqualified type (since we checked |
| 1414 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1415 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1418 | /// Determines whether there is a user-defined conversion sequence |
| 1419 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1420 | /// ToType. If such a conversion exists, User will contain the |
| 1421 | /// user-defined conversion sequence that performs such a conversion |
| 1422 | /// and this routine will return true. Otherwise, this routine returns |
| 1423 | /// false and User is unspecified. |
| 1424 | /// |
| 1425 | /// \param AllowConversionFunctions true if the conversion should |
| 1426 | /// consider conversion functions at all. If false, only constructors |
| 1427 | /// will be considered. |
| 1428 | /// |
| 1429 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1430 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1431 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1432 | /// |
| 1433 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1434 | /// for overload resolution. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1435 | /// \param UserCast true if looking for user defined conversion for a static |
| 1436 | /// cast. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1437 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1438 | UserDefinedConversionSequence& User, |
| 1439 | OverloadCandidateSet& CandidateSet, |
| 1440 | bool AllowConversionFunctions, |
| 1441 | bool AllowExplicit, |
| 1442 | bool ForceRValue, |
| 1443 | bool UserCast) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1444 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1445 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1446 | // We're not going to find any constructors. |
| 1447 | } else if (CXXRecordDecl *ToRecordDecl |
| 1448 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1449 | // C++ [over.match.ctor]p1: |
| 1450 | // When objects of class type are direct-initialized (8.5), or |
| 1451 | // copy-initialized from an expression of the same or a |
| 1452 | // derived class type (8.5), overload resolution selects the |
| 1453 | // constructor. [...] For copy-initialization, the candidate |
| 1454 | // functions are all the converting constructors (12.3.1) of |
| 1455 | // that class. The argument list is the expression-list within |
| 1456 | // the parentheses of the initializer. |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1457 | bool SuppressUserConversions = !UserCast; |
| 1458 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1459 | IsDerivedFrom(From->getType(), ToType)) { |
| 1460 | SuppressUserConversions = false; |
| 1461 | AllowConversionFunctions = false; |
| 1462 | } |
| 1463 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1465 | = Context.DeclarationNames.getCXXConstructorName( |
| 1466 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1467 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1469 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1470 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1471 | // Find the constructor (which may be a template). |
| 1472 | CXXConstructorDecl *Constructor = 0; |
| 1473 | FunctionTemplateDecl *ConstructorTmpl |
| 1474 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1475 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1477 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1478 | else |
| 1479 | Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1480 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1481 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1482 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1483 | if (ConstructorTmpl) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1484 | AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0, |
| 1485 | &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1486 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1487 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1488 | // Allow one user-defined conversion when user specifies a |
| 1489 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1490 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1491 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1492 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1493 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1497 | if (!AllowConversionFunctions) { |
| 1498 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1500 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1501 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1502 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1504 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1506 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1507 | // Add all of the conversion functions as candidates. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1508 | const UnresolvedSet *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1509 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1510 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 1511 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1512 | NamedDecl *D = *I; |
| 1513 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1514 | if (isa<UsingShadowDecl>(D)) |
| 1515 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1516 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1517 | CXXConversionDecl *Conv; |
| 1518 | FunctionTemplateDecl *ConvTemplate; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1519 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(*I))) |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1520 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1521 | else |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1522 | Conv = dyn_cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1523 | |
| 1524 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1525 | if (ConvTemplate) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1526 | AddTemplateConversionCandidate(ConvTemplate, ActingContext, |
| 1527 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1528 | else |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1529 | AddConversionCandidate(Conv, ActingContext, From, ToType, |
| 1530 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1534 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1535 | |
| 1536 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1537 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1538 | case OR_Success: |
| 1539 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1541 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1542 | // C++ [over.ics.user]p1: |
| 1543 | // If the user-defined conversion is specified by a |
| 1544 | // constructor (12.3.1), the initial standard conversion |
| 1545 | // sequence converts the source type to the type required by |
| 1546 | // the argument of the constructor. |
| 1547 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1548 | QualType ThisType = Constructor->getThisType(Context); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1549 | if (Best->Conversions[0].ConversionKind == |
| 1550 | ImplicitConversionSequence::EllipsisConversion) |
| 1551 | User.EllipsisConversion = true; |
| 1552 | else { |
| 1553 | User.Before = Best->Conversions[0].Standard; |
| 1554 | User.EllipsisConversion = false; |
| 1555 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1556 | User.ConversionFunction = Constructor; |
| 1557 | User.After.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1559 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1560 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1561 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1562 | } else if (CXXConversionDecl *Conversion |
| 1563 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1564 | // C++ [over.ics.user]p1: |
| 1565 | // |
| 1566 | // [...] If the user-defined conversion is specified by a |
| 1567 | // conversion function (12.3.2), the initial standard |
| 1568 | // conversion sequence converts the source type to the |
| 1569 | // implicit object parameter of the conversion function. |
| 1570 | User.Before = Best->Conversions[0].Standard; |
| 1571 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1572 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
| 1574 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1575 | // The second standard conversion sequence converts the |
| 1576 | // result of the user-defined conversion to the target type |
| 1577 | // for the sequence. Since an implicit conversion sequence |
| 1578 | // is an initialization, the special rules for |
| 1579 | // initialization by user-defined conversion apply when |
| 1580 | // selecting the best user-defined conversion for a |
| 1581 | // user-defined conversion sequence (see 13.3.3 and |
| 1582 | // 13.3.3.1). |
| 1583 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1584 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1585 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1586 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1587 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1590 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1591 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1592 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1593 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1594 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1595 | |
| 1596 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1597 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1600 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1601 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1602 | |
| 1603 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1604 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1605 | ImplicitConversionSequence ICS; |
| 1606 | OverloadCandidateSet CandidateSet; |
| 1607 | OverloadingResult OvResult = |
| 1608 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1609 | CandidateSet, true, false, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1610 | if (OvResult == OR_Ambiguous) |
| 1611 | Diag(From->getSourceRange().getBegin(), |
| 1612 | diag::err_typecheck_ambiguous_condition) |
| 1613 | << From->getType() << ToType << From->getSourceRange(); |
| 1614 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1615 | Diag(From->getSourceRange().getBegin(), |
| 1616 | diag::err_typecheck_nonviable_condition) |
| 1617 | << From->getType() << ToType << From->getSourceRange(); |
| 1618 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1619 | return false; |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1620 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1621 | return true; |
| 1622 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1624 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1625 | /// conversion sequences to determine whether one is better than the |
| 1626 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1628 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1629 | const ImplicitConversionSequence& ICS2) |
| 1630 | { |
| 1631 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1632 | // conversion sequences (as defined in 13.3.3.1) |
| 1633 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1634 | // conversion sequence than a user-defined conversion sequence or |
| 1635 | // an ellipsis conversion sequence, and |
| 1636 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1637 | // conversion sequence than an ellipsis conversion sequence |
| 1638 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | // |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1640 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1641 | return ImplicitConversionSequence::Better; |
| 1642 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1643 | return ImplicitConversionSequence::Worse; |
| 1644 | |
| 1645 | // Two implicit conversion sequences of the same form are |
| 1646 | // indistinguishable conversion sequences unless one of the |
| 1647 | // following rules apply: (C++ 13.3.3.2p3): |
| 1648 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1649 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | else if (ICS1.ConversionKind == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1651 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1652 | // User-defined conversion sequence U1 is a better conversion |
| 1653 | // sequence than another user-defined conversion sequence U2 if |
| 1654 | // they contain the same user-defined conversion function or |
| 1655 | // constructor and if the second standard conversion sequence of |
| 1656 | // U1 is better than the second standard conversion sequence of |
| 1657 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1659 | ICS2.UserDefined.ConversionFunction) |
| 1660 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1661 | ICS2.UserDefined.After); |
| 1662 | } |
| 1663 | |
| 1664 | return ImplicitConversionSequence::Indistinguishable; |
| 1665 | } |
| 1666 | |
| 1667 | /// CompareStandardConversionSequences - Compare two standard |
| 1668 | /// conversion sequences to determine whether one is better than the |
| 1669 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1671 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1672 | const StandardConversionSequence& SCS2) |
| 1673 | { |
| 1674 | // Standard conversion sequence S1 is a better conversion sequence |
| 1675 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1676 | |
| 1677 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1678 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1679 | // excluding any Lvalue Transformation; the identity conversion |
| 1680 | // sequence is considered to be a subsequence of any |
| 1681 | // non-identity conversion sequence) or, if not that, |
| 1682 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1683 | // Neither is a proper subsequence of the other. Do nothing. |
| 1684 | ; |
| 1685 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1686 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1688 | SCS1.Third == ICK_Identity)) |
| 1689 | // SCS1 is a proper subsequence of SCS2. |
| 1690 | return ImplicitConversionSequence::Better; |
| 1691 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1692 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1694 | SCS2.Third == ICK_Identity)) |
| 1695 | // SCS2 is a proper subsequence of SCS1. |
| 1696 | return ImplicitConversionSequence::Worse; |
| 1697 | |
| 1698 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1699 | // defined below), or, if not that, |
| 1700 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1701 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1702 | if (Rank1 < Rank2) |
| 1703 | return ImplicitConversionSequence::Better; |
| 1704 | else if (Rank2 < Rank1) |
| 1705 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1707 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1708 | // are indistinguishable unless one of the following rules |
| 1709 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1711 | // A conversion that is not a conversion of a pointer, or |
| 1712 | // pointer to member, to bool is better than another conversion |
| 1713 | // that is such a conversion. |
| 1714 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1715 | return SCS2.isPointerConversionToBool() |
| 1716 | ? ImplicitConversionSequence::Better |
| 1717 | : ImplicitConversionSequence::Worse; |
| 1718 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1719 | // C++ [over.ics.rank]p4b2: |
| 1720 | // |
| 1721 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1722 | // conversion of B* to A* is better than conversion of B* to |
| 1723 | // void*, and conversion of A* to void* is better than conversion |
| 1724 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1726 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1728 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1729 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1730 | // Exactly one of the conversion sequences is a conversion to |
| 1731 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1732 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1733 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1734 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1735 | // Neither conversion sequence converts to a void pointer; compare |
| 1736 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1737 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1738 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1739 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1740 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1741 | // Both conversion sequences are conversions to void |
| 1742 | // pointers. Compare the source types to determine if there's an |
| 1743 | // inheritance relationship in their sources. |
| 1744 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1745 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1746 | |
| 1747 | // Adjust the types we're converting from via the array-to-pointer |
| 1748 | // conversion, if we need to. |
| 1749 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1750 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1751 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1752 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1753 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1754 | QualType FromPointee1 |
| 1755 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1756 | QualType FromPointee2 |
| 1757 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1759 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1760 | return ImplicitConversionSequence::Better; |
| 1761 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1762 | return ImplicitConversionSequence::Worse; |
| 1763 | |
| 1764 | // Objective-C++: If one interface is more specific than the |
| 1765 | // other, it is the better one. |
| 1766 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1767 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1768 | if (FromIface1 && FromIface1) { |
| 1769 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1770 | return ImplicitConversionSequence::Better; |
| 1771 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1772 | return ImplicitConversionSequence::Worse; |
| 1773 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1774 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1775 | |
| 1776 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1777 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1779 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1780 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1782 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1783 | // C++0x [over.ics.rank]p3b4: |
| 1784 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1785 | // implicit object parameter of a non-static member function declared |
| 1786 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1787 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1788 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1789 | // or if the member function in this case has a ref qualifier. |
| 1790 | // (Of course, we don't have ref qualifiers yet.) |
| 1791 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1792 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1793 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1794 | |
| 1795 | // C++ [over.ics.rank]p3b4: |
| 1796 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1797 | // which the references refer are the same type except for |
| 1798 | // top-level cv-qualifiers, and the type to which the reference |
| 1799 | // initialized by S2 refers is more cv-qualified than the type |
| 1800 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1801 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1802 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1803 | T1 = Context.getCanonicalType(T1); |
| 1804 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1805 | Qualifiers T1Quals, T2Quals; |
| 1806 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1807 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1808 | if (UnqualT1 == UnqualT2) { |
| 1809 | // If the type is an array type, promote the element qualifiers to the type |
| 1810 | // for comparison. |
| 1811 | if (isa<ArrayType>(T1) && T1Quals) |
| 1812 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1813 | if (isa<ArrayType>(T2) && T2Quals) |
| 1814 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1815 | if (T2.isMoreQualifiedThan(T1)) |
| 1816 | return ImplicitConversionSequence::Better; |
| 1817 | else if (T1.isMoreQualifiedThan(T2)) |
| 1818 | return ImplicitConversionSequence::Worse; |
| 1819 | } |
| 1820 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1821 | |
| 1822 | return ImplicitConversionSequence::Indistinguishable; |
| 1823 | } |
| 1824 | |
| 1825 | /// CompareQualificationConversions - Compares two standard conversion |
| 1826 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1828 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1829 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1831 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1832 | // -- S1 and S2 differ only in their qualification conversion and |
| 1833 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1834 | // cv-qualification signature of type T1 is a proper subset of |
| 1835 | // the cv-qualification signature of type T2, and S1 is not the |
| 1836 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1837 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1838 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1839 | return ImplicitConversionSequence::Indistinguishable; |
| 1840 | |
| 1841 | // FIXME: the example in the standard doesn't use a qualification |
| 1842 | // conversion (!) |
| 1843 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1844 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1845 | T1 = Context.getCanonicalType(T1); |
| 1846 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1847 | Qualifiers T1Quals, T2Quals; |
| 1848 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1849 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1850 | |
| 1851 | // If the types are the same, we won't learn anything by unwrapped |
| 1852 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1853 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1854 | return ImplicitConversionSequence::Indistinguishable; |
| 1855 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1856 | // If the type is an array type, promote the element qualifiers to the type |
| 1857 | // for comparison. |
| 1858 | if (isa<ArrayType>(T1) && T1Quals) |
| 1859 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1860 | if (isa<ArrayType>(T2) && T2Quals) |
| 1861 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1862 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1864 | = ImplicitConversionSequence::Indistinguishable; |
| 1865 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1866 | // Within each iteration of the loop, we check the qualifiers to |
| 1867 | // determine if this still looks like a qualification |
| 1868 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1869 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1870 | // until there are no more pointers or pointers-to-members left |
| 1871 | // to unwrap. This essentially mimics what |
| 1872 | // IsQualificationConversion does, but here we're checking for a |
| 1873 | // strict subset of qualifiers. |
| 1874 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1875 | // The qualifiers are the same, so this doesn't tell us anything |
| 1876 | // about how the sequences rank. |
| 1877 | ; |
| 1878 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1879 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1880 | if (Result == ImplicitConversionSequence::Worse) |
| 1881 | // Neither has qualifiers that are a subset of the other's |
| 1882 | // qualifiers. |
| 1883 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1885 | Result = ImplicitConversionSequence::Better; |
| 1886 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1887 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1888 | if (Result == ImplicitConversionSequence::Better) |
| 1889 | // Neither has qualifiers that are a subset of the other's |
| 1890 | // qualifiers. |
| 1891 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1893 | Result = ImplicitConversionSequence::Worse; |
| 1894 | } else { |
| 1895 | // Qualifiers are disjoint. |
| 1896 | return ImplicitConversionSequence::Indistinguishable; |
| 1897 | } |
| 1898 | |
| 1899 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1900 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1901 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1904 | // Check that the winning standard conversion sequence isn't using |
| 1905 | // the deprecated string literal array to pointer conversion. |
| 1906 | switch (Result) { |
| 1907 | case ImplicitConversionSequence::Better: |
| 1908 | if (SCS1.Deprecated) |
| 1909 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1910 | break; |
| 1911 | |
| 1912 | case ImplicitConversionSequence::Indistinguishable: |
| 1913 | break; |
| 1914 | |
| 1915 | case ImplicitConversionSequence::Worse: |
| 1916 | if (SCS2.Deprecated) |
| 1917 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1918 | break; |
| 1919 | } |
| 1920 | |
| 1921 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1924 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1925 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1926 | /// various kinds of derived-to-base conversions (C++ |
| 1927 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1928 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1929 | ImplicitConversionSequence::CompareKind |
| 1930 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1931 | const StandardConversionSequence& SCS2) { |
| 1932 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1933 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1934 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1935 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1936 | |
| 1937 | // Adjust the types we're converting from via the array-to-pointer |
| 1938 | // conversion, if we need to. |
| 1939 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1940 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1941 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1942 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1943 | |
| 1944 | // Canonicalize all of the types. |
| 1945 | FromType1 = Context.getCanonicalType(FromType1); |
| 1946 | ToType1 = Context.getCanonicalType(ToType1); |
| 1947 | FromType2 = Context.getCanonicalType(FromType2); |
| 1948 | ToType2 = Context.getCanonicalType(ToType2); |
| 1949 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1950 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1951 | // |
| 1952 | // If class B is derived directly or indirectly from class A and |
| 1953 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1954 | // |
| 1955 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1956 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1957 | |
| 1958 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1960 | SCS2.Second == ICK_Pointer_Conversion && |
| 1961 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1962 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1963 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1965 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1967 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1968 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1969 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1970 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1971 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1972 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1973 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1974 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1975 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 1976 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1978 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1979 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1980 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1981 | return ImplicitConversionSequence::Better; |
| 1982 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1983 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1984 | |
| 1985 | if (ToIface1 && ToIface2) { |
| 1986 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1987 | return ImplicitConversionSequence::Better; |
| 1988 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1989 | return ImplicitConversionSequence::Worse; |
| 1990 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1991 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1992 | |
| 1993 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1994 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1995 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1996 | return ImplicitConversionSequence::Better; |
| 1997 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1998 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2000 | if (FromIface1 && FromIface2) { |
| 2001 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2002 | return ImplicitConversionSequence::Better; |
| 2003 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2004 | return ImplicitConversionSequence::Worse; |
| 2005 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2006 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2009 | // Compare based on reference bindings. |
| 2010 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 2011 | SCS1.Second == ICK_Derived_To_Base) { |
| 2012 | // -- binding of an expression of type C to a reference of type |
| 2013 | // B& is better than binding an expression of type C to a |
| 2014 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2015 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2016 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2017 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2018 | return ImplicitConversionSequence::Better; |
| 2019 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2020 | return ImplicitConversionSequence::Worse; |
| 2021 | } |
| 2022 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2023 | // -- binding of an expression of type B to a reference of type |
| 2024 | // A& is better than binding an expression of type C to a |
| 2025 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2026 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2027 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2028 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2029 | return ImplicitConversionSequence::Better; |
| 2030 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2031 | return ImplicitConversionSequence::Worse; |
| 2032 | } |
| 2033 | } |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2034 | |
| 2035 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2036 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2037 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2038 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2039 | const MemberPointerType * FromMemPointer1 = |
| 2040 | FromType1->getAs<MemberPointerType>(); |
| 2041 | const MemberPointerType * ToMemPointer1 = |
| 2042 | ToType1->getAs<MemberPointerType>(); |
| 2043 | const MemberPointerType * FromMemPointer2 = |
| 2044 | FromType2->getAs<MemberPointerType>(); |
| 2045 | const MemberPointerType * ToMemPointer2 = |
| 2046 | ToType2->getAs<MemberPointerType>(); |
| 2047 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2048 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2049 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2050 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2051 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2052 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2053 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2054 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2055 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2056 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2057 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2058 | return ImplicitConversionSequence::Worse; |
| 2059 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2060 | return ImplicitConversionSequence::Better; |
| 2061 | } |
| 2062 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2063 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2064 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2065 | return ImplicitConversionSequence::Better; |
| 2066 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2067 | return ImplicitConversionSequence::Worse; |
| 2068 | } |
| 2069 | } |
| 2070 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2071 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 2072 | SCS1.Second == ICK_Derived_To_Base) { |
| 2073 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2074 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2075 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2076 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2077 | return ImplicitConversionSequence::Better; |
| 2078 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2079 | return ImplicitConversionSequence::Worse; |
| 2080 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2082 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2083 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2084 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2085 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2086 | return ImplicitConversionSequence::Better; |
| 2087 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2088 | return ImplicitConversionSequence::Worse; |
| 2089 | } |
| 2090 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2092 | return ImplicitConversionSequence::Indistinguishable; |
| 2093 | } |
| 2094 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2095 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2096 | /// ToType from the expression From. Return the implicit conversion |
| 2097 | /// sequence required to pass this argument, which may be a bad |
| 2098 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2099 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2100 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2101 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | ImplicitConversionSequence |
| 2103 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2104 | bool SuppressUserConversions, bool ForceRValue, |
| 2105 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2106 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2107 | ImplicitConversionSequence ICS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2109 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2110 | SuppressUserConversions, |
| 2111 | /*AllowExplicit=*/false, |
| 2112 | ForceRValue, |
| 2113 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2114 | return ICS; |
| 2115 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2117 | SuppressUserConversions, |
| 2118 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2119 | ForceRValue, |
| 2120 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2124 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2125 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2126 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2127 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2128 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2130 | AssignmentAction Action, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2131 | if (!getLangOptions().CPlusPlus) { |
| 2132 | // In C, argument passing is the same as performing an assignment. |
| 2133 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2135 | AssignConvertType ConvTy = |
| 2136 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2137 | if (ConvTy != Compatible && |
| 2138 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2139 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2141 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2142 | FromType, From, Action); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2143 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2145 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2146 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2147 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2148 | /*SuppressUserConversions=*/false, |
| 2149 | /*AllowExplicit=*/false, |
| 2150 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2152 | if (!PerformImplicitConversion(From, ToType, Action, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2153 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2154 | return false; |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2155 | if (!DiagnoseMultipleUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2156 | return Diag(From->getSourceRange().getBegin(), |
| 2157 | diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2158 | << ToType << From->getType() << Action << From->getSourceRange(); |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2159 | return true; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2162 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2163 | /// parameter of the given member function (@c Method) from the |
| 2164 | /// expression @p From. |
| 2165 | ImplicitConversionSequence |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2166 | Sema::TryObjectArgumentInitialization(QualType FromType, |
| 2167 | CXXMethodDecl *Method, |
| 2168 | CXXRecordDecl *ActingContext) { |
| 2169 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2170 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2171 | // const volatile object. |
| 2172 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2173 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2174 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2175 | |
| 2176 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2177 | // to exit early. |
| 2178 | ImplicitConversionSequence ICS; |
| 2179 | ICS.Standard.setAsIdentityConversion(); |
| 2180 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2181 | |
| 2182 | // We need to have an object of class type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2183 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2184 | FromType = PT->getPointeeType(); |
| 2185 | |
| 2186 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2187 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2188 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2189 | // where X is the class of which the function is a member |
| 2190 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2191 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2193 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2194 | // reference binding here, that allows class rvalues to bind to |
| 2195 | // non-constant references. |
| 2196 | |
| 2197 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2198 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2199 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2200 | if (ImplicitParamType.getCVRQualifiers() |
| 2201 | != FromTypeCanon.getLocalCVRQualifiers() && |
Douglas Gregor | b1c2ea5 | 2009-11-05 00:07:36 +0000 | [diff] [blame] | 2202 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2203 | return ICS; |
| 2204 | |
| 2205 | // Check that we have either the same type or a derived type. It |
| 2206 | // affects the conversion rank. |
| 2207 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2208 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2209 | ICS.Standard.Second = ICK_Identity; |
| 2210 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2211 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2212 | else |
| 2213 | return ICS; |
| 2214 | |
| 2215 | // Success. Mark this as a reference binding. |
| 2216 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2217 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2218 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2219 | ICS.Standard.ReferenceBinding = true; |
| 2220 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2221 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2222 | return ICS; |
| 2223 | } |
| 2224 | |
| 2225 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2226 | /// the implicit object parameter for the given Method with the given |
| 2227 | /// expression. |
| 2228 | bool |
| 2229 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2230 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2232 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2234 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2235 | FromRecordType = PT->getPointeeType(); |
| 2236 | DestType = Method->getThisType(Context); |
| 2237 | } else { |
| 2238 | FromRecordType = From->getType(); |
| 2239 | DestType = ImplicitParamRecordType; |
| 2240 | } |
| 2241 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2242 | // Note that we always use the true parent context when performing |
| 2243 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | ImplicitConversionSequence ICS |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2245 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2246 | Method->getParent()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2247 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2248 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2249 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2250 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2252 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2253 | CheckDerivedToBaseConversion(FromRecordType, |
| 2254 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2255 | From->getSourceRange().getBegin(), |
| 2256 | From->getSourceRange())) |
| 2257 | return true; |
| 2258 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2260 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2261 | return false; |
| 2262 | } |
| 2263 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2264 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2265 | /// expression From to bool (C++0x [conv]p3). |
| 2266 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2268 | // FIXME: Are these flags correct? |
| 2269 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2271 | /*ForceRValue=*/false, |
| 2272 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2276 | /// of the expression From to bool (C++0x [conv]p3). |
| 2277 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2278 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2279 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting)) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2280 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2281 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2282 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2283 | return Diag(From->getSourceRange().getBegin(), |
| 2284 | diag::err_typecheck_bool_condition) |
| 2285 | << From->getType() << From->getSourceRange(); |
| 2286 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2289 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2290 | /// candidate functions, using the given function call arguments. If |
| 2291 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2292 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2293 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2294 | /// hacky way to implement the overloading rules for elidable copy |
| 2295 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2296 | /// |
| 2297 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2298 | /// based on an incomplete set of function arguments. This feature is used by |
| 2299 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | void |
| 2301 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2302 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2303 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2304 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2305 | bool ForceRValue, |
| 2306 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2308 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2309 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2311 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2313 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2315 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2316 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2317 | // If we get here, it's because we're calling a member function |
| 2318 | // that is named without a member access expression (e.g., |
| 2319 | // "this->f") that was either written explicitly or created |
| 2320 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2321 | // function, e.g., X::f(). We use an empty type for the implied |
| 2322 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2323 | // is irrelevant. |
| 2324 | AddMethodCandidate(Method, Method->getParent(), |
| 2325 | QualType(), Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2326 | SuppressUserConversions, ForceRValue); |
| 2327 | return; |
| 2328 | } |
| 2329 | // We treat a constructor like a non-member function, since its object |
| 2330 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2333 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2334 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2335 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2336 | // Overload resolution is always an unevaluated context. |
| 2337 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2338 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2339 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2340 | // C++ [class.copy]p3: |
| 2341 | // A member function template is never instantiated to perform the copy |
| 2342 | // of a class object to an object of its class type. |
| 2343 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2344 | if (NumArgs == 1 && |
| 2345 | Constructor->isCopyConstructorLikeSpecialization() && |
| 2346 | Context.hasSameUnqualifiedType(ClassType, Args[0]->getType())) |
| 2347 | return; |
| 2348 | } |
| 2349 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2350 | // Add this candidate |
| 2351 | CandidateSet.push_back(OverloadCandidate()); |
| 2352 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2353 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2354 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2355 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2356 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2357 | |
| 2358 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2359 | |
| 2360 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2361 | // parameters is viable only if it has an ellipsis in its parameter |
| 2362 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2363 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2364 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2365 | Candidate.Viable = false; |
| 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2370 | // is viable only if the (m+1)st parameter has a default argument |
| 2371 | // (8.3.6). For the purposes of overload resolution, the |
| 2372 | // parameter list is truncated on the right, so that there are |
| 2373 | // exactly m parameters. |
| 2374 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2375 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2376 | // Not enough arguments. |
| 2377 | Candidate.Viable = false; |
| 2378 | return; |
| 2379 | } |
| 2380 | |
| 2381 | // Determine the implicit conversion sequences for each of the |
| 2382 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2383 | Candidate.Conversions.resize(NumArgs); |
| 2384 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2385 | if (ArgIdx < NumArgsInProto) { |
| 2386 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2387 | // exist for each argument an implicit conversion sequence |
| 2388 | // (13.3.3.1) that converts that argument to the corresponding |
| 2389 | // parameter of F. |
| 2390 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | Candidate.Conversions[ArgIdx] |
| 2392 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2393 | SuppressUserConversions, ForceRValue, |
| 2394 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2396 | == ImplicitConversionSequence::BadConversion) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2397 | // 13.3.3.1-p10 If several different sequences of conversions exist that |
| 2398 | // each convert the argument to the parameter type, the implicit conversion |
| 2399 | // sequence associated with the parameter is defined to be the unique conversion |
| 2400 | // sequence designated the ambiguous conversion sequence. For the purpose of |
| 2401 | // ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous |
| 2402 | // conversion sequence is treated as a user-defined sequence that is |
| 2403 | // indistinguishable from any other user-defined conversion sequence |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2404 | if (!Candidate.Conversions[ArgIdx].ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2405 | Candidate.Conversions[ArgIdx].ConversionKind = |
| 2406 | ImplicitConversionSequence::UserDefinedConversion; |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2407 | // Set the conversion function to one of them. As due to ambiguity, |
| 2408 | // they carry the same weight and is needed for overload resolution |
| 2409 | // later. |
| 2410 | Candidate.Conversions[ArgIdx].UserDefined.ConversionFunction = |
| 2411 | Candidate.Conversions[ArgIdx].ConversionFunctionSet[0]; |
| 2412 | } |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2413 | else { |
| 2414 | Candidate.Viable = false; |
| 2415 | break; |
| 2416 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2417 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2418 | } else { |
| 2419 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2420 | // argument for which there is no corresponding parameter is |
| 2421 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2422 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2423 | = ImplicitConversionSequence::EllipsisConversion; |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2428 | /// \brief Add all of the function declarations in the given function set to |
| 2429 | /// the overload canddiate set. |
| 2430 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2431 | Expr **Args, unsigned NumArgs, |
| 2432 | OverloadCandidateSet& CandidateSet, |
| 2433 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2435 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2436 | F != FEnd; ++F) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2437 | // FIXME: using declarations |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2438 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2439 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2440 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2441 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2442 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2443 | CandidateSet, SuppressUserConversions); |
| 2444 | else |
| 2445 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2446 | SuppressUserConversions); |
| 2447 | } else { |
| 2448 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2449 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2450 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2451 | AddMethodTemplateCandidate(FunTmpl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2452 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2453 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2454 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2455 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2456 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2457 | else |
| 2458 | AddTemplateOverloadCandidate(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2459 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2460 | Args, NumArgs, CandidateSet, |
| 2461 | SuppressUserConversions); |
| 2462 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2463 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2466 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2467 | /// method) as a method candidate to the given overload set. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2468 | void Sema::AddMethodCandidate(NamedDecl *Decl, |
| 2469 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2470 | Expr **Args, unsigned NumArgs, |
| 2471 | OverloadCandidateSet& CandidateSet, |
| 2472 | bool SuppressUserConversions, bool ForceRValue) { |
| 2473 | |
| 2474 | // FIXME: use this |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2475 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 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 | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2483 | AddMethodTemplateCandidate(TD, ActingContext, /*ExplicitArgs*/ 0, |
| 2484 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2485 | CandidateSet, |
| 2486 | SuppressUserConversions, |
| 2487 | ForceRValue); |
| 2488 | } else { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2489 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), ActingContext, |
| 2490 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2491 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2492 | } |
| 2493 | } |
| 2494 | |
Douglas Gregor | 96176b3 | 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 | e2b6833 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | void |
John McCall | 701c89e | 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 | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2507 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 | bool SuppressUserConversions, bool ForceRValue) { |
| 2509 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2510 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2511 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2512 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2513 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2514 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2515 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2516 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2517 | if (!CandidateSet.isNewCandidate(Method)) |
| 2518 | return; |
| 2519 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2520 | // Overload resolution is always an unevaluated context. |
| 2521 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2522 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2523 | // Add this candidate |
| 2524 | CandidateSet.push_back(OverloadCandidate()); |
| 2525 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2526 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2527 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2528 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2529 | |
| 2530 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2531 | |
| 2532 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2533 | // parameters is viable only if it has an ellipsis in its parameter |
| 2534 | // list (8.3.5). |
| 2535 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2536 | Candidate.Viable = false; |
| 2537 | return; |
| 2538 | } |
| 2539 | |
| 2540 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2541 | // is viable only if the (m+1)st parameter has a default argument |
| 2542 | // (8.3.6). For the purposes of overload resolution, the |
| 2543 | // parameter list is truncated on the right, so that there are |
| 2544 | // exactly m parameters. |
| 2545 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2546 | if (NumArgs < MinRequiredArgs) { |
| 2547 | // Not enough arguments. |
| 2548 | Candidate.Viable = false; |
| 2549 | return; |
| 2550 | } |
| 2551 | |
| 2552 | Candidate.Viable = true; |
| 2553 | Candidate.Conversions.resize(NumArgs + 1); |
| 2554 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2555 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2556 | // The implicit object argument is ignored. |
| 2557 | Candidate.IgnoreObjectArgument = true; |
| 2558 | else { |
| 2559 | // Determine the implicit conversion sequence for the object |
| 2560 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2561 | Candidate.Conversions[0] |
| 2562 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2564 | == ImplicitConversionSequence::BadConversion) { |
| 2565 | Candidate.Viable = false; |
| 2566 | return; |
| 2567 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | // Determine the implicit conversion sequences for each of the |
| 2571 | // arguments. |
| 2572 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2573 | if (ArgIdx < NumArgsInProto) { |
| 2574 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2575 | // exist for each argument an implicit conversion sequence |
| 2576 | // (13.3.3.1) that converts that argument to the corresponding |
| 2577 | // parameter of F. |
| 2578 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | Candidate.Conversions[ArgIdx + 1] |
| 2580 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2581 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2582 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2584 | == ImplicitConversionSequence::BadConversion) { |
| 2585 | Candidate.Viable = false; |
| 2586 | break; |
| 2587 | } |
| 2588 | } else { |
| 2589 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2590 | // argument for which there is no corresponding parameter is |
| 2591 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2593 | = ImplicitConversionSequence::EllipsisConversion; |
| 2594 | } |
| 2595 | } |
| 2596 | } |
| 2597 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2598 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2599 | /// set, using template argument deduction to produce an appropriate member |
| 2600 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2602 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2603 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2604 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2605 | QualType ObjectType, |
| 2606 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2607 | OverloadCandidateSet& CandidateSet, |
| 2608 | bool SuppressUserConversions, |
| 2609 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2610 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2611 | return; |
| 2612 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2613 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2615 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2617 | // candidate functions in the usual way.113) A given name can refer to one |
| 2618 | // or more function templates and also to a set of overloaded non-template |
| 2619 | // functions. In such a case, the candidate functions generated from each |
| 2620 | // function template are combined with the set of non-template candidate |
| 2621 | // functions. |
| 2622 | TemplateDeductionInfo Info(Context); |
| 2623 | FunctionDecl *Specialization = 0; |
| 2624 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2625 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2626 | Args, NumArgs, Specialization, Info)) { |
| 2627 | // FIXME: Record what happened with template argument deduction, so |
| 2628 | // that we can give the user a beautiful diagnostic. |
| 2629 | (void)Result; |
| 2630 | return; |
| 2631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2633 | // Add the function template specialization produced by template argument |
| 2634 | // deduction as a candidate. |
| 2635 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2636 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2637 | "Specialization is not a member function?"); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2638 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), ActingContext, |
| 2639 | ObjectType, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2640 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2641 | } |
| 2642 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2643 | /// \brief Add a C++ function template specialization as a candidate |
| 2644 | /// in the candidate set, using template argument deduction to produce |
| 2645 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2647 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2648 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2649 | Expr **Args, unsigned NumArgs, |
| 2650 | OverloadCandidateSet& CandidateSet, |
| 2651 | bool SuppressUserConversions, |
| 2652 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2653 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2654 | return; |
| 2655 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2656 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2658 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2660 | // candidate functions in the usual way.113) A given name can refer to one |
| 2661 | // or more function templates and also to a set of overloaded non-template |
| 2662 | // functions. In such a case, the candidate functions generated from each |
| 2663 | // function template are combined with the set of non-template candidate |
| 2664 | // functions. |
| 2665 | TemplateDeductionInfo Info(Context); |
| 2666 | FunctionDecl *Specialization = 0; |
| 2667 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2668 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2669 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2670 | // FIXME: Record what happened with template argument deduction, so |
| 2671 | // that we can give the user a beautiful diagnostic. |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2672 | (void) Result; |
| 2673 | |
| 2674 | CandidateSet.push_back(OverloadCandidate()); |
| 2675 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 2676 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 2677 | Candidate.Viable = false; |
| 2678 | Candidate.IsSurrogate = false; |
| 2679 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2680 | return; |
| 2681 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2683 | // Add the function template specialization produced by template argument |
| 2684 | // deduction as a candidate. |
| 2685 | assert(Specialization && "Missing function template specialization?"); |
| 2686 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2687 | SuppressUserConversions, ForceRValue); |
| 2688 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2690 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2692 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2694 | /// (which may or may not be the same type as the type that the |
| 2695 | /// conversion function produces). |
| 2696 | void |
| 2697 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2698 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2699 | Expr *From, QualType ToType, |
| 2700 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2701 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2702 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2703 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2704 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2705 | return; |
| 2706 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2707 | // Overload resolution is always an unevaluated context. |
| 2708 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2709 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2710 | // Add this candidate |
| 2711 | CandidateSet.push_back(OverloadCandidate()); |
| 2712 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2713 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2714 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2715 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2716 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2718 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2719 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2720 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2721 | // Determine the implicit conversion sequence for the implicit |
| 2722 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2723 | Candidate.Viable = true; |
| 2724 | Candidate.Conversions.resize(1); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2725 | Candidate.Conversions[0] |
| 2726 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2727 | ActingContext); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2728 | // Conversion functions to a different type in the base class is visible in |
| 2729 | // the derived class. So, a derived to base conversion should not participate |
| 2730 | // in overload resolution. |
| 2731 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2732 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2734 | == ImplicitConversionSequence::BadConversion) { |
| 2735 | Candidate.Viable = false; |
| 2736 | return; |
| 2737 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2738 | |
| 2739 | // We won't go through a user-define type conversion function to convert a |
| 2740 | // derived to base as such conversions are given Conversion Rank. They only |
| 2741 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2742 | QualType FromCanon |
| 2743 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2744 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2745 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2746 | Candidate.Viable = false; |
| 2747 | return; |
| 2748 | } |
| 2749 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2750 | |
| 2751 | // To determine what the conversion from the result of calling the |
| 2752 | // conversion function to the type we're eventually trying to |
| 2753 | // convert to (ToType), we need to synthesize a call to the |
| 2754 | // conversion function and attempt copy initialization from it. This |
| 2755 | // makes sure that we get the right semantics with respect to |
| 2756 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2757 | // call on the stack and we don't need its arguments to be |
| 2758 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2760 | From->getLocStart()); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2761 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2762 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2763 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2766 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2767 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2769 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2770 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | ImplicitConversionSequence ICS = |
| 2772 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2773 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2774 | /*ForceRValue=*/false, |
| 2775 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2777 | switch (ICS.ConversionKind) { |
| 2778 | case ImplicitConversionSequence::StandardConversion: |
| 2779 | Candidate.FinalConversion = ICS.Standard; |
| 2780 | break; |
| 2781 | |
| 2782 | case ImplicitConversionSequence::BadConversion: |
| 2783 | Candidate.Viable = false; |
| 2784 | break; |
| 2785 | |
| 2786 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2788 | "Can only end up with a standard conversion sequence or failure"); |
| 2789 | } |
| 2790 | } |
| 2791 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2792 | /// \brief Adds a conversion function template specialization |
| 2793 | /// candidate to the overload set, using template argument deduction |
| 2794 | /// to deduce the template arguments of the conversion function |
| 2795 | /// template from the type that we are converting to (C++ |
| 2796 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2798 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2799 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2800 | Expr *From, QualType ToType, |
| 2801 | OverloadCandidateSet &CandidateSet) { |
| 2802 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2803 | "Only conversion function templates permitted here"); |
| 2804 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2805 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2806 | return; |
| 2807 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2808 | TemplateDeductionInfo Info(Context); |
| 2809 | CXXConversionDecl *Specialization = 0; |
| 2810 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2812 | Specialization, Info)) { |
| 2813 | // FIXME: Record what happened with template argument deduction, so |
| 2814 | // that we can give the user a beautiful diagnostic. |
| 2815 | (void)Result; |
| 2816 | return; |
| 2817 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2819 | // Add the conversion function template specialization produced by |
| 2820 | // template argument deduction as a candidate. |
| 2821 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2822 | AddConversionCandidate(Specialization, ActingDC, From, ToType, CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2825 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2826 | /// converts the given @c Object to a function pointer via the |
| 2827 | /// conversion function @c Conversion, and then attempts to call it |
| 2828 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2829 | /// the type of function that we'll eventually be calling. |
| 2830 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2831 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2832 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2833 | QualType ObjectType, |
| 2834 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2835 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2836 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2837 | return; |
| 2838 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2839 | // Overload resolution is always an unevaluated context. |
| 2840 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2841 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2842 | CandidateSet.push_back(OverloadCandidate()); |
| 2843 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2844 | Candidate.Function = 0; |
| 2845 | Candidate.Surrogate = Conversion; |
| 2846 | Candidate.Viable = true; |
| 2847 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2848 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2849 | Candidate.Conversions.resize(NumArgs + 1); |
| 2850 | |
| 2851 | // Determine the implicit conversion sequence for the implicit |
| 2852 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | ImplicitConversionSequence ObjectInit |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2854 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2855 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2856 | Candidate.Viable = false; |
| 2857 | return; |
| 2858 | } |
| 2859 | |
| 2860 | // The first conversion is actually a user-defined conversion whose |
| 2861 | // first conversion is ObjectInit's standard conversion (which is |
| 2862 | // effectively a reference binding). Record it as such. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2864 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2865 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2866 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2867 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2869 | = Candidate.Conversions[0].UserDefined.Before; |
| 2870 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2871 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2873 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2874 | |
| 2875 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2876 | // parameters is viable only if it has an ellipsis in its parameter |
| 2877 | // list (8.3.5). |
| 2878 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2879 | Candidate.Viable = false; |
| 2880 | return; |
| 2881 | } |
| 2882 | |
| 2883 | // Function types don't have any default arguments, so just check if |
| 2884 | // we have enough arguments. |
| 2885 | if (NumArgs < NumArgsInProto) { |
| 2886 | // Not enough arguments. |
| 2887 | Candidate.Viable = false; |
| 2888 | return; |
| 2889 | } |
| 2890 | |
| 2891 | // Determine the implicit conversion sequences for each of the |
| 2892 | // arguments. |
| 2893 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2894 | if (ArgIdx < NumArgsInProto) { |
| 2895 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2896 | // exist for each argument an implicit conversion sequence |
| 2897 | // (13.3.3.1) that converts that argument to the corresponding |
| 2898 | // parameter of F. |
| 2899 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | Candidate.Conversions[ArgIdx + 1] |
| 2901 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2902 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2903 | /*ForceRValue=*/false, |
| 2904 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2906 | == ImplicitConversionSequence::BadConversion) { |
| 2907 | Candidate.Viable = false; |
| 2908 | break; |
| 2909 | } |
| 2910 | } else { |
| 2911 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2912 | // argument for which there is no corresponding parameter is |
| 2913 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2915 | = ImplicitConversionSequence::EllipsisConversion; |
| 2916 | } |
| 2917 | } |
| 2918 | } |
| 2919 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2920 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2921 | // operator overloading logic over to the scheme used by binary operators, which |
| 2922 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2923 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2924 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2925 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2926 | OverloadCandidateSet& CandidateSet, |
| 2927 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2928 | FunctionSet Functions; |
| 2929 | |
| 2930 | QualType T1 = Args[0]->getType(); |
| 2931 | QualType T2; |
| 2932 | if (NumArgs > 1) |
| 2933 | T2 = Args[1]->getType(); |
| 2934 | |
| 2935 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2936 | if (S) |
| 2937 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2938 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, NumArgs, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2939 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2940 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2941 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | /// \brief Add overload candidates for overloaded operators that are |
| 2945 | /// member functions. |
| 2946 | /// |
| 2947 | /// Add the overloaded operator candidates that are member functions |
| 2948 | /// for the operator Op that was used in an operator expression such |
| 2949 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2950 | /// CandidateSet will store the added overload candidates. (C++ |
| 2951 | /// [over.match.oper]). |
| 2952 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2953 | SourceLocation OpLoc, |
| 2954 | Expr **Args, unsigned NumArgs, |
| 2955 | OverloadCandidateSet& CandidateSet, |
| 2956 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2957 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2958 | |
| 2959 | // C++ [over.match.oper]p3: |
| 2960 | // For a unary operator @ with an operand of a type whose |
| 2961 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2962 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2963 | // a right operand of a type whose cv-unqualified version is T2, |
| 2964 | // three sets of candidate functions, designated member |
| 2965 | // candidates, non-member candidates and built-in candidates, are |
| 2966 | // constructed as follows: |
| 2967 | QualType T1 = Args[0]->getType(); |
| 2968 | QualType T2; |
| 2969 | if (NumArgs > 1) |
| 2970 | T2 = Args[1]->getType(); |
| 2971 | |
| 2972 | // -- If T1 is a class type, the set of member candidates is the |
| 2973 | // result of the qualified lookup of T1::operator@ |
| 2974 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2975 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2976 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2977 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2978 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2979 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2981 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 2982 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 2983 | Operators.suppressDiagnostics(); |
| 2984 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2986 | OperEnd = Operators.end(); |
| 2987 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2988 | ++Oper) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2989 | AddMethodCandidate(*Oper, Args[0]->getType(), |
| 2990 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2991 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2992 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2995 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2996 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2997 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2998 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2999 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3000 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3001 | /// (at the beginning of the argument list) that will be contextually |
| 3002 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3004 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3005 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3006 | bool IsAssignmentOperator, |
| 3007 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3008 | // Overload resolution is always an unevaluated context. |
| 3009 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3010 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3011 | // Add this candidate |
| 3012 | CandidateSet.push_back(OverloadCandidate()); |
| 3013 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 3014 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3015 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3016 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3017 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3018 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3019 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3020 | |
| 3021 | // Determine the implicit conversion sequences for each of the |
| 3022 | // arguments. |
| 3023 | Candidate.Viable = true; |
| 3024 | Candidate.Conversions.resize(NumArgs); |
| 3025 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3026 | // C++ [over.match.oper]p4: |
| 3027 | // For the built-in assignment operators, conversions of the |
| 3028 | // left operand are restricted as follows: |
| 3029 | // -- no temporaries are introduced to hold the left operand, and |
| 3030 | // -- no user-defined conversions are applied to the left |
| 3031 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3033 | // |
| 3034 | // We block these conversions by turning off user-defined |
| 3035 | // conversions, since that is the only way that initialization of |
| 3036 | // a reference to a non-class type can occur from something that |
| 3037 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3038 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3040 | "Contextual conversion to bool requires bool type"); |
| 3041 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3042 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | Candidate.Conversions[ArgIdx] |
| 3044 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3045 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3046 | /*ForceRValue=*/false, |
| 3047 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3048 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3050 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3051 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3052 | break; |
| 3053 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3058 | /// candidate operator functions for built-in operators (C++ |
| 3059 | /// [over.built]). The types are separated into pointer types and |
| 3060 | /// enumeration types. |
| 3061 | class BuiltinCandidateTypeSet { |
| 3062 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3063 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3064 | |
| 3065 | /// PointerTypes - The set of pointer types that will be used in the |
| 3066 | /// built-in candidates. |
| 3067 | TypeSet PointerTypes; |
| 3068 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3069 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3070 | /// used in the built-in candidates. |
| 3071 | TypeSet MemberPointerTypes; |
| 3072 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3073 | /// EnumerationTypes - The set of enumeration types that will be |
| 3074 | /// used in the built-in candidates. |
| 3075 | TypeSet EnumerationTypes; |
| 3076 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3077 | /// Sema - The semantic analysis instance where we are building the |
| 3078 | /// candidate type set. |
| 3079 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3081 | /// Context - The AST context in which we will build the type sets. |
| 3082 | ASTContext &Context; |
| 3083 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3084 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3085 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3086 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3087 | |
| 3088 | public: |
| 3089 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3090 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3091 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3093 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3094 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3095 | void AddTypesConvertedFrom(QualType Ty, |
| 3096 | SourceLocation Loc, |
| 3097 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3098 | bool AllowExplicitConversions, |
| 3099 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3100 | |
| 3101 | /// pointer_begin - First pointer type found; |
| 3102 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3103 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3104 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3105 | iterator pointer_end() { return PointerTypes.end(); } |
| 3106 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3107 | /// member_pointer_begin - First member pointer type found; |
| 3108 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3109 | |
| 3110 | /// member_pointer_end - Past the last member pointer type found; |
| 3111 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3112 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3113 | /// enumeration_begin - First enumeration type found; |
| 3114 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3115 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3116 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3117 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3118 | }; |
| 3119 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3120 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3121 | /// the set of pointer types along with any more-qualified variants of |
| 3122 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3123 | /// will add "int const *", "int const volatile *", "int const |
| 3124 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3125 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3126 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3127 | /// |
| 3128 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3129 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3130 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3131 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3132 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3133 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3134 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3135 | return false; |
| 3136 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3137 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3138 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3139 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3140 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3141 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3142 | // (the qualifier would sink to the element type), and for another, the |
| 3143 | // only overload situation where it matters is subscript or pointer +- int, |
| 3144 | // and those shouldn't have qualifier variants anyway. |
| 3145 | if (PointeeTy->isArrayType()) |
| 3146 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3147 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3148 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3149 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3150 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3151 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3152 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3153 | // Iterate through all strict supersets of BaseCVR. |
| 3154 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3155 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3156 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3157 | // in the types. |
| 3158 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3159 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3160 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3161 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | return true; |
| 3165 | } |
| 3166 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3167 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3168 | /// to the set of pointer types along with any more-qualified variants of |
| 3169 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3170 | /// will add "int const *", "int const volatile *", "int const |
| 3171 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3172 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3173 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3174 | /// |
| 3175 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3176 | bool |
| 3177 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3178 | QualType Ty) { |
| 3179 | // Insert this type. |
| 3180 | if (!MemberPointerTypes.insert(Ty)) |
| 3181 | return false; |
| 3182 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3183 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3184 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3185 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3186 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3187 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3188 | // (the qualifier would sink to the element type), and for another, the |
| 3189 | // only overload situation where it matters is subscript or pointer +- int, |
| 3190 | // and those shouldn't have qualifier variants anyway. |
| 3191 | if (PointeeTy->isArrayType()) |
| 3192 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3193 | const Type *ClassTy = PointerTy->getClass(); |
| 3194 | |
| 3195 | // Iterate through all strict supersets of the pointee type's CVR |
| 3196 | // qualifiers. |
| 3197 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3198 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3199 | if ((CVR | BaseCVR) != CVR) continue; |
| 3200 | |
| 3201 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3202 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | return true; |
| 3206 | } |
| 3207 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3208 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3209 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3210 | /// primarily interested in pointer types and enumeration types. We also |
| 3211 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3212 | /// AllowUserConversions is true if we should look at the conversion |
| 3213 | /// functions of a class type, and AllowExplicitConversions if we |
| 3214 | /// should also include the explicit conversion functions of a class |
| 3215 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3217 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3218 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3219 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3220 | bool AllowExplicitConversions, |
| 3221 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3222 | // Only deal with canonical types. |
| 3223 | Ty = Context.getCanonicalType(Ty); |
| 3224 | |
| 3225 | // Look through reference types; they aren't part of the type of an |
| 3226 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3227 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3228 | Ty = RefTy->getPointeeType(); |
| 3229 | |
| 3230 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3231 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3232 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3233 | // If we're dealing with an array type, decay to the pointer. |
| 3234 | if (Ty->isArrayType()) |
| 3235 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3236 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3237 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3238 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3239 | |
| 3240 | // Insert our type, and its more-qualified variants, into the set |
| 3241 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3242 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3243 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3244 | } else if (Ty->isMemberPointerType()) { |
| 3245 | // Member pointers are far easier, since the pointee can't be converted. |
| 3246 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3247 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3248 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3249 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3250 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3251 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3252 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3253 | // No conversion functions in incomplete types. |
| 3254 | return; |
| 3255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3257 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3258 | const UnresolvedSet *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3259 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3260 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 3261 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3262 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3264 | // about which builtin types we can convert to. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3265 | if (isa<FunctionTemplateDecl>(*I)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3266 | continue; |
| 3267 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3268 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3269 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3270 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3271 | VisibleQuals); |
| 3272 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | } |
| 3277 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3278 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3279 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3280 | /// given type to the candidate set. |
| 3281 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3282 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3284 | unsigned NumArgs, |
| 3285 | OverloadCandidateSet &CandidateSet) { |
| 3286 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3288 | // T& operator=(T&, T) |
| 3289 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3290 | ParamTypes[1] = T; |
| 3291 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3292 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3294 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3295 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3296 | ParamTypes[0] |
| 3297 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3298 | ParamTypes[1] = T; |
| 3299 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3301 | } |
| 3302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3304 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3305 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3306 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3307 | Qualifiers VRQuals; |
| 3308 | const RecordType *TyRec; |
| 3309 | if (const MemberPointerType *RHSMPType = |
| 3310 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3311 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3312 | else |
| 3313 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3314 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3315 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3316 | VRQuals.addVolatile(); |
| 3317 | VRQuals.addRestrict(); |
| 3318 | return VRQuals; |
| 3319 | } |
| 3320 | |
| 3321 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3322 | const UnresolvedSet *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3323 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3324 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3325 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 3326 | E = Conversions->end(); I != E; ++I) { |
| 3327 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*I)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3328 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3329 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3330 | CanTy = ResTypeRef->getPointeeType(); |
| 3331 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3332 | // as see them. |
| 3333 | bool done = false; |
| 3334 | while (!done) { |
| 3335 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3336 | CanTy = ResTypePtr->getPointeeType(); |
| 3337 | else if (const MemberPointerType *ResTypeMPtr = |
| 3338 | CanTy->getAs<MemberPointerType>()) |
| 3339 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3340 | else |
| 3341 | done = true; |
| 3342 | if (CanTy.isVolatileQualified()) |
| 3343 | VRQuals.addVolatile(); |
| 3344 | if (CanTy.isRestrictQualified()) |
| 3345 | VRQuals.addRestrict(); |
| 3346 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3347 | return VRQuals; |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | return VRQuals; |
| 3352 | } |
| 3353 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3354 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3355 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3356 | /// on the operator @p Op and the arguments given. For example, if the |
| 3357 | /// operator is a binary '+', this routine might add "int |
| 3358 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3359 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3360 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3361 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3362 | Expr **Args, unsigned NumArgs, |
| 3363 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3364 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3365 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3366 | // that the first few of these types are the promoted integral |
| 3367 | // types; these types need to be first. |
| 3368 | // FIXME: What about complex? |
| 3369 | const unsigned FirstIntegralType = 0; |
| 3370 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3372 | LastPromotedIntegralType = 13; |
| 3373 | const unsigned FirstPromotedArithmeticType = 7, |
| 3374 | LastPromotedArithmeticType = 16; |
| 3375 | const unsigned NumArithmeticTypes = 16; |
| 3376 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3378 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3379 | Context.SignedCharTy, Context.ShortTy, |
| 3380 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3381 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3382 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3383 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3384 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3385 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3386 | "Invalid first promoted integral type"); |
| 3387 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3388 | == Context.UnsignedLongLongTy && |
| 3389 | "Invalid last promoted integral type"); |
| 3390 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3391 | "Invalid first promoted arithmetic type"); |
| 3392 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3393 | == Context.LongDoubleTy && |
| 3394 | "Invalid last promoted arithmetic type"); |
| 3395 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3396 | // Find all of the types that the arguments can convert to, but only |
| 3397 | // if the operator we're looking at has built-in operator candidates |
| 3398 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3399 | Qualifiers VisibleTypeConversionsQuals; |
| 3400 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3401 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3402 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3403 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3404 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3405 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3406 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3407 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3408 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3409 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3410 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3411 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3412 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3413 | OpLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3414 | true, |
| 3415 | (Op == OO_Exclaim || |
| 3416 | Op == OO_AmpAmp || |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3417 | Op == OO_PipePipe), |
| 3418 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | bool isComparison = false; |
| 3422 | switch (Op) { |
| 3423 | case OO_None: |
| 3424 | case NUM_OVERLOADED_OPERATORS: |
| 3425 | assert(false && "Expected an overloaded operator"); |
| 3426 | break; |
| 3427 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3428 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3430 | goto UnaryStar; |
| 3431 | else |
| 3432 | goto BinaryStar; |
| 3433 | break; |
| 3434 | |
| 3435 | case OO_Plus: // '+' is either unary or binary |
| 3436 | if (NumArgs == 1) |
| 3437 | goto UnaryPlus; |
| 3438 | else |
| 3439 | goto BinaryPlus; |
| 3440 | break; |
| 3441 | |
| 3442 | case OO_Minus: // '-' is either unary or binary |
| 3443 | if (NumArgs == 1) |
| 3444 | goto UnaryMinus; |
| 3445 | else |
| 3446 | goto BinaryMinus; |
| 3447 | break; |
| 3448 | |
| 3449 | case OO_Amp: // '&' is either unary or binary |
| 3450 | if (NumArgs == 1) |
| 3451 | goto UnaryAmp; |
| 3452 | else |
| 3453 | goto BinaryAmp; |
| 3454 | |
| 3455 | case OO_PlusPlus: |
| 3456 | case OO_MinusMinus: |
| 3457 | // C++ [over.built]p3: |
| 3458 | // |
| 3459 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3460 | // is either volatile or empty, there exist candidate operator |
| 3461 | // functions of the form |
| 3462 | // |
| 3463 | // VQ T& operator++(VQ T&); |
| 3464 | // T operator++(VQ T&, int); |
| 3465 | // |
| 3466 | // C++ [over.built]p4: |
| 3467 | // |
| 3468 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3469 | // than bool, and VQ is either volatile or empty, there exist |
| 3470 | // candidate operator functions of the form |
| 3471 | // |
| 3472 | // VQ T& operator--(VQ T&); |
| 3473 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3475 | Arith < NumArithmeticTypes; ++Arith) { |
| 3476 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3478 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3479 | |
| 3480 | // Non-volatile version. |
| 3481 | if (NumArgs == 1) |
| 3482 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3483 | else |
| 3484 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3485 | // heuristic to reduce number of builtin candidates in the set. |
| 3486 | // Add volatile version only if there are conversions to a volatile type. |
| 3487 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3488 | // Volatile version |
| 3489 | ParamTypes[0] |
| 3490 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3491 | if (NumArgs == 1) |
| 3492 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3493 | else |
| 3494 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3495 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | // C++ [over.built]p5: |
| 3499 | // |
| 3500 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3501 | // cv-unqualified object type, and VQ is either volatile or |
| 3502 | // empty, there exist candidate operator functions of the form |
| 3503 | // |
| 3504 | // T*VQ& operator++(T*VQ&); |
| 3505 | // T*VQ& operator--(T*VQ&); |
| 3506 | // T* operator++(T*VQ&, int); |
| 3507 | // T* operator--(T*VQ&, int); |
| 3508 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3509 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3510 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3511 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3512 | continue; |
| 3513 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | QualType ParamTypes[2] = { |
| 3515 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3516 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3517 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3518 | // Without volatile |
| 3519 | if (NumArgs == 1) |
| 3520 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3521 | else |
| 3522 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3523 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3524 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3525 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3526 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3527 | ParamTypes[0] |
| 3528 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3529 | if (NumArgs == 1) |
| 3530 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3531 | else |
| 3532 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3533 | } |
| 3534 | } |
| 3535 | break; |
| 3536 | |
| 3537 | UnaryStar: |
| 3538 | // C++ [over.built]p6: |
| 3539 | // For every cv-qualified or cv-unqualified object type T, there |
| 3540 | // exist candidate operator functions of the form |
| 3541 | // |
| 3542 | // T& operator*(T*); |
| 3543 | // |
| 3544 | // C++ [over.built]p7: |
| 3545 | // For every function type T, there exist candidate operator |
| 3546 | // functions of the form |
| 3547 | // T& operator*(T*); |
| 3548 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3549 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3550 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3551 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3553 | &ParamTy, Args, 1, CandidateSet); |
| 3554 | } |
| 3555 | break; |
| 3556 | |
| 3557 | UnaryPlus: |
| 3558 | // C++ [over.built]p8: |
| 3559 | // For every type T, there exist candidate operator functions of |
| 3560 | // the form |
| 3561 | // |
| 3562 | // T* operator+(T*); |
| 3563 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3564 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3565 | QualType ParamTy = *Ptr; |
| 3566 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3569 | // Fall through |
| 3570 | |
| 3571 | UnaryMinus: |
| 3572 | // C++ [over.built]p9: |
| 3573 | // For every promoted arithmetic type T, there exist candidate |
| 3574 | // operator functions of the form |
| 3575 | // |
| 3576 | // T operator+(T); |
| 3577 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3579 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3580 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3581 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3582 | } |
| 3583 | break; |
| 3584 | |
| 3585 | case OO_Tilde: |
| 3586 | // C++ [over.built]p10: |
| 3587 | // For every promoted integral type T, there exist candidate |
| 3588 | // operator functions of the form |
| 3589 | // |
| 3590 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3592 | Int < LastPromotedIntegralType; ++Int) { |
| 3593 | QualType IntTy = ArithmeticTypes[Int]; |
| 3594 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3595 | } |
| 3596 | break; |
| 3597 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3598 | case OO_New: |
| 3599 | case OO_Delete: |
| 3600 | case OO_Array_New: |
| 3601 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3602 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3603 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3604 | break; |
| 3605 | |
| 3606 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3607 | UnaryAmp: |
| 3608 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3609 | // C++ [over.match.oper]p3: |
| 3610 | // -- For the operator ',', the unary operator '&', or the |
| 3611 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3612 | break; |
| 3613 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3614 | case OO_EqualEqual: |
| 3615 | case OO_ExclaimEqual: |
| 3616 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | // For every pointer to member type T, there exist candidate operator |
| 3618 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3619 | // |
| 3620 | // bool operator==(T,T); |
| 3621 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3623 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3624 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3625 | MemPtr != MemPtrEnd; |
| 3626 | ++MemPtr) { |
| 3627 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3628 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3629 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3630 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3631 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3633 | case OO_Less: |
| 3634 | case OO_Greater: |
| 3635 | case OO_LessEqual: |
| 3636 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3637 | // C++ [over.built]p15: |
| 3638 | // |
| 3639 | // For every pointer or enumeration type T, there exist |
| 3640 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3642 | // bool operator<(T, T); |
| 3643 | // bool operator>(T, T); |
| 3644 | // bool operator<=(T, T); |
| 3645 | // bool operator>=(T, T); |
| 3646 | // bool operator==(T, T); |
| 3647 | // bool operator!=(T, T); |
| 3648 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3649 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3650 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3651 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3654 | = CandidateTypes.enumeration_begin(); |
| 3655 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3656 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3657 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3658 | } |
| 3659 | |
| 3660 | // Fall through. |
| 3661 | isComparison = true; |
| 3662 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3663 | BinaryPlus: |
| 3664 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3665 | if (!isComparison) { |
| 3666 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3667 | |
| 3668 | // C++ [over.built]p13: |
| 3669 | // |
| 3670 | // For every cv-qualified or cv-unqualified object type T |
| 3671 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3673 | // T* operator+(T*, ptrdiff_t); |
| 3674 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3675 | // T* operator-(T*, ptrdiff_t); |
| 3676 | // T* operator+(ptrdiff_t, T*); |
| 3677 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3678 | // |
| 3679 | // C++ [over.built]p14: |
| 3680 | // |
| 3681 | // For every T, where T is a pointer to object type, there |
| 3682 | // exist candidate operator functions of the form |
| 3683 | // |
| 3684 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3686 | = CandidateTypes.pointer_begin(); |
| 3687 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3688 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3689 | |
| 3690 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3691 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3692 | |
| 3693 | if (Op == OO_Plus) { |
| 3694 | // T* operator+(ptrdiff_t, T*); |
| 3695 | ParamTypes[0] = ParamTypes[1]; |
| 3696 | ParamTypes[1] = *Ptr; |
| 3697 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3698 | } else { |
| 3699 | // ptrdiff_t operator-(T, T); |
| 3700 | ParamTypes[1] = *Ptr; |
| 3701 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3702 | Args, 2, CandidateSet); |
| 3703 | } |
| 3704 | } |
| 3705 | } |
| 3706 | // Fall through |
| 3707 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3708 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3709 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3710 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3711 | // C++ [over.built]p12: |
| 3712 | // |
| 3713 | // For every pair of promoted arithmetic types L and R, there |
| 3714 | // exist candidate operator functions of the form |
| 3715 | // |
| 3716 | // LR operator*(L, R); |
| 3717 | // LR operator/(L, R); |
| 3718 | // LR operator+(L, R); |
| 3719 | // LR operator-(L, R); |
| 3720 | // bool operator<(L, R); |
| 3721 | // bool operator>(L, R); |
| 3722 | // bool operator<=(L, R); |
| 3723 | // bool operator>=(L, R); |
| 3724 | // bool operator==(L, R); |
| 3725 | // bool operator!=(L, R); |
| 3726 | // |
| 3727 | // where LR is the result of the usual arithmetic conversions |
| 3728 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3729 | // |
| 3730 | // C++ [over.built]p24: |
| 3731 | // |
| 3732 | // For every pair of promoted arithmetic types L and R, there exist |
| 3733 | // candidate operator functions of the form |
| 3734 | // |
| 3735 | // LR operator?(bool, L, R); |
| 3736 | // |
| 3737 | // where LR is the result of the usual arithmetic conversions |
| 3738 | // between types L and R. |
| 3739 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3741 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3743 | Right < LastPromotedArithmeticType; ++Right) { |
| 3744 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3745 | QualType Result |
| 3746 | = isComparison |
| 3747 | ? Context.BoolTy |
| 3748 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3749 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3750 | } |
| 3751 | } |
| 3752 | break; |
| 3753 | |
| 3754 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3755 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3756 | case OO_Caret: |
| 3757 | case OO_Pipe: |
| 3758 | case OO_LessLess: |
| 3759 | case OO_GreaterGreater: |
| 3760 | // C++ [over.built]p17: |
| 3761 | // |
| 3762 | // For every pair of promoted integral types L and R, there |
| 3763 | // exist candidate operator functions of the form |
| 3764 | // |
| 3765 | // LR operator%(L, R); |
| 3766 | // LR operator&(L, R); |
| 3767 | // LR operator^(L, R); |
| 3768 | // LR operator|(L, R); |
| 3769 | // L operator<<(L, R); |
| 3770 | // L operator>>(L, R); |
| 3771 | // |
| 3772 | // where LR is the result of the usual arithmetic conversions |
| 3773 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3775 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3777 | Right < LastPromotedIntegralType; ++Right) { |
| 3778 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3779 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3780 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3781 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3782 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3783 | } |
| 3784 | } |
| 3785 | break; |
| 3786 | |
| 3787 | case OO_Equal: |
| 3788 | // C++ [over.built]p20: |
| 3789 | // |
| 3790 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3791 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3792 | // empty, there exist candidate operator functions of the form |
| 3793 | // |
| 3794 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3795 | for (BuiltinCandidateTypeSet::iterator |
| 3796 | Enum = CandidateTypes.enumeration_begin(), |
| 3797 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3798 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3800 | CandidateSet); |
| 3801 | for (BuiltinCandidateTypeSet::iterator |
| 3802 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3803 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3804 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3806 | CandidateSet); |
| 3807 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3808 | |
| 3809 | case OO_PlusEqual: |
| 3810 | case OO_MinusEqual: |
| 3811 | // C++ [over.built]p19: |
| 3812 | // |
| 3813 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3814 | // volatile or empty, there exist candidate operator functions |
| 3815 | // of the form |
| 3816 | // |
| 3817 | // T*VQ& operator=(T*VQ&, T*); |
| 3818 | // |
| 3819 | // C++ [over.built]p21: |
| 3820 | // |
| 3821 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3822 | // cv-unqualified object type and VQ is either volatile or |
| 3823 | // empty, there exist candidate operator functions of the form |
| 3824 | // |
| 3825 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3826 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3827 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3828 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3829 | QualType ParamTypes[2]; |
| 3830 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3831 | |
| 3832 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3833 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3834 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3835 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3836 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3837 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3838 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3839 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3840 | ParamTypes[0] |
| 3841 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3842 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3843 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3844 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3845 | } |
| 3846 | // Fall through. |
| 3847 | |
| 3848 | case OO_StarEqual: |
| 3849 | case OO_SlashEqual: |
| 3850 | // C++ [over.built]p18: |
| 3851 | // |
| 3852 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3853 | // VQ is either volatile or empty, and R is a promoted |
| 3854 | // arithmetic type, there exist candidate operator functions of |
| 3855 | // the form |
| 3856 | // |
| 3857 | // VQ L& operator=(VQ L&, R); |
| 3858 | // VQ L& operator*=(VQ L&, R); |
| 3859 | // VQ L& operator/=(VQ L&, R); |
| 3860 | // VQ L& operator+=(VQ L&, R); |
| 3861 | // VQ L& operator-=(VQ L&, R); |
| 3862 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3864 | Right < LastPromotedArithmeticType; ++Right) { |
| 3865 | QualType ParamTypes[2]; |
| 3866 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3867 | |
| 3868 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3869 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3870 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3871 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3872 | |
| 3873 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3874 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3875 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3876 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3877 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3878 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3879 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | break; |
| 3883 | |
| 3884 | case OO_PercentEqual: |
| 3885 | case OO_LessLessEqual: |
| 3886 | case OO_GreaterGreaterEqual: |
| 3887 | case OO_AmpEqual: |
| 3888 | case OO_CaretEqual: |
| 3889 | case OO_PipeEqual: |
| 3890 | // C++ [over.built]p22: |
| 3891 | // |
| 3892 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3893 | // is either volatile or empty, and R is a promoted integral |
| 3894 | // type, there exist candidate operator functions of the form |
| 3895 | // |
| 3896 | // VQ L& operator%=(VQ L&, R); |
| 3897 | // VQ L& operator<<=(VQ L&, R); |
| 3898 | // VQ L& operator>>=(VQ L&, R); |
| 3899 | // VQ L& operator&=(VQ L&, R); |
| 3900 | // VQ L& operator^=(VQ L&, R); |
| 3901 | // VQ L& operator|=(VQ L&, R); |
| 3902 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3904 | Right < LastPromotedIntegralType; ++Right) { |
| 3905 | QualType ParamTypes[2]; |
| 3906 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3907 | |
| 3908 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3909 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3910 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3911 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3912 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3913 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3914 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3915 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3916 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3917 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3918 | } |
| 3919 | } |
| 3920 | break; |
| 3921 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3922 | case OO_Exclaim: { |
| 3923 | // C++ [over.operator]p23: |
| 3924 | // |
| 3925 | // There also exist candidate operator functions of the form |
| 3926 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3928 | // bool operator&&(bool, bool); [BELOW] |
| 3929 | // bool operator||(bool, bool); [BELOW] |
| 3930 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3931 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3932 | /*IsAssignmentOperator=*/false, |
| 3933 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3934 | break; |
| 3935 | } |
| 3936 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3937 | case OO_AmpAmp: |
| 3938 | case OO_PipePipe: { |
| 3939 | // C++ [over.operator]p23: |
| 3940 | // |
| 3941 | // There also exist candidate operator functions of the form |
| 3942 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3943 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3944 | // bool operator&&(bool, bool); |
| 3945 | // bool operator||(bool, bool); |
| 3946 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3947 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3948 | /*IsAssignmentOperator=*/false, |
| 3949 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3950 | break; |
| 3951 | } |
| 3952 | |
| 3953 | case OO_Subscript: |
| 3954 | // C++ [over.built]p13: |
| 3955 | // |
| 3956 | // For every cv-qualified or cv-unqualified object type T there |
| 3957 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3958 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3959 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3960 | // T& operator[](T*, ptrdiff_t); |
| 3961 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3962 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3963 | // T& operator[](ptrdiff_t, T*); |
| 3964 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3965 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3966 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3967 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3968 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3969 | |
| 3970 | // T& operator[](T*, ptrdiff_t) |
| 3971 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3972 | |
| 3973 | // T& operator[](ptrdiff_t, T*); |
| 3974 | ParamTypes[0] = ParamTypes[1]; |
| 3975 | ParamTypes[1] = *Ptr; |
| 3976 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3977 | } |
| 3978 | break; |
| 3979 | |
| 3980 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3981 | // C++ [over.built]p11: |
| 3982 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 3983 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 3984 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 3985 | // there exist candidate operator functions of the form |
| 3986 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 3987 | // where CV12 is the union of CV1 and CV2. |
| 3988 | { |
| 3989 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3990 | CandidateTypes.pointer_begin(); |
| 3991 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3992 | QualType C1Ty = (*Ptr); |
| 3993 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3994 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3995 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3996 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3997 | if (!isa<RecordType>(C1)) |
| 3998 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3999 | // heuristic to reduce number of builtin candidates in the set. |
| 4000 | // Add volatile/restrict version only if there are conversions to a |
| 4001 | // volatile/restrict type. |
| 4002 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4003 | continue; |
| 4004 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4005 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4006 | } |
| 4007 | for (BuiltinCandidateTypeSet::iterator |
| 4008 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4009 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4010 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4011 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4012 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4013 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4014 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4015 | break; |
| 4016 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4017 | // build CV12 T& |
| 4018 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4019 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4020 | T.isVolatileQualified()) |
| 4021 | continue; |
| 4022 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4023 | T.isRestrictQualified()) |
| 4024 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4025 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4026 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4027 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4028 | } |
| 4029 | } |
| 4030 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4031 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4032 | |
| 4033 | case OO_Conditional: |
| 4034 | // Note that we don't consider the first argument, since it has been |
| 4035 | // contextually converted to bool long ago. The candidates below are |
| 4036 | // therefore added as binary. |
| 4037 | // |
| 4038 | // C++ [over.built]p24: |
| 4039 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4040 | // there exist candidate operator functions of the form |
| 4041 | // |
| 4042 | // T operator?(bool, T, T); |
| 4043 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4044 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4045 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4046 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4047 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4048 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4049 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4050 | CandidateTypes.member_pointer_begin(), |
| 4051 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4052 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4053 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4054 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4055 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4056 | } |
| 4057 | } |
| 4058 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4059 | /// \brief Add function candidates found via argument-dependent lookup |
| 4060 | /// to the set of overloading candidates. |
| 4061 | /// |
| 4062 | /// This routine performs argument-dependent name lookup based on the |
| 4063 | /// given function name (which may also be an operator name) and adds |
| 4064 | /// all of the overload candidates found by ADL to the overload |
| 4065 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4067 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 4068 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4069 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4070 | OverloadCandidateSet& CandidateSet, |
| 4071 | bool PartialOverloading) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4072 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4073 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4074 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 4075 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4076 | // Record all of the function candidates that we've already |
| 4077 | // added to the overload set, so that we don't add those same |
| 4078 | // candidates a second time. |
| 4079 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4080 | CandEnd = CandidateSet.end(); |
| 4081 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4082 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4083 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4084 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4085 | Functions.insert(FunTmpl); |
| 4086 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4087 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4088 | // FIXME: Pass in the explicit template arguments? |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 4089 | ArgumentDependentLookup(Name, /*Operator*/false, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4090 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4091 | // Erase all of the candidates we already knew about. |
| 4092 | // FIXME: This is suboptimal. Is there a better way? |
| 4093 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4094 | CandEnd = CandidateSet.end(); |
| 4095 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4096 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4097 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4098 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4099 | Functions.erase(FunTmpl); |
| 4100 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4101 | |
| 4102 | // For each of the ADL candidates we found, add it to the overload |
| 4103 | // set. |
| 4104 | for (FunctionSet::iterator Func = Functions.begin(), |
| 4105 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4106 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4107 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4108 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4109 | continue; |
| 4110 | |
| 4111 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 4112 | false, false, PartialOverloading); |
| 4113 | } else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4115 | ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4116 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4117 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4120 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4121 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4123 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4125 | // Define viable functions to be better candidates than non-viable |
| 4126 | // functions. |
| 4127 | if (!Cand2.Viable) |
| 4128 | return Cand1.Viable; |
| 4129 | else if (!Cand1.Viable) |
| 4130 | return false; |
| 4131 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4132 | // C++ [over.match.best]p1: |
| 4133 | // |
| 4134 | // -- if F is a static member function, ICS1(F) is defined such |
| 4135 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4136 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4137 | // better nor worse than ICS1(F). |
| 4138 | unsigned StartArg = 0; |
| 4139 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4140 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4141 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4142 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | // A viable function F1 is defined to be a better function than another |
| 4144 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4145 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4146 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4147 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4148 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4149 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4150 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4151 | Cand2.Conversions[ArgIdx])) { |
| 4152 | case ImplicitConversionSequence::Better: |
| 4153 | // Cand1 has a better conversion sequence. |
| 4154 | HasBetterConversion = true; |
| 4155 | break; |
| 4156 | |
| 4157 | case ImplicitConversionSequence::Worse: |
| 4158 | // Cand1 can't be better than Cand2. |
| 4159 | return false; |
| 4160 | |
| 4161 | case ImplicitConversionSequence::Indistinguishable: |
| 4162 | // Do nothing. |
| 4163 | break; |
| 4164 | } |
| 4165 | } |
| 4166 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4167 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4168 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4169 | if (HasBetterConversion) |
| 4170 | return true; |
| 4171 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4173 | // specialization, or, if not that, |
| 4174 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4175 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4176 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | |
| 4178 | // -- F1 and F2 are function template specializations, and the function |
| 4179 | // template for F1 is more specialized than the template for F2 |
| 4180 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4181 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4182 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4183 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4184 | if (FunctionTemplateDecl *BetterTemplate |
| 4185 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4186 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4187 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4188 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4189 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4190 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4191 | // -- the context is an initialization by user-defined conversion |
| 4192 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4193 | // from the return type of F1 to the destination type (i.e., |
| 4194 | // the type of the entity being initialized) is a better |
| 4195 | // conversion sequence than the standard conversion sequence |
| 4196 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | if (Cand1.Function && Cand2.Function && |
| 4198 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4199 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4200 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4201 | Cand2.FinalConversion)) { |
| 4202 | case ImplicitConversionSequence::Better: |
| 4203 | // Cand1 has a better conversion sequence. |
| 4204 | return true; |
| 4205 | |
| 4206 | case ImplicitConversionSequence::Worse: |
| 4207 | // Cand1 can't be better than Cand2. |
| 4208 | return false; |
| 4209 | |
| 4210 | case ImplicitConversionSequence::Indistinguishable: |
| 4211 | // Do nothing |
| 4212 | break; |
| 4213 | } |
| 4214 | } |
| 4215 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4216 | return false; |
| 4217 | } |
| 4218 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4220 | /// within an overload candidate set. |
| 4221 | /// |
| 4222 | /// \param CandidateSet the set of candidate functions. |
| 4223 | /// |
| 4224 | /// \param Loc the location of the function name (or operator symbol) for |
| 4225 | /// which overload resolution occurs. |
| 4226 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4228 | /// function, Best points to the candidate function found. |
| 4229 | /// |
| 4230 | /// \returns The result of overload resolution. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4231 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4232 | SourceLocation Loc, |
| 4233 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4234 | // Find the best viable function. |
| 4235 | Best = CandidateSet.end(); |
| 4236 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4237 | Cand != CandidateSet.end(); ++Cand) { |
| 4238 | if (Cand->Viable) { |
| 4239 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 4240 | Best = Cand; |
| 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | // If we didn't find any viable functions, abort. |
| 4245 | if (Best == CandidateSet.end()) |
| 4246 | return OR_No_Viable_Function; |
| 4247 | |
| 4248 | // Make sure that this function is better than every other viable |
| 4249 | // function. If not, we have an ambiguity. |
| 4250 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4251 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4252 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4253 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4254 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 4255 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4256 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4257 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4260 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4261 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4263 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4264 | return OR_Deleted; |
| 4265 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4266 | // C++ [basic.def.odr]p2: |
| 4267 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4268 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4269 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4270 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4271 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4272 | if (Best->Function) |
| 4273 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4274 | return OR_Success; |
| 4275 | } |
| 4276 | |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4277 | /// Notes the location of an overload candidate. |
| 4278 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
| 4279 | |
| 4280 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
| 4281 | // At least call it a 'constructor'. |
| 4282 | if (!Ctor->isImplicit()) { |
| 4283 | Diag(Ctor->getLocation(), diag::note_ovl_candidate_ctor); |
| 4284 | return; |
| 4285 | } |
| 4286 | |
| 4287 | CXXRecordDecl *Record = Ctor->getParent(); |
| 4288 | if (Ctor->isCopyConstructor()) { |
| 4289 | Diag(Record->getLocation(), diag::note_ovl_candidate_implicit_copy_ctor); |
| 4290 | return; |
| 4291 | } |
| 4292 | |
| 4293 | Diag(Record->getLocation(), diag::note_ovl_candidate_implicit_default_ctor); |
| 4294 | return; |
| 4295 | } |
| 4296 | |
| 4297 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4298 | // This actually gets spelled 'candidate function' for now, but |
| 4299 | // it doesn't hurt to split it out. |
| 4300 | if (!Meth->isImplicit()) { |
| 4301 | Diag(Meth->getLocation(), diag::note_ovl_candidate_meth); |
| 4302 | return; |
| 4303 | } |
| 4304 | |
| 4305 | assert(Meth->isCopyAssignment() |
| 4306 | && "implicit method is not copy assignment operator?"); |
| 4307 | Diag(Meth->getParent()->getLocation(), |
| 4308 | diag::note_ovl_candidate_implicit_copy_assign); |
| 4309 | return; |
| 4310 | } |
| 4311 | |
| 4312 | Diag(Fn->getLocation(), diag::note_ovl_candidate); |
| 4313 | } |
| 4314 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4315 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4316 | /// diagnostic messages containing the candidates in the candidate |
| 4317 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4318 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4319 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4320 | bool OnlyViable, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4321 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4322 | SourceLocation OpLoc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4323 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4324 | LastCand = CandidateSet.end(); |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4325 | bool Reported = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4326 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4327 | if (Cand->Viable || !OnlyViable) { |
| 4328 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4329 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4330 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4331 | // Deleted or "unavailable" function. |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4332 | Diag(Cand->Function->getLocation(), diag::note_ovl_candidate_deleted) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4333 | << Cand->Function->isDeleted(); |
Douglas Gregor | 1fdd89b | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 4334 | } else if (FunctionTemplateDecl *FunTmpl |
| 4335 | = Cand->Function->getPrimaryTemplate()) { |
| 4336 | // Function template specialization |
| 4337 | // FIXME: Give a better reason! |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4338 | Diag(Cand->Function->getLocation(), diag::note_ovl_template_candidate) |
Douglas Gregor | 1fdd89b | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 4339 | << getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 4340 | *Cand->Function->getTemplateSpecializationArgs()); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4341 | } else { |
| 4342 | // Normal function |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 4343 | bool errReported = false; |
| 4344 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 4345 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 4346 | const ImplicitConversionSequence &Conversion = |
| 4347 | Cand->Conversions[i]; |
| 4348 | if ((Conversion.ConversionKind != |
| 4349 | ImplicitConversionSequence::BadConversion) || |
| 4350 | Conversion.ConversionFunctionSet.size() == 0) |
| 4351 | continue; |
| 4352 | Diag(Cand->Function->getLocation(), |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4353 | diag::note_ovl_candidate_not_viable) << (i+1); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 4354 | errReported = true; |
| 4355 | for (int j = Conversion.ConversionFunctionSet.size()-1; |
| 4356 | j >= 0; j--) { |
| 4357 | FunctionDecl *Func = Conversion.ConversionFunctionSet[j]; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4358 | NoteOverloadCandidate(Func); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | if (!errReported) |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4363 | NoteOverloadCandidate(Cand->Function); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4364 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4365 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4366 | // Desugar the type of the surrogate down to a function type, |
| 4367 | // retaining as many typedefs as possible while still showing |
| 4368 | // the function type (and, therefore, its parameter types). |
| 4369 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4370 | bool isLValueReference = false; |
| 4371 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4372 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4373 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4374 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4375 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4376 | isLValueReference = true; |
| 4377 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4378 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4379 | FnType = FnTypeRef->getPointeeType(); |
| 4380 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4381 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4382 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4383 | FnType = FnTypePtr->getPointeeType(); |
| 4384 | isPointer = true; |
| 4385 | } |
| 4386 | // Desugar down to a function type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4387 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4388 | // Reconstruct the pointer/reference as appropriate. |
| 4389 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4390 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 4391 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4392 | |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4393 | Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4394 | << FnType; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4395 | } else if (OnlyViable) { |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4396 | assert(Cand->Conversions.size() <= 2 && |
Fariborz Jahanian | ad3607d | 2009-10-09 17:09:58 +0000 | [diff] [blame] | 4397 | "builtin-binary-operator-not-binary"); |
Fariborz Jahanian | 866b274 | 2009-10-16 23:25:02 +0000 | [diff] [blame] | 4398 | std::string TypeStr("operator"); |
| 4399 | TypeStr += Opc; |
| 4400 | TypeStr += "("; |
| 4401 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4402 | if (Cand->Conversions.size() == 1) { |
| 4403 | TypeStr += ")"; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4404 | Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
Fariborz Jahanian | 866b274 | 2009-10-16 23:25:02 +0000 | [diff] [blame] | 4405 | } |
| 4406 | else { |
| 4407 | TypeStr += ", "; |
| 4408 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4409 | TypeStr += ")"; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4410 | Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
Fariborz Jahanian | 866b274 | 2009-10-16 23:25:02 +0000 | [diff] [blame] | 4411 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4412 | } |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4413 | else if (!Cand->Viable && !Reported) { |
| 4414 | // Non-viability might be due to ambiguous user-defined conversions, |
| 4415 | // needed for built-in operators. Report them as well, but only once |
| 4416 | // as we have typically many built-in candidates. |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4417 | unsigned NoOperands = Cand->Conversions.size(); |
| 4418 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4419 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
| 4420 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion || |
| 4421 | ICS.ConversionFunctionSet.empty()) |
| 4422 | continue; |
| 4423 | if (CXXConversionDecl *Func = dyn_cast<CXXConversionDecl>( |
| 4424 | Cand->Conversions[ArgIdx].ConversionFunctionSet[0])) { |
| 4425 | QualType FromTy = |
| 4426 | QualType( |
| 4427 | static_cast<Type*>(ICS.UserDefined.Before.FromTypePtr),0); |
| 4428 | Diag(OpLoc,diag::note_ambiguous_type_conversion) |
| 4429 | << FromTy << Func->getConversionType(); |
| 4430 | } |
| 4431 | for (unsigned j = 0; j < ICS.ConversionFunctionSet.size(); j++) { |
| 4432 | FunctionDecl *Func = |
| 4433 | Cand->Conversions[ArgIdx].ConversionFunctionSet[j]; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4434 | NoteOverloadCandidate(Func); |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4435 | } |
| 4436 | } |
| 4437 | Reported = true; |
| 4438 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4439 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4443 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4444 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4445 | /// expression with overloaded function type and @p ToType is the type |
| 4446 | /// we're trying to resolve to. For example: |
| 4447 | /// |
| 4448 | /// @code |
| 4449 | /// int f(double); |
| 4450 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4452 | /// int (*pfd)(double) = f; // selects f(double) |
| 4453 | /// @endcode |
| 4454 | /// |
| 4455 | /// This routine returns the resulting FunctionDecl if it could be |
| 4456 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4457 | /// routine will emit diagnostics if there is an error. |
| 4458 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4459 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4460 | bool Complain) { |
| 4461 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4462 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4463 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4464 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4465 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4466 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4467 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4468 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4469 | FunctionType = MemTypePtr->getPointeeType(); |
| 4470 | IsMember = true; |
| 4471 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4472 | |
| 4473 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4474 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4475 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4476 | return 0; |
| 4477 | |
| 4478 | // Find the actual overloaded function declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4479 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4480 | // C++ [over.over]p1: |
| 4481 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4482 | // overloaded function name is ignored (5.1). ] |
| 4483 | Expr *OvlExpr = From->IgnoreParens(); |
| 4484 | |
| 4485 | // C++ [over.over]p1: |
| 4486 | // [...] The overloaded function name can be preceded by the & |
| 4487 | // operator. |
| 4488 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4489 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4490 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4491 | } |
| 4492 | |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4493 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4494 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4495 | |
| 4496 | llvm::SmallVector<NamedDecl*,8> Fns; |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4497 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4498 | // Look into the overloaded expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4499 | if (UnresolvedLookupExpr *UL |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4500 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4501 | Fns.append(UL->decls_begin(), UL->decls_end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4502 | if (UL->hasExplicitTemplateArgs()) { |
| 4503 | HasExplicitTemplateArgs = true; |
| 4504 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4505 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4506 | } else if (UnresolvedMemberExpr *ME |
| 4507 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4508 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4509 | if (ME->hasExplicitTemplateArgs()) { |
| 4510 | HasExplicitTemplateArgs = true; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4511 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4512 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4513 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4515 | // If we didn't actually find anything, we're done. |
| 4516 | if (Fns.empty()) |
| 4517 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4519 | // Look through all of the overloaded functions, searching for one |
| 4520 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4521 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4522 | bool FoundNonTemplateFunction = false; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4523 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4524 | E = Fns.end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4525 | // Look through any using declarations to find the underlying function. |
| 4526 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 4527 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4528 | // C++ [over.over]p3: |
| 4529 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4530 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4531 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4532 | // type "pointer-to-member-function." |
| 4533 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4534 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4536 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4538 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4540 | // static when converting to member pointer. |
| 4541 | if (Method->isStatic() == IsMember) |
| 4542 | continue; |
| 4543 | } else if (IsMember) |
| 4544 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4546 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4547 | // If the name is a function template, template argument deduction is |
| 4548 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4549 | // resulting template argument list is used to generate a single |
| 4550 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4551 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4552 | // FIXME: We don't really want to build the specialization here, do we? |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4553 | FunctionDecl *Specialization = 0; |
| 4554 | TemplateDeductionInfo Info(Context); |
| 4555 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4556 | = DeduceTemplateArguments(FunctionTemplate, |
| 4557 | (HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4558 | FunctionType, Specialization, Info)) { |
| 4559 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4560 | (void)Result; |
| 4561 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4562 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4563 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4565 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4566 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4567 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4568 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4569 | |
| 4570 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4572 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4573 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4574 | // Skip non-static functions when converting to pointer, and static |
| 4575 | // when converting to member pointer. |
| 4576 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4577 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4578 | |
| 4579 | // If we have explicit template arguments, skip non-templates. |
| 4580 | if (HasExplicitTemplateArgs) |
| 4581 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4582 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4583 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4584 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4585 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 4586 | QualType ResultTy; |
| 4587 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 4588 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 4589 | ResultTy)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4590 | Matches.insert(cast<FunctionDecl>(FunDecl->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4591 | FoundNonTemplateFunction = true; |
| 4592 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4596 | // If there were 0 or 1 matches, we're done. |
| 4597 | if (Matches.empty()) |
| 4598 | return 0; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4599 | else if (Matches.size() == 1) { |
| 4600 | FunctionDecl *Result = *Matches.begin(); |
| 4601 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4602 | return Result; |
| 4603 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4604 | |
| 4605 | // C++ [over.over]p4: |
| 4606 | // If more than one function is selected, [...] |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4607 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4608 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4609 | // [...] and any given function template specialization F1 is |
| 4610 | // eliminated if the set contains a second function template |
| 4611 | // specialization whose function template is more specialized |
| 4612 | // than the function template of F1 according to the partial |
| 4613 | // ordering rules of 14.5.5.2. |
| 4614 | |
| 4615 | // The algorithm specified above is quadratic. We instead use a |
| 4616 | // two-pass algorithm (similar to the one used to identify the |
| 4617 | // best viable function in an overload set) that identifies the |
| 4618 | // best function template (if it exists). |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4619 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4620 | Matches.end()); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4621 | FunctionDecl *Result = |
| 4622 | getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4623 | TPOC_Other, From->getLocStart(), |
| 4624 | PDiag(), |
| 4625 | PDiag(diag::err_addr_ovl_ambiguous) |
| 4626 | << TemplateMatches[0]->getDeclName(), |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4627 | PDiag(diag::note_ovl_template_candidate)); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4628 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4629 | return Result; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4632 | // [...] any function template specializations in the set are |
| 4633 | // eliminated if the set also contains a non-template function, [...] |
| 4634 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4635 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4636 | if ((*M)->getPrimaryTemplate() == 0) |
| 4637 | RemainingMatches.push_back(*M); |
| 4638 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4640 | // selected function. |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4641 | if (RemainingMatches.size() == 1) { |
| 4642 | FunctionDecl *Result = RemainingMatches.front(); |
| 4643 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4644 | return Result; |
| 4645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4647 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4648 | // returns (even if we issue the diagnostics here). |
| 4649 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4650 | << RemainingMatches[0]->getDeclName(); |
| 4651 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4652 | NoteOverloadCandidate(RemainingMatches[I]); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4653 | return 0; |
| 4654 | } |
| 4655 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4656 | /// \brief Given an expression that refers to an overloaded function, try to |
| 4657 | /// resolve that overloaded function expression down to a single function. |
| 4658 | /// |
| 4659 | /// This routine can only resolve template-ids that refer to a single function |
| 4660 | /// template, where that template-id refers to a single template whose template |
| 4661 | /// arguments are either provided by the template-id or have defaults, |
| 4662 | /// as described in C++0x [temp.arg.explicit]p3. |
| 4663 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 4664 | // C++ [over.over]p1: |
| 4665 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4666 | // overloaded function name is ignored (5.1). ] |
| 4667 | Expr *OvlExpr = From->IgnoreParens(); |
| 4668 | |
| 4669 | // C++ [over.over]p1: |
| 4670 | // [...] The overloaded function name can be preceded by the & |
| 4671 | // operator. |
| 4672 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4673 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4674 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4675 | } |
| 4676 | |
| 4677 | bool HasExplicitTemplateArgs = false; |
| 4678 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 4679 | |
| 4680 | llvm::SmallVector<NamedDecl*,8> Fns; |
| 4681 | |
| 4682 | // Look into the overloaded expression. |
| 4683 | if (UnresolvedLookupExpr *UL |
| 4684 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4685 | Fns.append(UL->decls_begin(), UL->decls_end()); |
| 4686 | if (UL->hasExplicitTemplateArgs()) { |
| 4687 | HasExplicitTemplateArgs = true; |
| 4688 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4689 | } |
| 4690 | } else if (UnresolvedMemberExpr *ME |
| 4691 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4692 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4693 | if (ME->hasExplicitTemplateArgs()) { |
| 4694 | HasExplicitTemplateArgs = true; |
| 4695 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | // If we didn't actually find any template-ids, we're done. |
| 4700 | if (Fns.empty() || !HasExplicitTemplateArgs) |
| 4701 | return 0; |
| 4702 | |
| 4703 | // Look through all of the overloaded functions, searching for one |
| 4704 | // whose type matches exactly. |
| 4705 | FunctionDecl *Matched = 0; |
| 4706 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4707 | E = Fns.end(); I != E; ++I) { |
| 4708 | // C++0x [temp.arg.explicit]p3: |
| 4709 | // [...] In contexts where deduction is done and fails, or in contexts |
| 4710 | // where deduction is not done, if a template argument list is |
| 4711 | // specified and it, along with any default template arguments, |
| 4712 | // identifies a single function template specialization, then the |
| 4713 | // template-id is an lvalue for the function template specialization. |
| 4714 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 4715 | |
| 4716 | // C++ [over.over]p2: |
| 4717 | // If the name is a function template, template argument deduction is |
| 4718 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4719 | // resulting template argument list is used to generate a single |
| 4720 | // function template specialization, which is added to the set of |
| 4721 | // overloaded functions considered. |
| 4722 | // FIXME: We don't really want to build the specialization here, do we? |
| 4723 | FunctionDecl *Specialization = 0; |
| 4724 | TemplateDeductionInfo Info(Context); |
| 4725 | if (TemplateDeductionResult Result |
| 4726 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 4727 | Specialization, Info)) { |
| 4728 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4729 | (void)Result; |
| 4730 | continue; |
| 4731 | } |
| 4732 | |
| 4733 | // Multiple matches; we can't resolve to a single declaration. |
| 4734 | if (Matched) |
| 4735 | return 0; |
| 4736 | |
| 4737 | Matched = Specialization; |
| 4738 | } |
| 4739 | |
| 4740 | return Matched; |
| 4741 | } |
| 4742 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4743 | /// \brief Add a single candidate to the overload set. |
| 4744 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4745 | NamedDecl *Callee, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4746 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4747 | Expr **Args, unsigned NumArgs, |
| 4748 | OverloadCandidateSet &CandidateSet, |
| 4749 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4750 | if (isa<UsingShadowDecl>(Callee)) |
| 4751 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 4752 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4753 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4754 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4755 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4756 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4757 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | if (FunctionTemplateDecl *FuncTemplate |
| 4761 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4762 | S.AddTemplateOverloadCandidate(FuncTemplate, ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4763 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4764 | return; |
| 4765 | } |
| 4766 | |
| 4767 | assert(false && "unhandled case in overloaded call candidate"); |
| 4768 | |
| 4769 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4773 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4774 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4775 | Expr **Args, unsigned NumArgs, |
| 4776 | OverloadCandidateSet &CandidateSet, |
| 4777 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4778 | |
| 4779 | #ifndef NDEBUG |
| 4780 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 4781 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4782 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4783 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4784 | // and let Y be the lookup set produced by argument dependent |
| 4785 | // lookup (defined as follows). If X contains |
| 4786 | // |
| 4787 | // -- a declaration of a class member, or |
| 4788 | // |
| 4789 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4790 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4791 | // |
| 4792 | // -- a declaration that is neither a function or a function |
| 4793 | // template |
| 4794 | // |
| 4795 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4796 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4797 | if (ULE->requiresADL()) { |
| 4798 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 4799 | E = ULE->decls_end(); I != E; ++I) { |
| 4800 | assert(!(*I)->getDeclContext()->isRecord()); |
| 4801 | assert(isa<UsingShadowDecl>(*I) || |
| 4802 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 4803 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | #endif |
| 4807 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4808 | // It would be nice to avoid this copy. |
| 4809 | TemplateArgumentListInfo TABuffer; |
| 4810 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 4811 | if (ULE->hasExplicitTemplateArgs()) { |
| 4812 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 4813 | ExplicitTemplateArgs = &TABuffer; |
| 4814 | } |
| 4815 | |
| 4816 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 4817 | E = ULE->decls_end(); I != E; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4818 | AddOverloadedCallCandidate(*this, *I, ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4819 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4820 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4821 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4822 | if (ULE->requiresADL()) |
| 4823 | AddArgumentDependentLookupCandidates(ULE->getName(), Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4824 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4825 | CandidateSet, |
| 4826 | PartialOverloading); |
| 4827 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4828 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4829 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 4830 | Expr **Args, unsigned NumArgs) { |
| 4831 | Fn->Destroy(SemaRef.Context); |
| 4832 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4833 | Args[Arg]->Destroy(SemaRef.Context); |
| 4834 | return SemaRef.ExprError(); |
| 4835 | } |
| 4836 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4837 | /// Attempts to recover from a call where no functions were found. |
| 4838 | /// |
| 4839 | /// Returns true if new candidates were found. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4840 | static Sema::OwningExprResult |
| 4841 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 4842 | UnresolvedLookupExpr *ULE, |
| 4843 | SourceLocation LParenLoc, |
| 4844 | Expr **Args, unsigned NumArgs, |
| 4845 | SourceLocation *CommaLocs, |
| 4846 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4847 | |
| 4848 | CXXScopeSpec SS; |
| 4849 | if (ULE->getQualifier()) { |
| 4850 | SS.setScopeRep(ULE->getQualifier()); |
| 4851 | SS.setRange(ULE->getQualifierRange()); |
| 4852 | } |
| 4853 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4854 | TemplateArgumentListInfo TABuffer; |
| 4855 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 4856 | if (ULE->hasExplicitTemplateArgs()) { |
| 4857 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 4858 | ExplicitTemplateArgs = &TABuffer; |
| 4859 | } |
| 4860 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4861 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 4862 | Sema::LookupOrdinaryName); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 4863 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4864 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4865 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4866 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 4867 | |
| 4868 | // Build an implicit member call if appropriate. Just drop the |
| 4869 | // casts and such from the call, we don't really care. |
| 4870 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 4871 | if ((*R.begin())->isCXXClassMember()) |
| 4872 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 4873 | else if (ExplicitTemplateArgs) |
| 4874 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 4875 | else |
| 4876 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 4877 | |
| 4878 | if (NewFn.isInvalid()) |
| 4879 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 4880 | |
| 4881 | Fn->Destroy(SemaRef.Context); |
| 4882 | |
| 4883 | // This shouldn't cause an infinite loop because we're giving it |
| 4884 | // an expression with non-empty lookup results, which should never |
| 4885 | // end up here. |
| 4886 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 4887 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 4888 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4889 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4890 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4891 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4892 | /// (which eventually refers to the declaration Func) and the call |
| 4893 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4894 | /// to a specific function. If overload resolution succeeds, returns |
| 4895 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4896 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4897 | /// arguments and Fn, and returns NULL. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4898 | Sema::OwningExprResult |
| 4899 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 4900 | SourceLocation LParenLoc, |
| 4901 | Expr **Args, unsigned NumArgs, |
| 4902 | SourceLocation *CommaLocs, |
| 4903 | SourceLocation RParenLoc) { |
| 4904 | #ifndef NDEBUG |
| 4905 | if (ULE->requiresADL()) { |
| 4906 | // To do ADL, we must have found an unqualified name. |
| 4907 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 4908 | |
| 4909 | // We don't perform ADL for implicit declarations of builtins. |
| 4910 | // Verify that this was correctly set up. |
| 4911 | FunctionDecl *F; |
| 4912 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 4913 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 4914 | F->getBuiltinID() && F->isImplicit()) |
| 4915 | assert(0 && "performing ADL for builtin"); |
| 4916 | |
| 4917 | // We don't perform ADL in C. |
| 4918 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 4919 | } |
| 4920 | #endif |
| 4921 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4922 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4923 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4924 | // Add the functions denoted by the callee to the set of candidate |
| 4925 | // functions, including those from argument-dependent lookup. |
| 4926 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4927 | |
| 4928 | // If we found nothing, try to recover. |
| 4929 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 4930 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4931 | if (CandidateSet.empty()) |
| 4932 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 4933 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4934 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4935 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4936 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4937 | case OR_Success: { |
| 4938 | FunctionDecl *FDecl = Best->Function; |
| 4939 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 4940 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 4941 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4942 | |
| 4943 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4944 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4945 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4946 | << ULE->getName() << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4947 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4948 | break; |
| 4949 | |
| 4950 | case OR_Ambiguous: |
| 4951 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4952 | << ULE->getName() << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4953 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4954 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4955 | |
| 4956 | case OR_Deleted: |
| 4957 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4958 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4959 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4960 | << Fn->getSourceRange(); |
| 4961 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4962 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4966 | // return NULL. |
| 4967 | Fn->Destroy(Context); |
| 4968 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4969 | Args[Arg]->Destroy(Context); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4970 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 4973 | static bool IsOverloaded(const Sema::FunctionSet &Functions) { |
| 4974 | return Functions.size() > 1 || |
| 4975 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 4976 | } |
| 4977 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4978 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4979 | /// operator. |
| 4980 | /// |
| 4981 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4982 | /// |
| 4983 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4984 | /// operator. |
| 4985 | /// |
| 4986 | /// \param Functions The set of non-member functions that will be |
| 4987 | /// considered by overload resolution. The caller needs to build this |
| 4988 | /// set based on the context using, e.g., |
| 4989 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4990 | /// set should not contain any member functions; those will be added |
| 4991 | /// by CreateOverloadedUnaryOp(). |
| 4992 | /// |
| 4993 | /// \param input The input argument. |
| 4994 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4995 | unsigned OpcIn, |
| 4996 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4997 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4998 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4999 | Expr *Input = (Expr *)input.get(); |
| 5000 | |
| 5001 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5002 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5003 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5004 | |
| 5005 | Expr *Args[2] = { Input, 0 }; |
| 5006 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5007 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5008 | // For post-increment and post-decrement, add the implicit '0' as |
| 5009 | // the second argument, so that we know this is a post-increment or |
| 5010 | // post-decrement. |
| 5011 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5012 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5013 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5014 | SourceLocation()); |
| 5015 | NumArgs = 2; |
| 5016 | } |
| 5017 | |
| 5018 | if (Input->isTypeDependent()) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5019 | UnresolvedLookupExpr *Fn |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5020 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5021 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5022 | /*ADL*/ true, IsOverloaded(Functions)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5024 | FuncEnd = Functions.end(); |
| 5025 | Func != FuncEnd; ++Func) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5026 | Fn->addDecl(*Func); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5028 | input.release(); |
| 5029 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5030 | &Args[0], NumArgs, |
| 5031 | Context.DependentTy, |
| 5032 | OpLoc)); |
| 5033 | } |
| 5034 | |
| 5035 | // Build an empty overload set. |
| 5036 | OverloadCandidateSet CandidateSet; |
| 5037 | |
| 5038 | // Add the candidates from the given function set. |
| 5039 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 5040 | |
| 5041 | // Add operator candidates that are member functions. |
| 5042 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5043 | |
| 5044 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5045 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5046 | |
| 5047 | // Perform overload resolution. |
| 5048 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5049 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5050 | case OR_Success: { |
| 5051 | // We found a built-in operator or an overloaded operator. |
| 5052 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5053 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5054 | if (FnDecl) { |
| 5055 | // We matched an overloaded operator. Build a call to that |
| 5056 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5057 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5058 | // Convert the arguments. |
| 5059 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5060 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 5061 | return ExprError(); |
| 5062 | } else { |
| 5063 | // Convert the arguments. |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5064 | OwningExprResult InputInit |
| 5065 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5066 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5067 | SourceLocation(), |
| 5068 | move(input)); |
| 5069 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5070 | return ExprError(); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5071 | |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5072 | input = move(InputInit); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5073 | Input = (Expr *)input.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | // Determine the result type |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5077 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5078 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5079 | // Build the actual expression node. |
| 5080 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5081 | SourceLocation()); |
| 5082 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5083 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5084 | input.release(); |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5085 | Args[0] = Input; |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5086 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5087 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5088 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5089 | |
| 5090 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5091 | FnDecl)) |
| 5092 | return ExprError(); |
| 5093 | |
| 5094 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5095 | } else { |
| 5096 | // We matched a built-in operator. Convert the arguments, then |
| 5097 | // break out so that we will build the appropriate built-in |
| 5098 | // operator node. |
| 5099 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5100 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5101 | return ExprError(); |
| 5102 | |
| 5103 | break; |
| 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | case OR_No_Viable_Function: |
| 5108 | // No viable function; fall through to handling this as a |
| 5109 | // built-in operator, which will produce an error message for us. |
| 5110 | break; |
| 5111 | |
| 5112 | case OR_Ambiguous: |
| 5113 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5114 | << UnaryOperator::getOpcodeStr(Opc) |
| 5115 | << Input->getSourceRange(); |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5116 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 5117 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5118 | return ExprError(); |
| 5119 | |
| 5120 | case OR_Deleted: |
| 5121 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5122 | << Best->Function->isDeleted() |
| 5123 | << UnaryOperator::getOpcodeStr(Opc) |
| 5124 | << Input->getSourceRange(); |
| 5125 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5126 | return ExprError(); |
| 5127 | } |
| 5128 | |
| 5129 | // Either we found no viable overloaded operator or we matched a |
| 5130 | // built-in operator. In either case, fall through to trying to |
| 5131 | // build a built-in operation. |
| 5132 | input.release(); |
| 5133 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5134 | } |
| 5135 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5136 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5137 | /// operator. |
| 5138 | /// |
| 5139 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5140 | /// |
| 5141 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5142 | /// operator. |
| 5143 | /// |
| 5144 | /// \param Functions The set of non-member functions that will be |
| 5145 | /// considered by overload resolution. The caller needs to build this |
| 5146 | /// set based on the context using, e.g., |
| 5147 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5148 | /// set should not contain any member functions; those will be added |
| 5149 | /// by CreateOverloadedBinOp(). |
| 5150 | /// |
| 5151 | /// \param LHS Left-hand argument. |
| 5152 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5153 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5154 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5155 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5156 | FunctionSet &Functions, |
| 5157 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5158 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5159 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5160 | |
| 5161 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5162 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5163 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5164 | |
| 5165 | // If either side is type-dependent, create an appropriate dependent |
| 5166 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5167 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5168 | if (Functions.empty()) { |
| 5169 | // If there are no functions to store, just build a dependent |
| 5170 | // BinaryOperator or CompoundAssignment. |
| 5171 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5172 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5173 | Context.DependentTy, OpLoc)); |
| 5174 | |
| 5175 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5176 | Context.DependentTy, |
| 5177 | Context.DependentTy, |
| 5178 | Context.DependentTy, |
| 5179 | OpLoc)); |
| 5180 | } |
| 5181 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5182 | UnresolvedLookupExpr *Fn |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5183 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5184 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5185 | /* ADL */ true, IsOverloaded(Functions)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5186 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5187 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5188 | FuncEnd = Functions.end(); |
| 5189 | Func != FuncEnd; ++Func) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5190 | Fn->addDecl(*Func); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5191 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5192 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5193 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5194 | Context.DependentTy, |
| 5195 | OpLoc)); |
| 5196 | } |
| 5197 | |
| 5198 | // If this is the .* operator, which is not overloadable, just |
| 5199 | // create a built-in binary operator. |
| 5200 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5201 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5202 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 5203 | // If this is the assignment operator, we only perform overload resolution |
| 5204 | // if the left-hand side is a class or enumeration type. This is actually |
| 5205 | // a hack. The standard requires that we do overload resolution between the |
| 5206 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5207 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5208 | // Note that we go the traditional code path for compound assignment forms. |
| 5209 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5210 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5211 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5212 | // Build an empty overload set. |
| 5213 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5214 | |
| 5215 | // Add the candidates from the given function set. |
| 5216 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 5217 | |
| 5218 | // Add operator candidates that are member functions. |
| 5219 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5220 | |
| 5221 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5222 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5223 | |
| 5224 | // Perform overload resolution. |
| 5225 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5226 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5227 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5228 | // We found a built-in operator or an overloaded operator. |
| 5229 | FunctionDecl *FnDecl = Best->Function; |
| 5230 | |
| 5231 | if (FnDecl) { |
| 5232 | // We matched an overloaded operator. Build a call to that |
| 5233 | // operator. |
| 5234 | |
| 5235 | // Convert the arguments. |
| 5236 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5237 | OwningExprResult Arg1 |
| 5238 | = PerformCopyInitialization( |
| 5239 | InitializedEntity::InitializeParameter( |
| 5240 | FnDecl->getParamDecl(0)), |
| 5241 | SourceLocation(), |
| 5242 | Owned(Args[1])); |
| 5243 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5244 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5245 | |
| 5246 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5247 | return ExprError(); |
| 5248 | |
| 5249 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5250 | } else { |
| 5251 | // Convert the arguments. |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5252 | OwningExprResult Arg0 |
| 5253 | = PerformCopyInitialization( |
| 5254 | InitializedEntity::InitializeParameter( |
| 5255 | FnDecl->getParamDecl(0)), |
| 5256 | SourceLocation(), |
| 5257 | Owned(Args[0])); |
| 5258 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5259 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5260 | |
| 5261 | OwningExprResult Arg1 |
| 5262 | = PerformCopyInitialization( |
| 5263 | InitializedEntity::InitializeParameter( |
| 5264 | FnDecl->getParamDecl(1)), |
| 5265 | SourceLocation(), |
| 5266 | Owned(Args[1])); |
| 5267 | if (Arg1.isInvalid()) |
| 5268 | return ExprError(); |
| 5269 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5270 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | // Determine the result type |
| 5274 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5275 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5276 | ResultTy = ResultTy.getNonReferenceType(); |
| 5277 | |
| 5278 | // Build the actual expression node. |
| 5279 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 5280 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5281 | UsualUnaryConversions(FnExpr); |
| 5282 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5283 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5284 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5285 | Args, 2, ResultTy, |
| 5286 | OpLoc)); |
| 5287 | |
| 5288 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5289 | FnDecl)) |
| 5290 | return ExprError(); |
| 5291 | |
| 5292 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5293 | } else { |
| 5294 | // We matched a built-in operator. Convert the arguments, then |
| 5295 | // break out so that we will build the appropriate built-in |
| 5296 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5297 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5298 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5299 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5300 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5301 | return ExprError(); |
| 5302 | |
| 5303 | break; |
| 5304 | } |
| 5305 | } |
| 5306 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5307 | case OR_No_Viable_Function: { |
| 5308 | // C++ [over.match.oper]p9: |
| 5309 | // If the operator is the operator , [...] and there are no |
| 5310 | // viable functions, then the operator is assumed to be the |
| 5311 | // built-in operator and interpreted according to clause 5. |
| 5312 | if (Opc == BinaryOperator::Comma) |
| 5313 | break; |
| 5314 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5315 | // For class as left operand for assignment or compound assigment operator |
| 5316 | // do not fall through to handling in built-in, but report that no overloaded |
| 5317 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5318 | OwningExprResult Result = ExprError(); |
| 5319 | if (Args[0]->getType()->isRecordType() && |
| 5320 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5321 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5322 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5323 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5324 | } else { |
| 5325 | // No viable function; try to create a built-in operation, which will |
| 5326 | // produce an error. Then, show the non-viable candidates. |
| 5327 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5328 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5329 | assert(Result.isInvalid() && |
| 5330 | "C++ binary operator overloading is missing candidates!"); |
| 5331 | if (Result.isInvalid()) |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5332 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false, |
| 5333 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5334 | return move(Result); |
| 5335 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5336 | |
| 5337 | case OR_Ambiguous: |
| 5338 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5339 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5340 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5341 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 5342 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5343 | return ExprError(); |
| 5344 | |
| 5345 | case OR_Deleted: |
| 5346 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5347 | << Best->Function->isDeleted() |
| 5348 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5349 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5350 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5351 | return ExprError(); |
| 5352 | } |
| 5353 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5354 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5355 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5358 | Action::OwningExprResult |
| 5359 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5360 | SourceLocation RLoc, |
| 5361 | ExprArg Base, ExprArg Idx) { |
| 5362 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5363 | static_cast<Expr*>(Idx.get()) }; |
| 5364 | DeclarationName OpName = |
| 5365 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5366 | |
| 5367 | // If either side is type-dependent, create an appropriate dependent |
| 5368 | // expression. |
| 5369 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5370 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5371 | UnresolvedLookupExpr *Fn |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5372 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5373 | 0, SourceRange(), OpName, LLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5374 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5375 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5376 | |
| 5377 | Base.release(); |
| 5378 | Idx.release(); |
| 5379 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5380 | Args, 2, |
| 5381 | Context.DependentTy, |
| 5382 | RLoc)); |
| 5383 | } |
| 5384 | |
| 5385 | // Build an empty overload set. |
| 5386 | OverloadCandidateSet CandidateSet; |
| 5387 | |
| 5388 | // Subscript can only be overloaded as a member function. |
| 5389 | |
| 5390 | // Add operator candidates that are member functions. |
| 5391 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5392 | |
| 5393 | // Add builtin operator candidates. |
| 5394 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5395 | |
| 5396 | // Perform overload resolution. |
| 5397 | OverloadCandidateSet::iterator Best; |
| 5398 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5399 | case OR_Success: { |
| 5400 | // We found a built-in operator or an overloaded operator. |
| 5401 | FunctionDecl *FnDecl = Best->Function; |
| 5402 | |
| 5403 | if (FnDecl) { |
| 5404 | // We matched an overloaded operator. Build a call to that |
| 5405 | // operator. |
| 5406 | |
| 5407 | // Convert the arguments. |
| 5408 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5409 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 5410 | PerformCopyInitialization(Args[1], |
| 5411 | FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5412 | AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5413 | return ExprError(); |
| 5414 | |
| 5415 | // Determine the result type |
| 5416 | QualType ResultTy |
| 5417 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5418 | ResultTy = ResultTy.getNonReferenceType(); |
| 5419 | |
| 5420 | // Build the actual expression node. |
| 5421 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5422 | LLoc); |
| 5423 | UsualUnaryConversions(FnExpr); |
| 5424 | |
| 5425 | Base.release(); |
| 5426 | Idx.release(); |
| 5427 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5428 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5429 | FnExpr, Args, 2, |
| 5430 | ResultTy, RLoc)); |
| 5431 | |
| 5432 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5433 | FnDecl)) |
| 5434 | return ExprError(); |
| 5435 | |
| 5436 | return MaybeBindToTemporary(TheCall.release()); |
| 5437 | } else { |
| 5438 | // We matched a built-in operator. Convert the arguments, then |
| 5439 | // break out so that we will build the appropriate built-in |
| 5440 | // operator node. |
| 5441 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5442 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5443 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5444 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5445 | return ExprError(); |
| 5446 | |
| 5447 | break; |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5452 | if (CandidateSet.empty()) |
| 5453 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5454 | << Args[0]->getType() << /*subscript*/ 0 |
| 5455 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5456 | else |
| 5457 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5458 | << Args[0]->getType() |
| 5459 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5460 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false, |
| 5461 | "[]", LLoc); |
| 5462 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | case OR_Ambiguous: |
| 5466 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5467 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5468 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 5469 | "[]", LLoc); |
| 5470 | return ExprError(); |
| 5471 | |
| 5472 | case OR_Deleted: |
| 5473 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5474 | << Best->Function->isDeleted() << "[]" |
| 5475 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5476 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5477 | return ExprError(); |
| 5478 | } |
| 5479 | |
| 5480 | // We matched a built-in operator; build it. |
| 5481 | Base.release(); |
| 5482 | Idx.release(); |
| 5483 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5484 | Owned(Args[1]), RLoc); |
| 5485 | } |
| 5486 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5487 | /// BuildCallToMemberFunction - Build a call to a member |
| 5488 | /// function. MemExpr is the expression that refers to the member |
| 5489 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5490 | /// arguments to the function call (not including the object |
| 5491 | /// parameter). The caller needs to validate that the member |
| 5492 | /// expression refers to a member function or an overloaded member |
| 5493 | /// function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5494 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5495 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5496 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5497 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5498 | SourceLocation RParenLoc) { |
| 5499 | // Dig out the member expression. This holds both the object |
| 5500 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5501 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 5502 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5503 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5504 | CXXMethodDecl *Method = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5505 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 5506 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5507 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 5508 | } else { |
| 5509 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5510 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5511 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5512 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5513 | // Add overload candidates |
| 5514 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5515 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5516 | // FIXME: avoid copy. |
| 5517 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 5518 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 5519 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 5520 | TemplateArgs = &TemplateArgsBuffer; |
| 5521 | } |
| 5522 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5523 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 5524 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 5525 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5526 | NamedDecl *Func = *I; |
| 5527 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 5528 | if (isa<UsingShadowDecl>(Func)) |
| 5529 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 5530 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5531 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5532 | // If explicit template arguments were provided, we can't call a |
| 5533 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5534 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5535 | continue; |
| 5536 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5537 | AddMethodCandidate(Method, ActingDC, ObjectType, Args, NumArgs, |
| 5538 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5539 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5540 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5541 | ActingDC, TemplateArgs, |
| 5542 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5543 | CandidateSet, |
| 5544 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5545 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5546 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5547 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5548 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 5549 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5550 | OverloadCandidateSet::iterator Best; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5551 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5552 | case OR_Success: |
| 5553 | Method = cast<CXXMethodDecl>(Best->Function); |
| 5554 | break; |
| 5555 | |
| 5556 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5557 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5558 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5559 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5560 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5561 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5562 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5563 | |
| 5564 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5565 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5566 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5567 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5568 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5569 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5570 | |
| 5571 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5572 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5573 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5574 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5575 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5576 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5577 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5580 | MemExprE = FixOverloadedFunctionReference(MemExprE, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5581 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5582 | // If overload resolution picked a static member, build a |
| 5583 | // non-member call based on that function. |
| 5584 | if (Method->isStatic()) { |
| 5585 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 5586 | Args, NumArgs, RParenLoc); |
| 5587 | } |
| 5588 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5589 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5593 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5594 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5595 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5596 | Method->getResultType().getNonReferenceType(), |
| 5597 | RParenLoc)); |
| 5598 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5599 | // Check for a valid return type. |
| 5600 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 5601 | TheCall.get(), Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5602 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5603 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5604 | // Convert the object argument (for a non-static member function call). |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5605 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5606 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5607 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5608 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5609 | MemExpr->setBase(ObjectArg); |
| 5610 | |
| 5611 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5612 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5614 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5615 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5616 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5617 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5618 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 5619 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5620 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5623 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 5624 | /// type (C++ [over.call.object]), which can end up invoking an |
| 5625 | /// overloaded function call operator (@c operator()) or performing a |
| 5626 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5627 | Sema::ExprResult |
| 5628 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 5629 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5630 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5631 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5632 | SourceLocation RParenLoc) { |
| 5633 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5634 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5636 | // C++ [over.call.object]p1: |
| 5637 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5638 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5639 | // candidate functions includes at least the function call |
| 5640 | // operators of T. The function call operators of T are obtained by |
| 5641 | // ordinary lookup of the name operator() in the context of |
| 5642 | // (E).operator(). |
| 5643 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5644 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5645 | |
| 5646 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 5647 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 5648 | << Object->getSourceRange())) |
| 5649 | return true; |
| 5650 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5651 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 5652 | LookupQualifiedName(R, Record->getDecl()); |
| 5653 | R.suppressDiagnostics(); |
| 5654 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5655 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5656 | Oper != OperEnd; ++Oper) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5657 | AddMethodCandidate(*Oper, Object->getType(), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5658 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5659 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5660 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5661 | // C++ [over.call.object]p2: |
| 5662 | // In addition, for each conversion function declared in T of the |
| 5663 | // form |
| 5664 | // |
| 5665 | // operator conversion-type-id () cv-qualifier; |
| 5666 | // |
| 5667 | // where cv-qualifier is the same cv-qualification as, or a |
| 5668 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 5669 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 5670 | // R", or the type "reference to pointer to function of |
| 5671 | // (P1,...,Pn) returning R", or the type "reference to function |
| 5672 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5673 | // is also considered as a candidate function. Similarly, |
| 5674 | // surrogate call functions are added to the set of candidate |
| 5675 | // functions for each conversion function declared in an |
| 5676 | // accessible base class provided the function is not hidden |
| 5677 | // within T by another intervening declaration. |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5678 | // FIXME: Look in base classes for more conversion operators! |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5679 | const UnresolvedSet *Conversions |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5680 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5681 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 5682 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5683 | NamedDecl *D = *I; |
| 5684 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5685 | if (isa<UsingShadowDecl>(D)) |
| 5686 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5687 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5688 | // Skip over templated conversion functions; they aren't |
| 5689 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5690 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5691 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5692 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5693 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5694 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5695 | // Strip the reference type (if any) and then the pointer type (if |
| 5696 | // any) to get down to what might be a function type. |
| 5697 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 5698 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5699 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5701 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5702 | AddSurrogateCandidate(Conv, ActingContext, Proto, |
| 5703 | Object->getType(), Args, NumArgs, |
| 5704 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5707 | // Perform overload resolution. |
| 5708 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5709 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5710 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5711 | // Overload resolution succeeded; we'll build the appropriate call |
| 5712 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5713 | break; |
| 5714 | |
| 5715 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5716 | if (CandidateSet.empty()) |
| 5717 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 5718 | << Object->getType() << /*call*/ 1 |
| 5719 | << Object->getSourceRange(); |
| 5720 | else |
| 5721 | Diag(Object->getSourceRange().getBegin(), |
| 5722 | diag::err_ovl_no_viable_object_call) |
| 5723 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 5724 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5725 | break; |
| 5726 | |
| 5727 | case OR_Ambiguous: |
| 5728 | Diag(Object->getSourceRange().getBegin(), |
| 5729 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5730 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5731 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5732 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5733 | |
| 5734 | case OR_Deleted: |
| 5735 | Diag(Object->getSourceRange().getBegin(), |
| 5736 | diag::err_ovl_deleted_object_call) |
| 5737 | << Best->Function->isDeleted() |
| 5738 | << Object->getType() << Object->getSourceRange(); |
| 5739 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5740 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5741 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5742 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5743 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5744 | // We had an error; delete all of the subexpressions and return |
| 5745 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5746 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5747 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5748 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5749 | return true; |
| 5750 | } |
| 5751 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5752 | if (Best->Function == 0) { |
| 5753 | // Since there is no function declaration, this is one of the |
| 5754 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5755 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5756 | = cast<CXXConversionDecl>( |
| 5757 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 5758 | |
| 5759 | // We selected one of the surrogate functions that converts the |
| 5760 | // object parameter to a function pointer. Perform the conversion |
| 5761 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5762 | |
| 5763 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5764 | // and then call it. |
Eli Friedman | c8c771e | 2009-12-09 04:52:43 +0000 | [diff] [blame] | 5765 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5766 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5767 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5768 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 5769 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 5773 | // that calls this method, using Object for the implicit object |
| 5774 | // parameter and passing along the remaining arguments. |
| 5775 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5776 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5777 | |
| 5778 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5779 | unsigned NumArgsToCheck = NumArgs; |
| 5780 | |
| 5781 | // Build the full argument list for the method call (the |
| 5782 | // implicit object parameter is placed at the beginning of the |
| 5783 | // list). |
| 5784 | Expr **MethodArgs; |
| 5785 | if (NumArgs < NumArgsInProto) { |
| 5786 | NumArgsToCheck = NumArgsInProto; |
| 5787 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 5788 | } else { |
| 5789 | MethodArgs = new Expr*[NumArgs + 1]; |
| 5790 | } |
| 5791 | MethodArgs[0] = Object; |
| 5792 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5793 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5794 | |
| 5795 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5796 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5797 | UsualUnaryConversions(NewFn); |
| 5798 | |
| 5799 | // Once we've built TheCall, all of the expressions are properly |
| 5800 | // owned. |
| 5801 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5802 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5803 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5804 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5805 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5806 | delete [] MethodArgs; |
| 5807 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 5808 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 5809 | Method)) |
| 5810 | return true; |
| 5811 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5812 | // We may have default arguments. If so, we need to allocate more |
| 5813 | // slots in the call for them. |
| 5814 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5815 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5816 | else if (NumArgs > NumArgsInProto) |
| 5817 | NumArgsToCheck = NumArgsInProto; |
| 5818 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5819 | bool IsError = false; |
| 5820 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5821 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5822 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5823 | TheCall->setArg(0, Object); |
| 5824 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5825 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5826 | // Check the argument types. |
| 5827 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5828 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5829 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5830 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5831 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5832 | // Pass the argument. |
| 5833 | QualType ProtoArgType = Proto->getArgType(i); |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5834 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, AA_Passing); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5835 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 5836 | OwningExprResult DefArg |
| 5837 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 5838 | if (DefArg.isInvalid()) { |
| 5839 | IsError = true; |
| 5840 | break; |
| 5841 | } |
| 5842 | |
| 5843 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5844 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5845 | |
| 5846 | TheCall->setArg(i + 1, Arg); |
| 5847 | } |
| 5848 | |
| 5849 | // If this is a variadic call, handle args passed through "...". |
| 5850 | if (Proto->isVariadic()) { |
| 5851 | // Promote the arguments (C99 6.5.2.2p7). |
| 5852 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 5853 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5854 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5855 | TheCall->setArg(i + 1, Arg); |
| 5856 | } |
| 5857 | } |
| 5858 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5859 | if (IsError) return true; |
| 5860 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5861 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5862 | return true; |
| 5863 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 5864 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5865 | } |
| 5866 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5867 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5868 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5869 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5870 | Sema::OwningExprResult |
| 5871 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 5872 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5873 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5874 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5875 | // C++ [over.ref]p1: |
| 5876 | // |
| 5877 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 5878 | // for a class object x of type T if T::operator->() exists and if |
| 5879 | // the operator is selected as the best match function by the |
| 5880 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5881 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5882 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5883 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5884 | |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 5885 | if (RequireCompleteType(Base->getLocStart(), Base->getType(), |
| 5886 | PDiag(diag::err_typecheck_incomplete_tag) |
| 5887 | << Base->getSourceRange())) |
| 5888 | return ExprError(); |
| 5889 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5890 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5891 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 5892 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5893 | |
| 5894 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5895 | Oper != OperEnd; ++Oper) { |
| 5896 | NamedDecl *D = *Oper; |
| 5897 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5898 | if (isa<UsingShadowDecl>(D)) |
| 5899 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5900 | |
| 5901 | AddMethodCandidate(cast<CXXMethodDecl>(D), ActingContext, |
| 5902 | Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5903 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5904 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5905 | |
| 5906 | // Perform overload resolution. |
| 5907 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5908 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5909 | case OR_Success: |
| 5910 | // Overload resolution succeeded; we'll build the call below. |
| 5911 | break; |
| 5912 | |
| 5913 | case OR_No_Viable_Function: |
| 5914 | if (CandidateSet.empty()) |
| 5915 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5916 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5917 | else |
| 5918 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5919 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5920 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5921 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5922 | |
| 5923 | case OR_Ambiguous: |
| 5924 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5925 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5926 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5927 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5928 | |
| 5929 | case OR_Deleted: |
| 5930 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5931 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5932 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5933 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5934 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | // Convert the object parameter. |
| 5938 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5939 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5940 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5941 | |
| 5942 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5943 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5944 | |
| 5945 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5946 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 5947 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5948 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5949 | |
| 5950 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 5951 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5952 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 5953 | &Base, 1, ResultTy, OpLoc)); |
| 5954 | |
| 5955 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 5956 | Method)) |
| 5957 | return ExprError(); |
| 5958 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5961 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 5962 | /// a C++ overloaded function (possibly with some parentheses and |
| 5963 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 5964 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5965 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 5966 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5967 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5968 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 5969 | if (SubExpr == PE->getSubExpr()) |
| 5970 | return PE->Retain(); |
| 5971 | |
| 5972 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 5973 | } |
| 5974 | |
| 5975 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 5976 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5977 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5978 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5979 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5980 | if (SubExpr == ICE->getSubExpr()) |
| 5981 | return ICE->Retain(); |
| 5982 | |
| 5983 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 5984 | ICE->getCastKind(), |
| 5985 | SubExpr, |
| 5986 | ICE->isLvalueCast()); |
| 5987 | } |
| 5988 | |
| 5989 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5990 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5991 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5992 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5993 | if (Method->isStatic()) { |
| 5994 | // Do nothing: static member functions aren't any different |
| 5995 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5996 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5997 | // Fix the sub expression, which really has to be an |
| 5998 | // UnresolvedLookupExpr holding an overloaded member function |
| 5999 | // or template. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6000 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6001 | if (SubExpr == UnOp->getSubExpr()) |
| 6002 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6003 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6004 | assert(isa<DeclRefExpr>(SubExpr) |
| 6005 | && "fixed to something other than a decl ref"); |
| 6006 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6007 | && "fixed to a member ref with no nested name qualifier"); |
| 6008 | |
| 6009 | // We have taken the address of a pointer to member |
| 6010 | // function. Perform the computation here so that we get the |
| 6011 | // appropriate pointer to member type. |
| 6012 | QualType ClassType |
| 6013 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6014 | QualType MemPtrType |
| 6015 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6016 | |
| 6017 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6018 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6019 | } |
| 6020 | } |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6021 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6022 | if (SubExpr == UnOp->getSubExpr()) |
| 6023 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6024 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6025 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6026 | Context.getPointerType(SubExpr->getType()), |
| 6027 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6028 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6029 | |
| 6030 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6031 | // FIXME: avoid copy. |
| 6032 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6033 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6034 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6035 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6038 | return DeclRefExpr::Create(Context, |
| 6039 | ULE->getQualifier(), |
| 6040 | ULE->getQualifierRange(), |
| 6041 | Fn, |
| 6042 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6043 | Fn->getType(), |
| 6044 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6045 | } |
| 6046 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6047 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6048 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6049 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6050 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6051 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6052 | TemplateArgs = &TemplateArgsBuffer; |
| 6053 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6054 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6055 | Expr *Base; |
| 6056 | |
| 6057 | // If we're filling in |
| 6058 | if (MemExpr->isImplicitAccess()) { |
| 6059 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6060 | return DeclRefExpr::Create(Context, |
| 6061 | MemExpr->getQualifier(), |
| 6062 | MemExpr->getQualifierRange(), |
| 6063 | Fn, |
| 6064 | MemExpr->getMemberLoc(), |
| 6065 | Fn->getType(), |
| 6066 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame^] | 6067 | } else { |
| 6068 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6069 | if (MemExpr->getQualifier()) |
| 6070 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6071 | Base = new (Context) CXXThisExpr(Loc, |
| 6072 | MemExpr->getBaseType(), |
| 6073 | /*isImplicit=*/true); |
| 6074 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6075 | } else |
| 6076 | Base = MemExpr->getBase()->Retain(); |
| 6077 | |
| 6078 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6079 | MemExpr->isArrow(), |
| 6080 | MemExpr->getQualifier(), |
| 6081 | MemExpr->getQualifierRange(), |
| 6082 | Fn, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6083 | MemExpr->getMemberLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6084 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6085 | Fn->getType()); |
| 6086 | } |
| 6087 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6088 | assert(false && "Invalid reference to overloaded function"); |
| 6089 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6092 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
| 6093 | FunctionDecl *Fn) { |
| 6094 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Fn)); |
| 6095 | } |
| 6096 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6097 | } // end namespace clang |