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 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
| 25 | #include "clang/Sema/Initialization.h" |
| 26 | #include "clang/Sema/Lookup.h" |
| 27 | #include "clang/Sema/SemaInternal.h" |
| 28 | #include "clang/Sema/Template.h" |
| 29 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | |
| 36 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 37 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 38 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 39 | /// A convenience routine for creating a decayed reference to a |
| 40 | /// function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 41 | static ExprResult |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 42 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 43 | SourceLocation Loc = SourceLocation(), |
| 44 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 45 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 46 | VK_LValue, Loc, LocInfo); |
| 47 | if (HadMultipleCandidates) |
| 48 | DRE->setHadMultipleCandidates(true); |
| 49 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 50 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 51 | if (E.isInvalid()) |
| 52 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 53 | return E; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 56 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 57 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 58 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 59 | bool CStyle, |
| 60 | bool AllowObjCWritebackConversion); |
Sam Panzer | d012586 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 61 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 62 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 63 | QualType &ToType, |
| 64 | bool InOverloadResolution, |
| 65 | StandardConversionSequence &SCS, |
| 66 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 67 | static OverloadingResult |
| 68 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 69 | UserDefinedConversionSequence& User, |
| 70 | OverloadCandidateSet& Conversions, |
| 71 | bool AllowExplicit); |
| 72 | |
| 73 | |
| 74 | static ImplicitConversionSequence::CompareKind |
| 75 | CompareStandardConversionSequences(Sema &S, |
| 76 | const StandardConversionSequence& SCS1, |
| 77 | const StandardConversionSequence& SCS2); |
| 78 | |
| 79 | static ImplicitConversionSequence::CompareKind |
| 80 | CompareQualificationConversions(Sema &S, |
| 81 | const StandardConversionSequence& SCS1, |
| 82 | const StandardConversionSequence& SCS2); |
| 83 | |
| 84 | static ImplicitConversionSequence::CompareKind |
| 85 | CompareDerivedToBaseConversions(Sema &S, |
| 86 | const StandardConversionSequence& SCS1, |
| 87 | const StandardConversionSequence& SCS2); |
| 88 | |
| 89 | |
| 90 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 91 | /// GetConversionCategory - Retrieve the implicit conversion |
| 92 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 94 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 95 | static const ImplicitConversionCategory |
| 96 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 97 | ICC_Identity, |
| 98 | ICC_Lvalue_Transformation, |
| 99 | ICC_Lvalue_Transformation, |
| 100 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 101 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 102 | ICC_Qualification_Adjustment, |
| 103 | ICC_Promotion, |
| 104 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 105 | ICC_Promotion, |
| 106 | ICC_Conversion, |
| 107 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | ICC_Conversion, |
| 109 | ICC_Conversion, |
| 110 | ICC_Conversion, |
| 111 | ICC_Conversion, |
| 112 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 113 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 114 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 115 | ICC_Conversion, |
| 116 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 117 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 118 | ICC_Conversion |
| 119 | }; |
| 120 | return Category[(int)Kind]; |
| 121 | } |
| 122 | |
| 123 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 124 | /// corresponding to the given implicit conversion kind. |
| 125 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 126 | static const ImplicitConversionRank |
| 127 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 128 | ICR_Exact_Match, |
| 129 | ICR_Exact_Match, |
| 130 | ICR_Exact_Match, |
| 131 | ICR_Exact_Match, |
| 132 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 133 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 134 | ICR_Promotion, |
| 135 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 136 | ICR_Promotion, |
| 137 | ICR_Conversion, |
| 138 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 139 | ICR_Conversion, |
| 140 | ICR_Conversion, |
| 141 | ICR_Conversion, |
| 142 | ICR_Conversion, |
| 143 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 144 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 145 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 146 | ICR_Conversion, |
| 147 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 148 | ICR_Complex_Real_Conversion, |
| 149 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 150 | ICR_Conversion, |
| 151 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 152 | }; |
| 153 | return Rank[(int)Kind]; |
| 154 | } |
| 155 | |
| 156 | /// GetImplicitConversionName - Return the name of this kind of |
| 157 | /// implicit conversion. |
| 158 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 159 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 160 | "No conversion", |
| 161 | "Lvalue-to-rvalue", |
| 162 | "Array-to-pointer", |
| 163 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 164 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 165 | "Qualification", |
| 166 | "Integral promotion", |
| 167 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 168 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 169 | "Integral conversion", |
| 170 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 171 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 172 | "Floating-integral conversion", |
| 173 | "Pointer conversion", |
| 174 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 175 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 176 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 177 | "Derived-to-base conversion", |
| 178 | "Vector conversion", |
| 179 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 180 | "Complex-real conversion", |
| 181 | "Block Pointer conversion", |
| 182 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 183 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 184 | }; |
| 185 | return Name[Kind]; |
| 186 | } |
| 187 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 188 | /// StandardConversionSequence - Set the standard conversion |
| 189 | /// sequence to the identity conversion. |
| 190 | void StandardConversionSequence::setAsIdentityConversion() { |
| 191 | First = ICK_Identity; |
| 192 | Second = ICK_Identity; |
| 193 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 194 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 195 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 196 | ReferenceBinding = false; |
| 197 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 198 | IsLvalueReference = true; |
| 199 | BindsToFunctionLvalue = false; |
| 200 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 201 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 202 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 203 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 206 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 207 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 208 | /// implicit conversions. |
| 209 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 210 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 211 | if (GetConversionRank(First) > Rank) |
| 212 | Rank = GetConversionRank(First); |
| 213 | if (GetConversionRank(Second) > Rank) |
| 214 | Rank = GetConversionRank(Second); |
| 215 | if (GetConversionRank(Third) > Rank) |
| 216 | Rank = GetConversionRank(Third); |
| 217 | return Rank; |
| 218 | } |
| 219 | |
| 220 | /// isPointerConversionToBool - Determines whether this conversion is |
| 221 | /// 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] | 222 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 223 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 225 | // Note that FromType has not necessarily been transformed by the |
| 226 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 227 | // check for their presence as well as checking whether FromType is |
| 228 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 229 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 230 | (getFromType()->isPointerType() || |
| 231 | getFromType()->isObjCObjectPointerType() || |
| 232 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 233 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 234 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 235 | return true; |
| 236 | |
| 237 | return false; |
| 238 | } |
| 239 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 240 | /// isPointerConversionToVoidPointer - Determines whether this |
| 241 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 242 | /// used as part of the ranking of standard conversion sequences (C++ |
| 243 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 245 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 247 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 248 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 249 | |
| 250 | // Note that FromType has not necessarily been transformed by the |
| 251 | // array-to-pointer implicit conversion, so check for its presence |
| 252 | // and redo the conversion to get a pointer. |
| 253 | if (First == ICK_Array_To_Pointer) |
| 254 | FromType = Context.getArrayDecayedType(FromType); |
| 255 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 256 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 257 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 258 | return ToPtrType->getPointeeType()->isVoidType(); |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 263 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 264 | /// or after one in an implicit conversion. |
| 265 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 266 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 267 | switch (ICE->getCastKind()) { |
| 268 | case CK_NoOp: |
| 269 | case CK_IntegralCast: |
| 270 | case CK_IntegralToBoolean: |
| 271 | case CK_IntegralToFloating: |
| 272 | case CK_FloatingToIntegral: |
| 273 | case CK_FloatingToBoolean: |
| 274 | case CK_FloatingCast: |
| 275 | Converted = ICE->getSubExpr(); |
| 276 | continue; |
| 277 | |
| 278 | default: |
| 279 | return Converted; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return Converted; |
| 284 | } |
| 285 | |
| 286 | /// Check if this standard conversion sequence represents a narrowing |
| 287 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 288 | /// |
| 289 | /// \param Ctx The AST context. |
| 290 | /// \param Converted The result of applying this standard conversion sequence. |
| 291 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 292 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 293 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 294 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 295 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 296 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 297 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 298 | APValue &ConstantValue, |
| 299 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 300 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 301 | |
| 302 | // C++11 [dcl.init.list]p7: |
| 303 | // A narrowing conversion is an implicit conversion ... |
| 304 | QualType FromType = getToType(0); |
| 305 | QualType ToType = getToType(1); |
| 306 | switch (Second) { |
| 307 | // -- from a floating-point type to an integer type, or |
| 308 | // |
| 309 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 310 | // type, except where the source is a constant expression and the actual |
| 311 | // value after conversion will fit into the target type and will produce |
| 312 | // the original value when converted back to the original type, or |
| 313 | case ICK_Floating_Integral: |
| 314 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 315 | return NK_Type_Narrowing; |
| 316 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 317 | llvm::APSInt IntConstantValue; |
| 318 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 319 | if (Initializer && |
| 320 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 321 | // Convert the integer to the floating type. |
| 322 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 323 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 324 | llvm::APFloat::rmNearestTiesToEven); |
| 325 | // And back. |
| 326 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 327 | bool ignored; |
| 328 | Result.convertToInteger(ConvertedValue, |
| 329 | llvm::APFloat::rmTowardZero, &ignored); |
| 330 | // If the resulting value is different, this was a narrowing conversion. |
| 331 | if (IntConstantValue != ConvertedValue) { |
| 332 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 333 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 334 | return NK_Constant_Narrowing; |
| 335 | } |
| 336 | } else { |
| 337 | // Variables are always narrowings. |
| 338 | return NK_Variable_Narrowing; |
| 339 | } |
| 340 | } |
| 341 | return NK_Not_Narrowing; |
| 342 | |
| 343 | // -- from long double to double or float, or from double to float, except |
| 344 | // where the source is a constant expression and the actual value after |
| 345 | // conversion is within the range of values that can be represented (even |
| 346 | // if it cannot be represented exactly), or |
| 347 | case ICK_Floating_Conversion: |
| 348 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 349 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 350 | // FromType is larger than ToType. |
| 351 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 352 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 353 | // Constant! |
| 354 | assert(ConstantValue.isFloat()); |
| 355 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 356 | // Convert the source value into the target type. |
| 357 | bool ignored; |
| 358 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 359 | Ctx.getFloatTypeSemantics(ToType), |
| 360 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 361 | // If there was no overflow, the source value is within the range of |
| 362 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 363 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 364 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 365 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 366 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 367 | } else { |
| 368 | return NK_Variable_Narrowing; |
| 369 | } |
| 370 | } |
| 371 | return NK_Not_Narrowing; |
| 372 | |
| 373 | // -- from an integer type or unscoped enumeration type to an integer type |
| 374 | // that cannot represent all the values of the original type, except where |
| 375 | // the source is a constant expression and the actual value after |
| 376 | // conversion will fit into the target type and will produce the original |
| 377 | // value when converted back to the original type. |
| 378 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 379 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 380 | // Boolean conversions can be from pointers and pointers to members |
| 381 | // [conv.bool], and those aren't considered narrowing conversions. |
| 382 | return NK_Not_Narrowing; |
| 383 | } // Otherwise, fall through to the integral case. |
| 384 | case ICK_Integral_Conversion: { |
| 385 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 386 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 387 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 388 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 389 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 390 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 391 | |
| 392 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 393 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 394 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 395 | // Not all values of FromType can be represented in ToType. |
| 396 | llvm::APSInt InitializerValue; |
| 397 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 398 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 399 | // Such conversions on variables are always narrowing. |
| 400 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 401 | } |
| 402 | bool Narrowing = false; |
| 403 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 404 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 405 | // narrowing. |
| 406 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 407 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 408 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 409 | // Add a bit to the InitializerValue so we don't have to worry about |
| 410 | // signed vs. unsigned comparisons. |
| 411 | InitializerValue = InitializerValue.extend( |
| 412 | InitializerValue.getBitWidth() + 1); |
| 413 | // Convert the initializer to and from the target width and signed-ness. |
| 414 | llvm::APSInt ConvertedValue = InitializerValue; |
| 415 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 416 | ConvertedValue.setIsSigned(ToSigned); |
| 417 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 418 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 419 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 420 | if (ConvertedValue != InitializerValue) |
| 421 | Narrowing = true; |
| 422 | } |
| 423 | if (Narrowing) { |
| 424 | ConstantType = Initializer->getType(); |
| 425 | ConstantValue = APValue(InitializerValue); |
| 426 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | return NK_Not_Narrowing; |
| 430 | } |
| 431 | |
| 432 | default: |
| 433 | // Other kinds of conversions are not narrowings. |
| 434 | return NK_Not_Narrowing; |
| 435 | } |
| 436 | } |
| 437 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 438 | /// DebugPrint - Print this standard conversion sequence to standard |
| 439 | /// error. Useful for debugging overloading issues. |
| 440 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 441 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 442 | bool PrintedSomething = false; |
| 443 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 444 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 445 | PrintedSomething = true; |
| 446 | } |
| 447 | |
| 448 | if (Second != ICK_Identity) { |
| 449 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 450 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 451 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 452 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 453 | |
| 454 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 455 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 456 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 457 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 458 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 459 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 460 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 461 | PrintedSomething = true; |
| 462 | } |
| 463 | |
| 464 | if (Third != ICK_Identity) { |
| 465 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 466 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 467 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 468 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 469 | PrintedSomething = true; |
| 470 | } |
| 471 | |
| 472 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 473 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 478 | /// error. Useful for debugging overloading issues. |
| 479 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 480 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 481 | if (Before.First || Before.Second || Before.Third) { |
| 482 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 483 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 484 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 485 | if (ConversionFunction) |
| 486 | OS << '\'' << *ConversionFunction << '\''; |
| 487 | else |
| 488 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 489 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 490 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | After.DebugPrint(); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 496 | /// error. Useful for debugging overloading issues. |
| 497 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 498 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 499 | switch (ConversionKind) { |
| 500 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 501 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 502 | Standard.DebugPrint(); |
| 503 | break; |
| 504 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 505 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | UserDefined.DebugPrint(); |
| 507 | break; |
| 508 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 509 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 511 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 512 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 513 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 514 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 515 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | break; |
| 517 | } |
| 518 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 519 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 520 | } |
| 521 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 522 | void AmbiguousConversionSequence::construct() { |
| 523 | new (&conversions()) ConversionSet(); |
| 524 | } |
| 525 | |
| 526 | void AmbiguousConversionSequence::destruct() { |
| 527 | conversions().~ConversionSet(); |
| 528 | } |
| 529 | |
| 530 | void |
| 531 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 532 | FromTypePtr = O.FromTypePtr; |
| 533 | ToTypePtr = O.ToTypePtr; |
| 534 | new (&conversions()) ConversionSet(O.conversions()); |
| 535 | } |
| 536 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 537 | namespace { |
| 538 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 539 | // template parameter and template argument information. |
| 540 | struct DFIParamWithArguments { |
| 541 | TemplateParameter Param; |
| 542 | TemplateArgument FirstArg; |
| 543 | TemplateArgument SecondArg; |
| 544 | }; |
| 545 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 547 | /// \brief Convert from Sema's representation of template deduction information |
| 548 | /// to the form used in overload-candidate information. |
| 549 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 550 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 551 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 552 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 553 | OverloadCandidate::DeductionFailureInfo Result; |
| 554 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 555 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 556 | Result.Data = 0; |
| 557 | switch (TDK) { |
| 558 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 559 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 560 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 561 | case Sema::TDK_TooManyArguments: |
| 562 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 563 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 565 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 566 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 567 | Result.Data = Info.Param.getOpaqueValue(); |
| 568 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 570 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 571 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 572 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 573 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 574 | Saved->Param = Info.Param; |
| 575 | Saved->FirstArg = Info.FirstArg; |
| 576 | Saved->SecondArg = Info.SecondArg; |
| 577 | Result.Data = Saved; |
| 578 | break; |
| 579 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 581 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 582 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 583 | if (Info.hasSFINAEDiagnostic()) { |
| 584 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 585 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 586 | Info.takeSFINAEDiagnostic(*Diag); |
| 587 | Result.HasDiagnostic = true; |
| 588 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 589 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 591 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 592 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 593 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 594 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 596 | return Result; |
| 597 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 599 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 600 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 601 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 602 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 603 | case Sema::TDK_InstantiationDepth: |
| 604 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 605 | case Sema::TDK_TooManyArguments: |
| 606 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 607 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 608 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 610 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 611 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 612 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 613 | Data = 0; |
| 614 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 615 | |
| 616 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 617 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 618 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 619 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 620 | Diag->~PartialDiagnosticAt(); |
| 621 | HasDiagnostic = false; |
| 622 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 623 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 625 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 626 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 627 | case Sema::TDK_FailedOverloadResolution: |
| 628 | break; |
| 629 | } |
| 630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 631 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 632 | PartialDiagnosticAt * |
| 633 | OverloadCandidate::DeductionFailureInfo::getSFINAEDiagnostic() { |
| 634 | if (HasDiagnostic) |
| 635 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 636 | return 0; |
| 637 | } |
| 638 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 639 | TemplateParameter |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 640 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 641 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 642 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 643 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 644 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 645 | case Sema::TDK_TooManyArguments: |
| 646 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 647 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 648 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 650 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 651 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 652 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 653 | |
| 654 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 655 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 656 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 658 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 659 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 660 | case Sema::TDK_FailedOverloadResolution: |
| 661 | break; |
| 662 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 664 | return TemplateParameter(); |
| 665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 666 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 667 | TemplateArgumentList * |
| 668 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 669 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 670 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 671 | case Sema::TDK_Invalid: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 672 | case Sema::TDK_InstantiationDepth: |
| 673 | case Sema::TDK_TooManyArguments: |
| 674 | case Sema::TDK_TooFewArguments: |
| 675 | case Sema::TDK_Incomplete: |
| 676 | case Sema::TDK_InvalidExplicitArguments: |
| 677 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 678 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 679 | return 0; |
| 680 | |
| 681 | case Sema::TDK_SubstitutionFailure: |
| 682 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 684 | // Unhandled |
| 685 | case Sema::TDK_NonDeducedMismatch: |
| 686 | case Sema::TDK_FailedOverloadResolution: |
| 687 | break; |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 693 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 694 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 695 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 696 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 697 | case Sema::TDK_InstantiationDepth: |
| 698 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 699 | case Sema::TDK_TooManyArguments: |
| 700 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 701 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 702 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 703 | return 0; |
| 704 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 705 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 706 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 707 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 708 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 709 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 710 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 711 | case Sema::TDK_FailedOverloadResolution: |
| 712 | break; |
| 713 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 715 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 717 | |
| 718 | const TemplateArgument * |
| 719 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 720 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 721 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 722 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 723 | case Sema::TDK_InstantiationDepth: |
| 724 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 725 | case Sema::TDK_TooManyArguments: |
| 726 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 727 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 728 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 729 | return 0; |
| 730 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 731 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 732 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 733 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 734 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 735 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 736 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 737 | case Sema::TDK_FailedOverloadResolution: |
| 738 | break; |
| 739 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 740 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 744 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 745 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 746 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 747 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 748 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 749 | i->DeductionFailure.Destroy(); |
| 750 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | void OverloadCandidateSet::clear() { |
| 754 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 755 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 756 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 757 | Functions.clear(); |
| 758 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 760 | namespace { |
| 761 | class UnbridgedCastsSet { |
| 762 | struct Entry { |
| 763 | Expr **Addr; |
| 764 | Expr *Saved; |
| 765 | }; |
| 766 | SmallVector<Entry, 2> Entries; |
| 767 | |
| 768 | public: |
| 769 | void save(Sema &S, Expr *&E) { |
| 770 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 771 | Entry entry = { &E, E }; |
| 772 | Entries.push_back(entry); |
| 773 | E = S.stripARCUnbridgedCast(E); |
| 774 | } |
| 775 | |
| 776 | void restore() { |
| 777 | for (SmallVectorImpl<Entry>::iterator |
| 778 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 779 | *i->Addr = i->Saved; |
| 780 | } |
| 781 | }; |
| 782 | } |
| 783 | |
| 784 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 785 | /// preprocessing on the given expression. |
| 786 | /// |
| 787 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 788 | /// without this, they will be immediately diagnosed as errors |
| 789 | /// |
| 790 | /// Return true on unrecoverable error. |
| 791 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 792 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 793 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 794 | // We can't handle overloaded expressions here because overload |
| 795 | // resolution might reasonably tweak them. |
| 796 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 797 | |
| 798 | // If the context potentially accepts unbridged ARC casts, strip |
| 799 | // the unbridged cast and add it to the collection for later restoration. |
| 800 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 801 | unbridgedCasts) { |
| 802 | unbridgedCasts->save(S, E); |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | // Go ahead and check everything else. |
| 807 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 808 | if (result.isInvalid()) |
| 809 | return true; |
| 810 | |
| 811 | E = result.take(); |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | // Nothing to do. |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 820 | /// placeholders. |
| 821 | static bool checkArgPlaceholdersForOverload(Sema &S, Expr **args, |
| 822 | unsigned numArgs, |
| 823 | UnbridgedCastsSet &unbridged) { |
| 824 | for (unsigned i = 0; i != numArgs; ++i) |
| 825 | if (checkPlaceholderForOverload(S, args[i], &unbridged)) |
| 826 | return true; |
| 827 | |
| 828 | return false; |
| 829 | } |
| 830 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 831 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 832 | // overload of the declarations in Old. This routine returns false if |
| 833 | // New and Old cannot be overloaded, e.g., if New has the same |
| 834 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 835 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 836 | // it does return false, MatchedDecl will point to the decl that New |
| 837 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 838 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 839 | // |
| 840 | // Example: Given the following input: |
| 841 | // |
| 842 | // void f(int, float); // #1 |
| 843 | // void f(int, int); // #2 |
| 844 | // int f(int, int); // #3 |
| 845 | // |
| 846 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 848 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 849 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 850 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 851 | // (since they have different signatures), so this routine returns |
| 852 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 853 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 854 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 855 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 856 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 857 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 858 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 859 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 860 | // |
| 861 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 862 | // into a class by a using declaration. The rules for whether to hide |
| 863 | // shadow declarations ignore some properties which otherwise figure |
| 864 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 865 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 866 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 867 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 868 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 869 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 870 | NamedDecl *OldD = *I; |
| 871 | |
| 872 | bool OldIsUsingDecl = false; |
| 873 | if (isa<UsingShadowDecl>(OldD)) { |
| 874 | OldIsUsingDecl = true; |
| 875 | |
| 876 | // We can always introduce two using declarations into the same |
| 877 | // context, even if they have identical signatures. |
| 878 | if (NewIsUsingDecl) continue; |
| 879 | |
| 880 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 881 | } |
| 882 | |
| 883 | // If either declaration was introduced by a using declaration, |
| 884 | // we'll need to use slightly different rules for matching. |
| 885 | // Essentially, these rules are the normal rules, except that |
| 886 | // function templates hide function templates with different |
| 887 | // return types or template parameter lists. |
| 888 | bool UseMemberUsingDeclRules = |
| 889 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 890 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 891 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 892 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 893 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 894 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 895 | continue; |
| 896 | } |
| 897 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 898 | Match = *I; |
| 899 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 900 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 901 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 902 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 903 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 904 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 905 | continue; |
| 906 | } |
| 907 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 908 | Match = *I; |
| 909 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 910 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 911 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 912 | // We can overload with these, which can show up when doing |
| 913 | // redeclaration checks for UsingDecls. |
| 914 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 915 | } else if (isa<TagDecl>(OldD)) { |
| 916 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 917 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 918 | // Optimistically assume that an unresolved using decl will |
| 919 | // overload; if it doesn't, we'll have to diagnose during |
| 920 | // template instantiation. |
| 921 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 922 | // (C++ 13p1): |
| 923 | // Only function declarations can be overloaded; object and type |
| 924 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 925 | Match = *I; |
| 926 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 927 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 928 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 929 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 930 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 931 | } |
| 932 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 933 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 934 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 935 | // If both of the functions are extern "C", then they are not |
| 936 | // overloads. |
| 937 | if (Old->isExternC() && New->isExternC()) |
| 938 | return false; |
| 939 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 940 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 941 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 942 | |
| 943 | // C++ [temp.fct]p2: |
| 944 | // A function template can be overloaded with other function templates |
| 945 | // and with normal (non-template) functions. |
| 946 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 947 | return true; |
| 948 | |
| 949 | // Is the function New an overload of the function Old? |
| 950 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 951 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 952 | |
| 953 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 954 | // determine whether they are overloads. If we find any mismatch |
| 955 | // in the signature, they are overloads. |
| 956 | |
| 957 | // If either of these functions is a K&R-style function (no |
| 958 | // prototype), then we consider them to have matching signatures. |
| 959 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 960 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 961 | return false; |
| 962 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 963 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 964 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 965 | |
| 966 | // The signature of a function includes the types of its |
| 967 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 968 | // of the ellipsis; see C++ DR 357). |
| 969 | if (OldQType != NewQType && |
| 970 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 971 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 972 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 973 | return true; |
| 974 | |
| 975 | // C++ [temp.over.link]p4: |
| 976 | // The signature of a function template consists of its function |
| 977 | // signature, its return type and its template parameter list. The names |
| 978 | // of the template parameters are significant only for establishing the |
| 979 | // relationship between the template parameters and the rest of the |
| 980 | // signature. |
| 981 | // |
| 982 | // We check the return type and template parameter lists for function |
| 983 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 984 | // |
| 985 | // However, we don't consider either of these when deciding whether |
| 986 | // a member introduced by a shadow declaration is hidden. |
| 987 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 988 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 989 | OldTemplate->getTemplateParameters(), |
| 990 | false, TPL_TemplateMatch) || |
| 991 | OldType->getResultType() != NewType->getResultType())) |
| 992 | return true; |
| 993 | |
| 994 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 995 | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 996 | // |
| 997 | // As part of this, also check whether one of the member functions |
| 998 | // is static, in which case they are not overloads (C++ |
| 999 | // 13.1p2). While not part of the definition of the signature, |
| 1000 | // this check is important to determine whether these functions |
| 1001 | // can be overloaded. |
| 1002 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1003 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 1004 | if (OldMethod && NewMethod && |
| 1005 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1006 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1007 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 1008 | if (!UseUsingDeclRules && |
| 1009 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 1010 | (OldMethod->getRefQualifier() == RQ_None || |
| 1011 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1012 | // C++0x [over.load]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1013 | // - Member function declarations with the same name and the same |
| 1014 | // parameter-type-list as well as member function template |
| 1015 | // declarations with the same name, the same parameter-type-list, and |
| 1016 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1017 | // them, but not all, have a ref-qualifier (8.3.5). |
| 1018 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 1019 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 1020 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 1021 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1022 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1023 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1024 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1025 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1026 | // The signatures match; this is not an overload. |
| 1027 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1030 | /// \brief Checks availability of the function depending on the current |
| 1031 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1032 | /// |
| 1033 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1034 | /// an available function, false otherwise. |
| 1035 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1036 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1037 | } |
| 1038 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1039 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1040 | /// |
| 1041 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1042 | /// is not an option. See TryImplicitConversion for more information. |
| 1043 | static ImplicitConversionSequence |
| 1044 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1045 | bool SuppressUserConversions, |
| 1046 | bool AllowExplicit, |
| 1047 | bool InOverloadResolution, |
| 1048 | bool CStyle, |
| 1049 | bool AllowObjCWritebackConversion) { |
| 1050 | ImplicitConversionSequence ICS; |
| 1051 | |
| 1052 | if (SuppressUserConversions) { |
| 1053 | // We're not in the case above, so there is no conversion that |
| 1054 | // we can perform. |
| 1055 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1056 | return ICS; |
| 1057 | } |
| 1058 | |
| 1059 | // Attempt user-defined conversion. |
| 1060 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1061 | OverloadingResult UserDefResult |
| 1062 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1063 | AllowExplicit); |
| 1064 | |
| 1065 | if (UserDefResult == OR_Success) { |
| 1066 | ICS.setUserDefined(); |
| 1067 | // C++ [over.ics.user]p4: |
| 1068 | // A conversion of an expression of class type to the same class |
| 1069 | // type is given Exact Match rank, and a conversion of an |
| 1070 | // expression of class type to a base class of that type is |
| 1071 | // given Conversion rank, in spite of the fact that a copy |
| 1072 | // constructor (i.e., a user-defined conversion function) is |
| 1073 | // called for those cases. |
| 1074 | if (CXXConstructorDecl *Constructor |
| 1075 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1076 | QualType FromCanon |
| 1077 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1078 | QualType ToCanon |
| 1079 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1080 | if (Constructor->isCopyConstructor() && |
| 1081 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1082 | // Turn this into a "standard" conversion sequence, so that it |
| 1083 | // gets ranked with standard conversion sequences. |
| 1084 | ICS.setStandard(); |
| 1085 | ICS.Standard.setAsIdentityConversion(); |
| 1086 | ICS.Standard.setFromType(From->getType()); |
| 1087 | ICS.Standard.setAllToTypes(ToType); |
| 1088 | ICS.Standard.CopyConstructor = Constructor; |
| 1089 | if (ToCanon != FromCanon) |
| 1090 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | // C++ [over.best.ics]p4: |
| 1095 | // However, when considering the argument of a user-defined |
| 1096 | // conversion function that is a candidate by 13.3.1.3 when |
| 1097 | // invoked for the copying of the temporary in the second step |
| 1098 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1099 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1100 | // ellipsis conversion sequences are allowed. |
| 1101 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1102 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1103 | } |
| 1104 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1105 | ICS.setAmbiguous(); |
| 1106 | ICS.Ambiguous.setFromType(From->getType()); |
| 1107 | ICS.Ambiguous.setToType(ToType); |
| 1108 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1109 | Cand != Conversions.end(); ++Cand) |
| 1110 | if (Cand->Viable) |
| 1111 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1112 | } else { |
| 1113 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1114 | } |
| 1115 | |
| 1116 | return ICS; |
| 1117 | } |
| 1118 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1119 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1120 | /// from the given expression (Expr) to the given type (ToType). This |
| 1121 | /// function returns an implicit conversion sequence that can be used |
| 1122 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1123 | /// |
| 1124 | /// void f(float f); |
| 1125 | /// void g(int i) { f(i); } |
| 1126 | /// |
| 1127 | /// this routine would produce an implicit conversion sequence to |
| 1128 | /// describe the initialization of f from i, which will be a standard |
| 1129 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1130 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1131 | // |
| 1132 | /// Note that this routine only determines how the conversion can be |
| 1133 | /// performed; it does not actually perform the conversion. As such, |
| 1134 | /// it will not produce any diagnostics if no conversion is available, |
| 1135 | /// but will instead return an implicit conversion sequence of kind |
| 1136 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1137 | /// |
| 1138 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1139 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1140 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1141 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1142 | /// |
| 1143 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1144 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1145 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1146 | static ImplicitConversionSequence |
| 1147 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1148 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1149 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1150 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1151 | bool CStyle, |
| 1152 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1153 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1154 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1155 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1156 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1157 | return ICS; |
| 1158 | } |
| 1159 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1160 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1161 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1162 | return ICS; |
| 1163 | } |
| 1164 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1165 | // C++ [over.ics.user]p4: |
| 1166 | // A conversion of an expression of class type to the same class |
| 1167 | // type is given Exact Match rank, and a conversion of an |
| 1168 | // expression of class type to a base class of that type is |
| 1169 | // given Conversion rank, in spite of the fact that a copy/move |
| 1170 | // constructor (i.e., a user-defined conversion function) is |
| 1171 | // called for those cases. |
| 1172 | QualType FromType = From->getType(); |
| 1173 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1174 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1175 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1176 | ICS.setStandard(); |
| 1177 | ICS.Standard.setAsIdentityConversion(); |
| 1178 | ICS.Standard.setFromType(FromType); |
| 1179 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1181 | // We don't actually check at this point whether there is a valid |
| 1182 | // copy/move constructor, since overloading just assumes that it |
| 1183 | // exists. When we actually perform initialization, we'll find the |
| 1184 | // appropriate constructor to copy the returned object, if needed. |
| 1185 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1187 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1188 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1189 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1191 | return ICS; |
| 1192 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1193 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1194 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1195 | AllowExplicit, InOverloadResolution, CStyle, |
| 1196 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1199 | ImplicitConversionSequence |
| 1200 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1201 | bool SuppressUserConversions, |
| 1202 | bool AllowExplicit, |
| 1203 | bool InOverloadResolution, |
| 1204 | bool CStyle, |
| 1205 | bool AllowObjCWritebackConversion) { |
| 1206 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1207 | SuppressUserConversions, AllowExplicit, |
| 1208 | InOverloadResolution, CStyle, |
| 1209 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1212 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1213 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1214 | /// converted expression. Flavor is the kind of conversion we're |
| 1215 | /// performing, used in the error message. If @p AllowExplicit, |
| 1216 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1217 | ExprResult |
| 1218 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1219 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1220 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1221 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1224 | ExprResult |
| 1225 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1226 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1227 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1228 | if (checkPlaceholderForOverload(*this, From)) |
| 1229 | return ExprError(); |
| 1230 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1231 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1232 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1233 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1234 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1235 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1236 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1237 | /*SuppressUserConversions=*/false, |
| 1238 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1239 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1240 | /*CStyle=*/false, |
| 1241 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1242 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1243 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | |
| 1245 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1246 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1247 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1248 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1249 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1250 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1252 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1253 | // where F adds one of the following at most once: |
| 1254 | // - a pointer |
| 1255 | // - a member pointer |
| 1256 | // - a block pointer |
| 1257 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1258 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1259 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1260 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1261 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1262 | if (TyClass == Type::Pointer) { |
| 1263 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1264 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1265 | } else if (TyClass == Type::BlockPointer) { |
| 1266 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1267 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1268 | } else if (TyClass == Type::MemberPointer) { |
| 1269 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1270 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1271 | } else { |
| 1272 | return false; |
| 1273 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1274 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1275 | TyClass = CanTo->getTypeClass(); |
| 1276 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1277 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1278 | return false; |
| 1279 | } |
| 1280 | |
| 1281 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1282 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1283 | if (!EInfo.getNoReturn()) return false; |
| 1284 | |
| 1285 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1286 | assert(QualType(FromFn, 0).isCanonical()); |
| 1287 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1288 | |
| 1289 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1290 | return true; |
| 1291 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1292 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1293 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1294 | /// vector conversion. |
| 1295 | /// |
| 1296 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1297 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1299 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1300 | // We need at least one of these types to be a vector type to have a vector |
| 1301 | // conversion. |
| 1302 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1303 | return false; |
| 1304 | |
| 1305 | // Identical types require no conversions. |
| 1306 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1307 | return false; |
| 1308 | |
| 1309 | // There are no conversions between extended vector types, only identity. |
| 1310 | if (ToType->isExtVectorType()) { |
| 1311 | // There are no conversions between extended vector types other than the |
| 1312 | // identity conversion. |
| 1313 | if (FromType->isExtVectorType()) |
| 1314 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1316 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1317 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1318 | ICK = ICK_Vector_Splat; |
| 1319 | return true; |
| 1320 | } |
| 1321 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1322 | |
| 1323 | // We can perform the conversion between vector types in the following cases: |
| 1324 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1325 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1326 | // same size |
| 1327 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1328 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1329 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1330 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1331 | ICK = ICK_Vector_Conversion; |
| 1332 | return true; |
| 1333 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1334 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1335 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1336 | return false; |
| 1337 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1338 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1339 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1340 | bool InOverloadResolution, |
| 1341 | StandardConversionSequence &SCS, |
| 1342 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1344 | /// IsStandardConversion - Determines whether there is a standard |
| 1345 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1346 | /// expression From to the type ToType. Standard conversion sequences |
| 1347 | /// only consider non-class types; for conversions that involve class |
| 1348 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1349 | /// contain the standard conversion sequence required to perform this |
| 1350 | /// conversion and this routine will return true. Otherwise, this |
| 1351 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1352 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1353 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1354 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1355 | bool CStyle, |
| 1356 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1357 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1358 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1359 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1360 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1361 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1362 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1363 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1364 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1365 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1366 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1368 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1369 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1370 | return false; |
| 1371 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1375 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1376 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1377 | // (C++ 4p1). |
| 1378 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1379 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1380 | DeclAccessPair AccessPair; |
| 1381 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1382 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1383 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1384 | // We were able to resolve the address of the overloaded function, |
| 1385 | // so we can convert to the type of that function. |
| 1386 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1387 | |
| 1388 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1389 | // if the type matches (identity) or we are converting to bool |
| 1390 | if (!S.Context.hasSameUnqualifiedType( |
| 1391 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1392 | QualType resultTy; |
| 1393 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1394 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1395 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1396 | // otherwise, only a boolean conversion is standard |
| 1397 | if (!ToType->isBooleanType()) |
| 1398 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1399 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1400 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1401 | // Check if the "from" expression is taking the address of an overloaded |
| 1402 | // function and recompute the FromType accordingly. Take advantage of the |
| 1403 | // fact that non-static member functions *must* have such an address-of |
| 1404 | // expression. |
| 1405 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1406 | if (Method && !Method->isStatic()) { |
| 1407 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1408 | "Non-unary operator on non-static member address"); |
| 1409 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1410 | == UO_AddrOf && |
| 1411 | "Non-address-of operator on non-static member address"); |
| 1412 | const Type *ClassType |
| 1413 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1414 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1415 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1416 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1417 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1418 | "Non-address-of operator for overloaded function expression"); |
| 1419 | FromType = S.Context.getPointerType(FromType); |
| 1420 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1422 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1423 | assert(S.Context.hasSameType( |
| 1424 | FromType, |
| 1425 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1426 | } else { |
| 1427 | return false; |
| 1428 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1429 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1430 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1431 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1432 | // be converted to a prvalue. |
| 1433 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1434 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1435 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1436 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1437 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1438 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1439 | // C11 6.3.2.1p2: |
| 1440 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1441 | // of the type of the lvalue ... |
| 1442 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1443 | FromType = Atomic->getValueType(); |
| 1444 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1445 | // If T is a non-class type, the type of the rvalue is the |
| 1446 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1447 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1448 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1449 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1450 | } else if (FromType->isArrayType()) { |
| 1451 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1452 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1453 | |
| 1454 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1455 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1456 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1457 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1458 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1459 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1460 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1461 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1462 | |
| 1463 | // For the purpose of ranking in overload resolution |
| 1464 | // (13.3.3.1.1), this conversion is considered an |
| 1465 | // array-to-pointer conversion followed by a qualification |
| 1466 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1467 | SCS.Second = ICK_Identity; |
| 1468 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1469 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1470 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1471 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1472 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1473 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1474 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1475 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1476 | |
| 1477 | // An lvalue of function type T can be converted to an rvalue of |
| 1478 | // type "pointer to T." The result is a pointer to the |
| 1479 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1480 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1481 | } else { |
| 1482 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1483 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1484 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1485 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1486 | |
| 1487 | // The second conversion can be an integral promotion, floating |
| 1488 | // point promotion, integral conversion, floating point conversion, |
| 1489 | // floating-integral conversion, pointer conversion, |
| 1490 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1491 | // For overloading in C, this can also be a "compatible-type" |
| 1492 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1493 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1494 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1495 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1496 | // The unqualified versions of the types are the same: there's no |
| 1497 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1498 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1499 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1501 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1502 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1503 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1504 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1505 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1506 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1507 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1508 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1509 | SCS.Second = ICK_Complex_Promotion; |
| 1510 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1511 | } else if (ToType->isBooleanType() && |
| 1512 | (FromType->isArithmeticType() || |
| 1513 | FromType->isAnyPointerType() || |
| 1514 | FromType->isBlockPointerType() || |
| 1515 | FromType->isMemberPointerType() || |
| 1516 | FromType->isNullPtrType())) { |
| 1517 | // Boolean conversions (C++ 4.12). |
| 1518 | SCS.Second = ICK_Boolean_Conversion; |
| 1519 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1520 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1521 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1522 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1523 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1524 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1525 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1526 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1527 | SCS.Second = ICK_Complex_Conversion; |
| 1528 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1529 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1530 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1531 | // Complex-real conversions (C99 6.3.1.7) |
| 1532 | SCS.Second = ICK_Complex_Real; |
| 1533 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1534 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1535 | // Floating point conversions (C++ 4.8). |
| 1536 | SCS.Second = ICK_Floating_Conversion; |
| 1537 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1538 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1539 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1540 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1541 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1542 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1543 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1544 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1545 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1546 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1547 | } else if (AllowObjCWritebackConversion && |
| 1548 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1549 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1550 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1551 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1552 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1553 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1554 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1555 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1556 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1557 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1558 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1559 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1560 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1561 | SCS.Second = SecondICK; |
| 1562 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1563 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1564 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1565 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1566 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1567 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1568 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1569 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1570 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1571 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1572 | InOverloadResolution, |
| 1573 | SCS, CStyle)) { |
| 1574 | SCS.Second = ICK_TransparentUnionConversion; |
| 1575 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1576 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1577 | CStyle)) { |
| 1578 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1579 | // appropriately. |
| 1580 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1581 | } else { |
| 1582 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1583 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1584 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1585 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1587 | QualType CanonFrom; |
| 1588 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1589 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1590 | bool ObjCLifetimeConversion; |
| 1591 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1592 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1593 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1594 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1595 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1596 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1597 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1598 | } else { |
| 1599 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1600 | SCS.Third = ICK_Identity; |
| 1601 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1603 | // [...] Any difference in top-level cv-qualification is |
| 1604 | // subsumed by the initialization itself and does not constitute |
| 1605 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1606 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1607 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1608 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1609 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1610 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1611 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr() |
| 1612 | || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1613 | FromType = ToType; |
| 1614 | CanonFrom = CanonTo; |
| 1615 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1616 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1617 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1618 | |
| 1619 | // If we have not converted the argument type to the parameter type, |
| 1620 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1621 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1622 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1624 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1625 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1626 | |
| 1627 | static bool |
| 1628 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1629 | QualType &ToType, |
| 1630 | bool InOverloadResolution, |
| 1631 | StandardConversionSequence &SCS, |
| 1632 | bool CStyle) { |
| 1633 | |
| 1634 | const RecordType *UT = ToType->getAsUnionType(); |
| 1635 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1636 | return false; |
| 1637 | // The field to initialize within the transparent union. |
| 1638 | RecordDecl *UD = UT->getDecl(); |
| 1639 | // It's compatible if the expression matches any of the fields. |
| 1640 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1641 | itend = UD->field_end(); |
| 1642 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1643 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1644 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1645 | ToType = it->getType(); |
| 1646 | return true; |
| 1647 | } |
| 1648 | } |
| 1649 | return false; |
| 1650 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1651 | |
| 1652 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1653 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1654 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1655 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1657 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1658 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1659 | if (!To) { |
| 1660 | return false; |
| 1661 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1662 | |
| 1663 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1664 | // unsigned short int can be converted to an rvalue of type int if |
| 1665 | // int can represent all the values of the source type; otherwise, |
| 1666 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1667 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1668 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1669 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1670 | if (// We can promote any signed, promotable integer type to an int |
| 1671 | (FromType->isSignedIntegerType() || |
| 1672 | // We can promote any unsigned integer type whose size is |
| 1673 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1675 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1676 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1679 | return To->getKind() == BuiltinType::UInt; |
| 1680 | } |
| 1681 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1682 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1683 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1684 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1685 | // following types that can represent all the values of the enumeration |
| 1686 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1687 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1688 | // long long int. If none of the types in that list can represent all the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1689 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1690 | // type can be converted to an rvalue a prvalue of the extended integer type |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1691 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1692 | // long in which all the values of the enumeration can be represented. If |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1693 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1694 | // C++11 [conv.prom]p4: |
| 1695 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1696 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1697 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1698 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1699 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1700 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1701 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1702 | // provided for a scoped enumeration. |
| 1703 | if (FromEnumType->getDecl()->isScoped()) |
| 1704 | return false; |
| 1705 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1706 | // We can perform an integral promotion to the underlying type of the enum, |
| 1707 | // even if that's not the promoted type. |
| 1708 | if (FromEnumType->getDecl()->isFixed()) { |
| 1709 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1710 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1711 | IsIntegralPromotion(From, Underlying, ToType); |
| 1712 | } |
| 1713 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1714 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1715 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1716 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1717 | return Context.hasSameUnqualifiedType(ToType, |
| 1718 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1719 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1720 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1721 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1722 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1723 | // to an rvalue a prvalue of the first of the following types that can |
| 1724 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1725 | // long int, unsigned long int, long long int, or unsigned long long int. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1726 | // If none of the types in that list can represent all the values of its |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1727 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1728 | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1729 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1730 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1731 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1732 | // Determine whether the type we're converting from is signed or |
| 1733 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1734 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1735 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1736 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1737 | // The types we'll try to promote to, in the appropriate |
| 1738 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | QualType PromoteTypes[6] = { |
| 1740 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1741 | Context.LongTy, Context.UnsignedLongTy , |
| 1742 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1743 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1744 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1745 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1746 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1748 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1749 | // We found the type that we can promote to. If this is the |
| 1750 | // type we wanted, we have a promotion. Otherwise, no |
| 1751 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1752 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1758 | // rvalue of type int if int can represent all the values of the |
| 1759 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1760 | // unsigned int can represent all the values of the bit-field. If |
| 1761 | // the bit-field is larger yet, no integral promotion applies to |
| 1762 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1763 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1764 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1765 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1766 | using llvm::APSInt; |
| 1767 | if (From) |
| 1768 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1769 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1770 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1771 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1772 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1773 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1775 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1776 | if (BitWidth < ToSize || |
| 1777 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1778 | return To->getKind() == BuiltinType::Int; |
| 1779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1781 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1782 | // that fits into an unsigned int? |
| 1783 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1784 | return To->getKind() == BuiltinType::UInt; |
| 1785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1787 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1788 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1791 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1792 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1793 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1794 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1795 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1796 | |
| 1797 | return false; |
| 1798 | } |
| 1799 | |
| 1800 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1801 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1802 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1804 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1805 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1806 | /// An rvalue of type float can be converted to an rvalue of type |
| 1807 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1808 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1809 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1810 | return true; |
| 1811 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1812 | // C99 6.3.1.5p1: |
| 1813 | // When a float is promoted to double or long double, or a |
| 1814 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1815 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1816 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1817 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1818 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1819 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1820 | |
| 1821 | // Half can be promoted to float. |
| 1822 | if (FromBuiltin->getKind() == BuiltinType::Half && |
| 1823 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1824 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1827 | return false; |
| 1828 | } |
| 1829 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1830 | /// \brief Determine if a conversion is a complex promotion. |
| 1831 | /// |
| 1832 | /// A complex promotion is defined as a complex -> complex conversion |
| 1833 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1834 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1835 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1836 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1837 | if (!FromComplex) |
| 1838 | return false; |
| 1839 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1840 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1841 | if (!ToComplex) |
| 1842 | return false; |
| 1843 | |
| 1844 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1845 | ToComplex->getElementType()) || |
| 1846 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1847 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1850 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1851 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1852 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1853 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1854 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1855 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1857 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1858 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1859 | ASTContext &Context, |
| 1860 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1861 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1862 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1863 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1864 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1865 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1866 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1867 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1868 | |
| 1869 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1870 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1871 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1872 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1874 | if (StripObjCLifetime) |
| 1875 | Quals.removeObjCLifetime(); |
| 1876 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1878 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1879 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1880 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1881 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1884 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1885 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1886 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1887 | return Context.getPointerType(ToPointee); |
| 1888 | } |
| 1889 | |
| 1890 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1891 | QualType QualifiedCanonToPointee |
| 1892 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1894 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1895 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1896 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1897 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1898 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1900 | bool InOverloadResolution, |
| 1901 | ASTContext &Context) { |
| 1902 | // Handle value-dependent integral null pointer constants correctly. |
| 1903 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1904 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1905 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1906 | return !InOverloadResolution; |
| 1907 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1908 | return Expr->isNullPointerConstant(Context, |
| 1909 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1910 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1911 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1913 | /// IsPointerConversion - Determines whether the conversion of the |
| 1914 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1915 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1916 | /// 4.10). If so, returns true and places the converted type (that |
| 1917 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1918 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1919 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1920 | /// This routine also supports conversions to and from block pointers |
| 1921 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1922 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1923 | /// appropriate overloading rules for Objective-C, we may want to |
| 1924 | /// split the Objective-C checks into a different routine; however, |
| 1925 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1926 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1927 | /// set if the conversion is an allowed Objective-C conversion that |
| 1928 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1929 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1930 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1931 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1933 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1934 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1935 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1936 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1937 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1939 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1940 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1941 | ConvertedType = ToType; |
| 1942 | return true; |
| 1943 | } |
| 1944 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1945 | // Blocks: Block pointers can be converted to void*. |
| 1946 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1947 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1948 | ConvertedType = ToType; |
| 1949 | return true; |
| 1950 | } |
| 1951 | // Blocks: A null pointer constant can be converted to a block |
| 1952 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1954 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1955 | ConvertedType = ToType; |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1959 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1960 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1962 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1963 | ConvertedType = ToType; |
| 1964 | return true; |
| 1965 | } |
| 1966 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1967 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1968 | if (!ToTypePtr) |
| 1969 | return false; |
| 1970 | |
| 1971 | // 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] | 1972 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1973 | ConvertedType = ToType; |
| 1974 | return true; |
| 1975 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1976 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1977 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1978 | // , including objective-c pointers. |
| 1979 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1980 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1981 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1982 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1983 | FromType->getAs<ObjCObjectPointerType>(), |
| 1984 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1985 | ToType, Context); |
| 1986 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1987 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1988 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1989 | if (!FromTypePtr) |
| 1990 | return false; |
| 1991 | |
| 1992 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1993 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1994 | // If the unqualified pointee types are the same, this can't be a |
Douglas Gregor | 4e938f57b | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 1995 | // pointer conversion, so don't do all of the work below. |
| 1996 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1997 | return false; |
| 1998 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1999 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2000 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2001 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2002 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2003 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2005 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2006 | ToType, Context, |
| 2007 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2008 | return true; |
| 2009 | } |
| 2010 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2011 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2012 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2013 | ToPointeeType->isVoidType()) { |
| 2014 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2015 | ToPointeeType, |
| 2016 | ToType, Context); |
| 2017 | return true; |
| 2018 | } |
| 2019 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2020 | // When we're overloading in C, we allow a special kind of pointer |
| 2021 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2022 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2023 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2025 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2027 | return true; |
| 2028 | } |
| 2029 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2030 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2032 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2033 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2034 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2035 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2036 | // necessitates this conversion is ill-formed. The result of the |
| 2037 | // conversion is a pointer to the base class sub-object of the |
| 2038 | // derived class object. The null pointer value is converted to |
| 2039 | // the null pointer value of the destination type. |
| 2040 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2041 | // Note that we do not check for ambiguity or inaccessibility |
| 2042 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2043 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2044 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2045 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2046 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2047 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2049 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2050 | ToType, Context); |
| 2051 | return true; |
| 2052 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2053 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2054 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2055 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2056 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2057 | ToPointeeType, |
| 2058 | ToType, Context); |
| 2059 | return true; |
| 2060 | } |
| 2061 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2062 | return false; |
| 2063 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2064 | |
| 2065 | /// \brief Adopt the given qualifiers for the given type. |
| 2066 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2067 | Qualifiers TQs = T.getQualifiers(); |
| 2068 | |
| 2069 | // Check whether qualifiers already match. |
| 2070 | if (TQs == Qs) |
| 2071 | return T; |
| 2072 | |
| 2073 | if (Qs.compatiblyIncludes(TQs)) |
| 2074 | return Context.getQualifiedType(T, Qs); |
| 2075 | |
| 2076 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2077 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2078 | |
| 2079 | /// isObjCPointerConversion - Determines whether this is an |
| 2080 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2081 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2083 | QualType& ConvertedType, |
| 2084 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2085 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2086 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2087 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2088 | // The set of qualifiers on the type we're converting from. |
| 2089 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2090 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2091 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2092 | const ObjCObjectPointerType* ToObjCPtr = |
| 2093 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2095 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2096 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2097 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2098 | // If the pointee types are the same (ignoring qualifications), |
| 2099 | // then this is not a pointer conversion. |
| 2100 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2101 | FromObjCPtr->getPointeeType())) |
| 2102 | return false; |
| 2103 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2104 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2105 | // 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] | 2106 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2107 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2108 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2109 | return true; |
| 2110 | } |
| 2111 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2113 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2115 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2116 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2117 | return true; |
| 2118 | } |
| 2119 | // Objective C++: We're able to convert from a pointer to an |
| 2120 | // interface to a pointer to a different interface. |
| 2121 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2122 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2123 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2124 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2125 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2126 | FromObjCPtr->getPointeeType())) |
| 2127 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2128 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2129 | ToObjCPtr->getPointeeType(), |
| 2130 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2131 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2132 | return true; |
| 2133 | } |
| 2134 | |
| 2135 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2136 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2137 | // interfaces, which is permitted. However, we're going to |
| 2138 | // complain about it. |
| 2139 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2141 | ToObjCPtr->getPointeeType(), |
| 2142 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2143 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2144 | return true; |
| 2145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2147 | // 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] | 2148 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2149 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2150 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2151 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2152 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2153 | // Objective C++: We're able to convert from a pointer to any object |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2154 | // to a block pointer type. |
| 2155 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2156 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2157 | return true; |
| 2158 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2159 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2160 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2161 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2162 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2163 | // Objective C++: We're able to convert from a block pointer type to a |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2164 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2165 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2166 | return true; |
| 2167 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2168 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2169 | return false; |
| 2170 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2171 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2172 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2173 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2174 | else if (const BlockPointerType *FromBlockPtr = |
| 2175 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2176 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2177 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2178 | return false; |
| 2179 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2180 | // If we have pointers to pointers, recursively check whether this |
| 2181 | // is an Objective-C conversion. |
| 2182 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2183 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2184 | IncompatibleObjC)) { |
| 2185 | // We always complain about this conversion. |
| 2186 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2187 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2188 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2189 | return true; |
| 2190 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2191 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2192 | // as in I* to id. |
| 2193 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2194 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2195 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2196 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2198 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2199 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2200 | return true; |
| 2201 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2202 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2203 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2204 | // differences in the argument and result types are in Objective-C |
| 2205 | // pointer conversions. If so, we permit the conversion (but |
| 2206 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2208 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2209 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2210 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2211 | if (FromFunctionType && ToFunctionType) { |
| 2212 | // If the function types are exactly the same, this isn't an |
| 2213 | // Objective-C pointer conversion. |
| 2214 | if (Context.getCanonicalType(FromPointeeType) |
| 2215 | == Context.getCanonicalType(ToPointeeType)) |
| 2216 | return false; |
| 2217 | |
| 2218 | // Perform the quick checks that will tell us whether these |
| 2219 | // function types are obviously different. |
| 2220 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2221 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2222 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2223 | return false; |
| 2224 | |
| 2225 | bool HasObjCConversion = false; |
| 2226 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2227 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2228 | // Okay, the types match exactly. Nothing to do. |
| 2229 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2230 | ToFunctionType->getResultType(), |
| 2231 | ConvertedType, IncompatibleObjC)) { |
| 2232 | // Okay, we have an Objective-C pointer conversion. |
| 2233 | HasObjCConversion = true; |
| 2234 | } else { |
| 2235 | // Function types are too different. Abort. |
| 2236 | return false; |
| 2237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2239 | // Check argument types. |
| 2240 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2241 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2242 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2243 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2244 | if (Context.getCanonicalType(FromArgType) |
| 2245 | == Context.getCanonicalType(ToArgType)) { |
| 2246 | // Okay, the types match exactly. Nothing to do. |
| 2247 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2248 | ConvertedType, IncompatibleObjC)) { |
| 2249 | // Okay, we have an Objective-C pointer conversion. |
| 2250 | HasObjCConversion = true; |
| 2251 | } else { |
| 2252 | // Argument types are too different. Abort. |
| 2253 | return false; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | if (HasObjCConversion) { |
| 2258 | // We had an Objective-C conversion. Allow this pointer |
| 2259 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2260 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2261 | IncompatibleObjC = true; |
| 2262 | return true; |
| 2263 | } |
| 2264 | } |
| 2265 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2266 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2267 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2268 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2269 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2270 | /// used for parameter passing when performing automatic reference counting. |
| 2271 | /// |
| 2272 | /// \param FromType The type we're converting form. |
| 2273 | /// |
| 2274 | /// \param ToType The type we're converting to. |
| 2275 | /// |
| 2276 | /// \param ConvertedType The type that will be produced after applying |
| 2277 | /// this conversion. |
| 2278 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2279 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2280 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2281 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2282 | return false; |
| 2283 | |
| 2284 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2285 | QualType ToPointee; |
| 2286 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2287 | ToPointee = ToPointer->getPointeeType(); |
| 2288 | else |
| 2289 | return false; |
| 2290 | |
| 2291 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2292 | if (!ToPointee->isObjCLifetimeType() || |
| 2293 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2294 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2295 | return false; |
| 2296 | |
| 2297 | // Argument must be a pointer to __strong to __weak. |
| 2298 | QualType FromPointee; |
| 2299 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2300 | FromPointee = FromPointer->getPointeeType(); |
| 2301 | else |
| 2302 | return false; |
| 2303 | |
| 2304 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2305 | if (!FromPointee->isObjCLifetimeType() || |
| 2306 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2307 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2308 | return false; |
| 2309 | |
| 2310 | // Make sure that we have compatible qualifiers. |
| 2311 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2312 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2313 | return false; |
| 2314 | |
| 2315 | // Remove qualifiers from the pointee type we're converting from; they |
| 2316 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2317 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2318 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2319 | |
| 2320 | // The unqualified form of the pointee types must be compatible. |
| 2321 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2322 | bool IncompatibleObjC; |
| 2323 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2324 | FromPointee = ToPointee; |
| 2325 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2326 | IncompatibleObjC)) |
| 2327 | return false; |
| 2328 | |
| 2329 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2330 | /// __autoreleasing pointee. |
| 2331 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2332 | ConvertedType = Context.getPointerType(FromPointee); |
| 2333 | return true; |
| 2334 | } |
| 2335 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2336 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2337 | QualType& ConvertedType) { |
| 2338 | QualType ToPointeeType; |
| 2339 | if (const BlockPointerType *ToBlockPtr = |
| 2340 | ToType->getAs<BlockPointerType>()) |
| 2341 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2342 | else |
| 2343 | return false; |
| 2344 | |
| 2345 | QualType FromPointeeType; |
| 2346 | if (const BlockPointerType *FromBlockPtr = |
| 2347 | FromType->getAs<BlockPointerType>()) |
| 2348 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2349 | else |
| 2350 | return false; |
| 2351 | // We have pointer to blocks, check whether the only |
| 2352 | // differences in the argument and result types are in Objective-C |
| 2353 | // pointer conversions. If so, we permit the conversion. |
| 2354 | |
| 2355 | const FunctionProtoType *FromFunctionType |
| 2356 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2357 | const FunctionProtoType *ToFunctionType |
| 2358 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2359 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2360 | if (!FromFunctionType || !ToFunctionType) |
| 2361 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2362 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2363 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2364 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Perform the quick checks that will tell us whether these |
| 2367 | // function types are obviously different. |
| 2368 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2369 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2370 | return false; |
| 2371 | |
| 2372 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2373 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2374 | if (FromEInfo != ToEInfo) |
| 2375 | return false; |
| 2376 | |
| 2377 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2378 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2379 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2380 | // Okay, the types match exactly. Nothing to do. |
| 2381 | } else { |
| 2382 | QualType RHS = FromFunctionType->getResultType(); |
| 2383 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2384 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2385 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2386 | LHS = LHS.getUnqualifiedType(); |
| 2387 | |
| 2388 | if (Context.hasSameType(RHS,LHS)) { |
| 2389 | // OK exact match. |
| 2390 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2391 | ConvertedType, IncompatibleObjC)) { |
| 2392 | if (IncompatibleObjC) |
| 2393 | return false; |
| 2394 | // Okay, we have an Objective-C pointer conversion. |
| 2395 | } |
| 2396 | else |
| 2397 | return false; |
| 2398 | } |
| 2399 | |
| 2400 | // Check argument types. |
| 2401 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2402 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2403 | IncompatibleObjC = false; |
| 2404 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2405 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2406 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2407 | // Okay, the types match exactly. Nothing to do. |
| 2408 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2409 | ConvertedType, IncompatibleObjC)) { |
| 2410 | if (IncompatibleObjC) |
| 2411 | return false; |
| 2412 | // Okay, we have an Objective-C pointer conversion. |
| 2413 | } else |
| 2414 | // Argument types are too different. Abort. |
| 2415 | return false; |
| 2416 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2417 | if (LangOpts.ObjCAutoRefCount && |
| 2418 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2419 | ToFunctionType)) |
| 2420 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2421 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2422 | ConvertedType = ToType; |
| 2423 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2426 | enum { |
| 2427 | ft_default, |
| 2428 | ft_different_class, |
| 2429 | ft_parameter_arity, |
| 2430 | ft_parameter_mismatch, |
| 2431 | ft_return_type, |
| 2432 | ft_qualifer_mismatch |
| 2433 | }; |
| 2434 | |
| 2435 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2436 | /// function types. Catches different number of parameter, mismatch in |
| 2437 | /// parameter types, and different return types. |
| 2438 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2439 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2440 | // If either type is not valid, include no extra info. |
| 2441 | if (FromType.isNull() || ToType.isNull()) { |
| 2442 | PDiag << ft_default; |
| 2443 | return; |
| 2444 | } |
| 2445 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2446 | // Get the function type from the pointers. |
| 2447 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2448 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2449 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2450 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2451 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2452 | << QualType(FromMember->getClass(), 0); |
| 2453 | return; |
| 2454 | } |
| 2455 | FromType = FromMember->getPointeeType(); |
| 2456 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2459 | if (FromType->isPointerType()) |
| 2460 | FromType = FromType->getPointeeType(); |
| 2461 | if (ToType->isPointerType()) |
| 2462 | ToType = ToType->getPointeeType(); |
| 2463 | |
| 2464 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2465 | FromType = FromType.getNonReferenceType(); |
| 2466 | ToType = ToType.getNonReferenceType(); |
| 2467 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2468 | // Don't print extra info for non-specialized template functions. |
| 2469 | if (FromType->isInstantiationDependentType() && |
| 2470 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2471 | PDiag << ft_default; |
| 2472 | return; |
| 2473 | } |
| 2474 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2475 | // No extra info for same types. |
| 2476 | if (Context.hasSameType(FromType, ToType)) { |
| 2477 | PDiag << ft_default; |
| 2478 | return; |
| 2479 | } |
| 2480 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2481 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2482 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2483 | |
| 2484 | // Both types need to be function types. |
| 2485 | if (!FromFunction || !ToFunction) { |
| 2486 | PDiag << ft_default; |
| 2487 | return; |
| 2488 | } |
| 2489 | |
| 2490 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2491 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2492 | << FromFunction->getNumArgs(); |
| 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | // Handle different parameter types. |
| 2497 | unsigned ArgPos; |
| 2498 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2499 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2500 | << ToFunction->getArgType(ArgPos) |
| 2501 | << FromFunction->getArgType(ArgPos); |
| 2502 | return; |
| 2503 | } |
| 2504 | |
| 2505 | // Handle different return type. |
| 2506 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2507 | ToFunction->getResultType())) { |
| 2508 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2509 | << FromFunction->getResultType(); |
| 2510 | return; |
| 2511 | } |
| 2512 | |
| 2513 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2514 | ToQuals = ToFunction->getTypeQuals(); |
| 2515 | if (FromQuals != ToQuals) { |
| 2516 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2517 | return; |
| 2518 | } |
| 2519 | |
| 2520 | // Unable to find a difference, so add no extra info. |
| 2521 | PDiag << ft_default; |
| 2522 | } |
| 2523 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2524 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2525 | /// for equality of their argument types. Caller has already checked that |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2526 | /// they have same number of arguments. This routine assumes that Objective-C |
| 2527 | /// pointer types which only differ in their protocol qualifiers are equal. |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 2528 | /// If the parameters are different, ArgPos will have the parameter index |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2529 | /// of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2531 | const FunctionProtoType *NewType, |
| 2532 | unsigned *ArgPos) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2533 | if (!getLangOpts().ObjC1) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2534 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2535 | N = NewType->arg_type_begin(), |
| 2536 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2537 | if (!Context.hasSameType(*O, *N)) { |
| 2538 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2539 | return false; |
| 2540 | } |
| 2541 | } |
| 2542 | return true; |
| 2543 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2545 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2546 | N = NewType->arg_type_begin(), |
| 2547 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2548 | QualType ToType = (*O); |
| 2549 | QualType FromType = (*N); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2550 | if (!Context.hasSameType(ToType, FromType)) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2551 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 2552 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 2553 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 2554 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 2555 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 2556 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2557 | continue; |
| 2558 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2559 | else if (const ObjCObjectPointerType *PTTo = |
| 2560 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2561 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2562 | FromType->getAs<ObjCObjectPointerType>()) |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2563 | if (Context.hasSameUnqualifiedType( |
| 2564 | PTTo->getObjectType()->getBaseType(), |
| 2565 | PTFr->getObjectType()->getBaseType())) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2566 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2567 | } |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2568 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | return true; |
| 2573 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2575 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2576 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2577 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2578 | /// conversions for which IsPointerConversion has already returned |
| 2579 | /// true. It returns true and produces a diagnostic if there was an |
| 2580 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2581 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2582 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2583 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2584 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2585 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2586 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2587 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2588 | Kind = CK_BitCast; |
| 2589 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2590 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2591 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2592 | Expr::NPCK_ZeroExpression) { |
| 2593 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2594 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2595 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2596 | << ToType << From->getSourceRange()); |
| 2597 | else if (!isUnevaluatedContext()) |
| 2598 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2599 | << ToType << From->getSourceRange(); |
| 2600 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2601 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2602 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2603 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2604 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2605 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2606 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2607 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2608 | // We must have a derived-to-base conversion. Check an |
| 2609 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2610 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2611 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2612 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2613 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2614 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2615 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2616 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2617 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2618 | } |
| 2619 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2620 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2621 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2622 | if (const ObjCObjectPointerType *FromPtrType = |
| 2623 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2624 | // Objective-C++ conversions are always okay. |
| 2625 | // FIXME: We should have a different class of conversions for the |
| 2626 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2627 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2628 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2629 | } else if (FromType->isBlockPointerType()) { |
| 2630 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2631 | } else { |
| 2632 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2633 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2634 | } else if (ToType->isBlockPointerType()) { |
| 2635 | if (!FromType->isBlockPointerType()) |
| 2636 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2637 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2638 | |
| 2639 | // We shouldn't fall into this case unless it's valid for other |
| 2640 | // reasons. |
| 2641 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2642 | Kind = CK_NullToPointer; |
| 2643 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2644 | return false; |
| 2645 | } |
| 2646 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2647 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2648 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2649 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2650 | /// If so, returns true and places the converted type (that might differ from |
| 2651 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2652 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2654 | bool InOverloadResolution, |
| 2655 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2656 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2657 | if (!ToTypePtr) |
| 2658 | return false; |
| 2659 | |
| 2660 | // 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] | 2661 | if (From->isNullPointerConstant(Context, |
| 2662 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2663 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2664 | ConvertedType = ToType; |
| 2665 | return true; |
| 2666 | } |
| 2667 | |
| 2668 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2669 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2670 | if (!FromTypePtr) |
| 2671 | return false; |
| 2672 | |
| 2673 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2674 | // where D is derived from B (C++ 4.11p2). |
| 2675 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2676 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2678 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2679 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2680 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2681 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2682 | ToClass.getTypePtr()); |
| 2683 | return true; |
| 2684 | } |
| 2685 | |
| 2686 | return false; |
| 2687 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2688 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2689 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2690 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2691 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2692 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2693 | /// true and produces a diagnostic if there was an error, or returns false |
| 2694 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2696 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2697 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2698 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2699 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2700 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2701 | if (!FromPtrType) { |
| 2702 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2703 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2704 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2705 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2706 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2707 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2708 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2709 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2710 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2711 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2712 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2713 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2714 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2715 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2716 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2717 | // FIXME: What about dependent types? |
| 2718 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2719 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2720 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2721 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2722 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2723 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2724 | assert(DerivationOkay && |
| 2725 | "Should not have been called if derivation isn't OK."); |
| 2726 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2727 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2728 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2729 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2730 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2731 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2732 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2733 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2734 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2736 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2737 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2738 | << FromClass << ToClass << QualType(VBase, 0) |
| 2739 | << From->getSourceRange(); |
| 2740 | return true; |
| 2741 | } |
| 2742 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2743 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2744 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2745 | Paths.front(), |
| 2746 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2747 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2748 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2749 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2750 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2751 | return false; |
| 2752 | } |
| 2753 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2754 | /// IsQualificationConversion - Determines whether the conversion from |
| 2755 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2756 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2757 | /// |
| 2758 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2759 | /// when the qualification conversion involves a change in the Objective-C |
| 2760 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2762 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2763 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2764 | FromType = Context.getCanonicalType(FromType); |
| 2765 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2766 | ObjCLifetimeConversion = false; |
| 2767 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2768 | // If FromType and ToType are the same type, this is not a |
| 2769 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2770 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2771 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2773 | // (C++ 4.4p4): |
| 2774 | // A conversion can add cv-qualifiers at levels other than the first |
| 2775 | // in multi-level pointers, subject to the following rules: [...] |
| 2776 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2777 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2778 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2779 | // Within each iteration of the loop, we check the qualifiers to |
| 2780 | // determine if this still looks like a qualification |
| 2781 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2782 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2783 | // until there are no more pointers or pointers-to-members left to |
| 2784 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2785 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2787 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2788 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2789 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2790 | // Objective-C ARC: |
| 2791 | // Check Objective-C lifetime conversions. |
| 2792 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2793 | UnwrappedAnyPointer) { |
| 2794 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2795 | ObjCLifetimeConversion = true; |
| 2796 | FromQuals.removeObjCLifetime(); |
| 2797 | ToQuals.removeObjCLifetime(); |
| 2798 | } else { |
| 2799 | // Qualification conversions cannot cast between different |
| 2800 | // Objective-C lifetime qualifiers. |
| 2801 | return false; |
| 2802 | } |
| 2803 | } |
| 2804 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2805 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2806 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2807 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2808 | FromQuals.removeObjCGCAttr(); |
| 2809 | ToQuals.removeObjCGCAttr(); |
| 2810 | } |
| 2811 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2812 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2813 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2814 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2815 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2817 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2818 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2819 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2820 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2821 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2823 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2824 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2826 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2827 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2828 | |
| 2829 | // We are left with FromType and ToType being the pointee types |
| 2830 | // after unwrapping the original FromType and ToType the same number |
| 2831 | // of types. If we unwrapped any pointers, and if FromType and |
| 2832 | // ToType have the same unqualified type (since we checked |
| 2833 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2834 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2837 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2838 | /// atomic type. |
| 2839 | /// |
| 2840 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2841 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2842 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2843 | bool InOverloadResolution, |
| 2844 | StandardConversionSequence &SCS, |
| 2845 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2846 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2847 | if (!ToAtomic) |
| 2848 | return false; |
| 2849 | |
| 2850 | StandardConversionSequence InnerSCS; |
| 2851 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2852 | InOverloadResolution, InnerSCS, |
| 2853 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2854 | return false; |
| 2855 | |
| 2856 | SCS.Second = InnerSCS.Second; |
| 2857 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2858 | SCS.Third = InnerSCS.Third; |
| 2859 | SCS.QualificationIncludesObjCLifetime |
| 2860 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2861 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2865 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2866 | CXXConstructorDecl *Constructor, |
| 2867 | QualType Type) { |
| 2868 | const FunctionProtoType *CtorType = |
| 2869 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2870 | if (CtorType->getNumArgs() > 0) { |
| 2871 | QualType FirstArg = CtorType->getArgType(0); |
| 2872 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2873 | return true; |
| 2874 | } |
| 2875 | return false; |
| 2876 | } |
| 2877 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2878 | static OverloadingResult |
| 2879 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2880 | CXXRecordDecl *To, |
| 2881 | UserDefinedConversionSequence &User, |
| 2882 | OverloadCandidateSet &CandidateSet, |
| 2883 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2884 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2885 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2886 | Con != ConEnd; ++Con) { |
| 2887 | NamedDecl *D = *Con; |
| 2888 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2889 | |
| 2890 | // Find the constructor (which may be a template). |
| 2891 | CXXConstructorDecl *Constructor = 0; |
| 2892 | FunctionTemplateDecl *ConstructorTmpl |
| 2893 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2894 | if (ConstructorTmpl) |
| 2895 | Constructor |
| 2896 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2897 | else |
| 2898 | Constructor = cast<CXXConstructorDecl>(D); |
| 2899 | |
| 2900 | bool Usable = !Constructor->isInvalidDecl() && |
| 2901 | S.isInitListConstructor(Constructor) && |
| 2902 | (AllowExplicit || !Constructor->isExplicit()); |
| 2903 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2904 | // If the first argument is (a reference to) the target type, |
| 2905 | // suppress conversions. |
| 2906 | bool SuppressUserConversions = |
| 2907 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2908 | if (ConstructorTmpl) |
| 2909 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2910 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2911 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2912 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2913 | else |
| 2914 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2915 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2916 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2921 | |
| 2922 | OverloadCandidateSet::iterator Best; |
| 2923 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2924 | case OR_Success: { |
| 2925 | // Record the standard conversion we used and the conversion function. |
| 2926 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2927 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2928 | // Initializer lists don't have conversions as such. |
| 2929 | User.Before.setAsIdentityConversion(); |
| 2930 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2931 | User.ConversionFunction = Constructor; |
| 2932 | User.FoundConversionFunction = Best->FoundDecl; |
| 2933 | User.After.setAsIdentityConversion(); |
| 2934 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2935 | User.After.setAllToTypes(ToType); |
| 2936 | return OR_Success; |
| 2937 | } |
| 2938 | |
| 2939 | case OR_No_Viable_Function: |
| 2940 | return OR_No_Viable_Function; |
| 2941 | case OR_Deleted: |
| 2942 | return OR_Deleted; |
| 2943 | case OR_Ambiguous: |
| 2944 | return OR_Ambiguous; |
| 2945 | } |
| 2946 | |
| 2947 | llvm_unreachable("Invalid OverloadResult!"); |
| 2948 | } |
| 2949 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2950 | /// Determines whether there is a user-defined conversion sequence |
| 2951 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2952 | /// ToType. If such a conversion exists, User will contain the |
| 2953 | /// user-defined conversion sequence that performs such a conversion |
| 2954 | /// and this routine will return true. Otherwise, this routine returns |
| 2955 | /// false and User is unspecified. |
| 2956 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2957 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2958 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2959 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2960 | static OverloadingResult |
| 2961 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2962 | UserDefinedConversionSequence &User, |
| 2963 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2964 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2965 | // Whether we will only visit constructors. |
| 2966 | bool ConstructorsOnly = false; |
| 2967 | |
| 2968 | // If the type we are conversion to is a class type, enumerate its |
| 2969 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2970 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2971 | // C++ [over.match.ctor]p1: |
| 2972 | // When objects of class type are direct-initialized (8.5), or |
| 2973 | // copy-initialized from an expression of the same or a |
| 2974 | // derived class type (8.5), overload resolution selects the |
| 2975 | // constructor. [...] For copy-initialization, the candidate |
| 2976 | // functions are all the converting constructors (12.3.1) of |
| 2977 | // that class. The argument list is the expression-list within |
| 2978 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2979 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2980 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2981 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2982 | ConstructorsOnly = true; |
| 2983 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 2984 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2985 | // RequireCompleteType may have returned true due to some invalid decl |
| 2986 | // during template instantiation, but ToType may be complete enough now |
| 2987 | // to try to recover. |
| 2988 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2989 | // We're not going to find any constructors. |
| 2990 | } else if (CXXRecordDecl *ToRecordDecl |
| 2991 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2992 | |
| 2993 | Expr **Args = &From; |
| 2994 | unsigned NumArgs = 1; |
| 2995 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2996 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2997 | // But first, see if there is an init-list-contructor that will work. |
| 2998 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 2999 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3000 | if (Result != OR_No_Viable_Function) |
| 3001 | return Result; |
| 3002 | // Never mind. |
| 3003 | CandidateSet.clear(); |
| 3004 | |
| 3005 | // If we're list-initializing, we pass the individual elements as |
| 3006 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3007 | Args = InitList->getInits(); |
| 3008 | NumArgs = InitList->getNumInits(); |
| 3009 | ListInitializing = true; |
| 3010 | } |
| 3011 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3012 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3013 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3014 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3015 | NamedDecl *D = *Con; |
| 3016 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3017 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3018 | // Find the constructor (which may be a template). |
| 3019 | CXXConstructorDecl *Constructor = 0; |
| 3020 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3021 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3022 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3024 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3025 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3026 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3027 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3028 | bool Usable = !Constructor->isInvalidDecl(); |
| 3029 | if (ListInitializing) |
| 3030 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3031 | else |
| 3032 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3033 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3034 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3035 | if (SuppressUserConversions && ListInitializing) { |
| 3036 | SuppressUserConversions = false; |
| 3037 | if (NumArgs == 1) { |
| 3038 | // If the first argument is (a reference to) the target type, |
| 3039 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3040 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3041 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3042 | } |
| 3043 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3044 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3045 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3046 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3047 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3048 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3049 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3050 | // Allow one user-defined conversion when user specifies a |
| 3051 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3052 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3053 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3054 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3055 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3056 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3060 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3061 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3062 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3063 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3065 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3067 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3068 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3069 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3070 | CXXRecordDecl::conversion_iterator> |
| 3071 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3072 | for (CXXRecordDecl::conversion_iterator |
| 3073 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3074 | DeclAccessPair FoundDecl = I.getPair(); |
| 3075 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3076 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3077 | if (isa<UsingShadowDecl>(D)) |
| 3078 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3079 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3080 | CXXConversionDecl *Conv; |
| 3081 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3082 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3083 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3084 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3085 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3086 | |
| 3087 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3088 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3089 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3090 | ActingContext, From, ToType, |
| 3091 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3092 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3093 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3094 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3098 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3099 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3100 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3101 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3102 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3103 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3104 | case OR_Success: |
| 3105 | // Record the standard conversion we used and the conversion function. |
| 3106 | if (CXXConstructorDecl *Constructor |
| 3107 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3108 | // C++ [over.ics.user]p1: |
| 3109 | // If the user-defined conversion is specified by a |
| 3110 | // constructor (12.3.1), the initial standard conversion |
| 3111 | // sequence converts the source type to the type required by |
| 3112 | // the argument of the constructor. |
| 3113 | // |
| 3114 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3115 | if (isa<InitListExpr>(From)) { |
| 3116 | // Initializer lists don't have conversions as such. |
| 3117 | User.Before.setAsIdentityConversion(); |
| 3118 | } else { |
| 3119 | if (Best->Conversions[0].isEllipsis()) |
| 3120 | User.EllipsisConversion = true; |
| 3121 | else { |
| 3122 | User.Before = Best->Conversions[0].Standard; |
| 3123 | User.EllipsisConversion = false; |
| 3124 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3125 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3126 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3127 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3128 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3129 | User.After.setAsIdentityConversion(); |
| 3130 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3131 | User.After.setAllToTypes(ToType); |
| 3132 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3133 | } |
| 3134 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3135 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3136 | // C++ [over.ics.user]p1: |
| 3137 | // |
| 3138 | // [...] If the user-defined conversion is specified by a |
| 3139 | // conversion function (12.3.2), the initial standard |
| 3140 | // conversion sequence converts the source type to the |
| 3141 | // implicit object parameter of the conversion function. |
| 3142 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3143 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3144 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3145 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3146 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3148 | // C++ [over.ics.user]p2: |
| 3149 | // The second standard conversion sequence converts the |
| 3150 | // result of the user-defined conversion to the target type |
| 3151 | // for the sequence. Since an implicit conversion sequence |
| 3152 | // is an initialization, the special rules for |
| 3153 | // initialization by user-defined conversion apply when |
| 3154 | // selecting the best user-defined conversion for a |
| 3155 | // user-defined conversion sequence (see 13.3.3 and |
| 3156 | // 13.3.3.1). |
| 3157 | User.After = Best->FinalConversion; |
| 3158 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3159 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3160 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3161 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3162 | case OR_No_Viable_Function: |
| 3163 | return OR_No_Viable_Function; |
| 3164 | case OR_Deleted: |
| 3165 | // No conversion here! We're done. |
| 3166 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3167 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3168 | case OR_Ambiguous: |
| 3169 | return OR_Ambiguous; |
| 3170 | } |
| 3171 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3172 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3173 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3174 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3175 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3176 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3177 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3178 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3179 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3180 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3181 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3182 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3183 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3184 | diag::err_typecheck_ambiguous_condition) |
| 3185 | << From->getType() << ToType << From->getSourceRange(); |
| 3186 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3187 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3188 | diag::err_typecheck_nonviable_condition) |
| 3189 | << From->getType() << ToType << From->getSourceRange(); |
| 3190 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3191 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3192 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3194 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3195 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3196 | /// \brief Compare the user-defined conversion functions or constructors |
| 3197 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3198 | /// is possible. |
| 3199 | static ImplicitConversionSequence::CompareKind |
| 3200 | compareConversionFunctions(Sema &S, |
| 3201 | FunctionDecl *Function1, |
| 3202 | FunctionDecl *Function2) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3203 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus0x) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3204 | return ImplicitConversionSequence::Indistinguishable; |
| 3205 | |
| 3206 | // Objective-C++: |
| 3207 | // If both conversion functions are implicitly-declared conversions from |
| 3208 | // a lambda closure type to a function pointer and a block pointer, |
| 3209 | // respectively, always prefer the conversion to a function pointer, |
| 3210 | // because the function pointer is more lightweight and is more likely |
| 3211 | // to keep code working. |
| 3212 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3213 | if (!Conv1) |
| 3214 | return ImplicitConversionSequence::Indistinguishable; |
| 3215 | |
| 3216 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3217 | if (!Conv2) |
| 3218 | return ImplicitConversionSequence::Indistinguishable; |
| 3219 | |
| 3220 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3221 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3222 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3223 | if (Block1 != Block2) |
| 3224 | return Block1? ImplicitConversionSequence::Worse |
| 3225 | : ImplicitConversionSequence::Better; |
| 3226 | } |
| 3227 | |
| 3228 | return ImplicitConversionSequence::Indistinguishable; |
| 3229 | } |
| 3230 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3231 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3232 | /// conversion sequences to determine whether one is better than the |
| 3233 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3234 | static ImplicitConversionSequence::CompareKind |
| 3235 | CompareImplicitConversionSequences(Sema &S, |
| 3236 | const ImplicitConversionSequence& ICS1, |
| 3237 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3238 | { |
| 3239 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3240 | // conversion sequences (as defined in 13.3.3.1) |
| 3241 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3242 | // conversion sequence than a user-defined conversion sequence or |
| 3243 | // an ellipsis conversion sequence, and |
| 3244 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3245 | // conversion sequence than an ellipsis conversion sequence |
| 3246 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3248 | // C++0x [over.best.ics]p10: |
| 3249 | // For the purpose of ranking implicit conversion sequences as |
| 3250 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3251 | // treated as a user-defined sequence that is indistinguishable |
| 3252 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3253 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3254 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3255 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3256 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3257 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3258 | // The following checks require both conversion sequences to be of |
| 3259 | // the same kind. |
| 3260 | if (ICS1.getKind() != ICS2.getKind()) |
| 3261 | return ImplicitConversionSequence::Indistinguishable; |
| 3262 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3263 | ImplicitConversionSequence::CompareKind Result = |
| 3264 | ImplicitConversionSequence::Indistinguishable; |
| 3265 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3266 | // Two implicit conversion sequences of the same form are |
| 3267 | // indistinguishable conversion sequences unless one of the |
| 3268 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3269 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3270 | Result = CompareStandardConversionSequences(S, |
| 3271 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3272 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3273 | // User-defined conversion sequence U1 is a better conversion |
| 3274 | // sequence than another user-defined conversion sequence U2 if |
| 3275 | // they contain the same user-defined conversion function or |
| 3276 | // constructor and if the second standard conversion sequence of |
| 3277 | // U1 is better than the second standard conversion sequence of |
| 3278 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3280 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3281 | Result = CompareStandardConversionSequences(S, |
| 3282 | ICS1.UserDefined.After, |
| 3283 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3284 | else |
| 3285 | Result = compareConversionFunctions(S, |
| 3286 | ICS1.UserDefined.ConversionFunction, |
| 3287 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3290 | // List-initialization sequence L1 is a better conversion sequence than |
| 3291 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3292 | // for some X and L2 does not. |
| 3293 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3294 | !ICS1.isBad() && |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3295 | ICS1.isListInitializationSequence() && |
| 3296 | ICS2.isListInitializationSequence()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3297 | if (ICS1.isStdInitializerListElement() && |
| 3298 | !ICS2.isStdInitializerListElement()) |
| 3299 | return ImplicitConversionSequence::Better; |
| 3300 | if (!ICS1.isStdInitializerListElement() && |
| 3301 | ICS2.isStdInitializerListElement()) |
| 3302 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3308 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3309 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3310 | Qualifiers Quals; |
| 3311 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3312 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3313 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3315 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3316 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3318 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3319 | // determine if one is a proper subset of the other. |
| 3320 | static ImplicitConversionSequence::CompareKind |
| 3321 | compareStandardConversionSubsets(ASTContext &Context, |
| 3322 | const StandardConversionSequence& SCS1, |
| 3323 | const StandardConversionSequence& SCS2) { |
| 3324 | ImplicitConversionSequence::CompareKind Result |
| 3325 | = ImplicitConversionSequence::Indistinguishable; |
| 3326 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3327 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3328 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3329 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3330 | return ImplicitConversionSequence::Better; |
| 3331 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3332 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3334 | if (SCS1.Second != SCS2.Second) { |
| 3335 | if (SCS1.Second == ICK_Identity) |
| 3336 | Result = ImplicitConversionSequence::Better; |
| 3337 | else if (SCS2.Second == ICK_Identity) |
| 3338 | Result = ImplicitConversionSequence::Worse; |
| 3339 | else |
| 3340 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3341 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3342 | return ImplicitConversionSequence::Indistinguishable; |
| 3343 | |
| 3344 | if (SCS1.Third == SCS2.Third) { |
| 3345 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3346 | : ImplicitConversionSequence::Indistinguishable; |
| 3347 | } |
| 3348 | |
| 3349 | if (SCS1.Third == ICK_Identity) |
| 3350 | return Result == ImplicitConversionSequence::Worse |
| 3351 | ? ImplicitConversionSequence::Indistinguishable |
| 3352 | : ImplicitConversionSequence::Better; |
| 3353 | |
| 3354 | if (SCS2.Third == ICK_Identity) |
| 3355 | return Result == ImplicitConversionSequence::Better |
| 3356 | ? ImplicitConversionSequence::Indistinguishable |
| 3357 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3359 | return ImplicitConversionSequence::Indistinguishable; |
| 3360 | } |
| 3361 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3362 | /// \brief Determine whether one of the given reference bindings is better |
| 3363 | /// than the other based on what kind of bindings they are. |
| 3364 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3365 | const StandardConversionSequence &SCS2) { |
| 3366 | // C++0x [over.ics.rank]p3b4: |
| 3367 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3368 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3369 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3370 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3371 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3372 | // reference*. |
| 3373 | // |
| 3374 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3375 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3376 | // time of this writing) break the standard definition of std::forward |
| 3377 | // and std::reference_wrapper when dealing with references to functions. |
| 3378 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3379 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3380 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3381 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3382 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3383 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3384 | SCS2.IsLvalueReference) || |
| 3385 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3386 | !SCS2.IsLvalueReference); |
| 3387 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3388 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3389 | /// CompareStandardConversionSequences - Compare two standard |
| 3390 | /// conversion sequences to determine whether one is better than the |
| 3391 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3392 | static ImplicitConversionSequence::CompareKind |
| 3393 | CompareStandardConversionSequences(Sema &S, |
| 3394 | const StandardConversionSequence& SCS1, |
| 3395 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3396 | { |
| 3397 | // Standard conversion sequence S1 is a better conversion sequence |
| 3398 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3399 | |
| 3400 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3401 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3402 | // excluding any Lvalue Transformation; the identity conversion |
| 3403 | // sequence is considered to be a subsequence of any |
| 3404 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3405 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3406 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3407 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3408 | |
| 3409 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3410 | // defined below), or, if not that, |
| 3411 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3412 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3413 | if (Rank1 < Rank2) |
| 3414 | return ImplicitConversionSequence::Better; |
| 3415 | else if (Rank2 < Rank1) |
| 3416 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3418 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3419 | // are indistinguishable unless one of the following rules |
| 3420 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3422 | // A conversion that is not a conversion of a pointer, or |
| 3423 | // pointer to member, to bool is better than another conversion |
| 3424 | // that is such a conversion. |
| 3425 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3426 | return SCS2.isPointerConversionToBool() |
| 3427 | ? ImplicitConversionSequence::Better |
| 3428 | : ImplicitConversionSequence::Worse; |
| 3429 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3430 | // C++ [over.ics.rank]p4b2: |
| 3431 | // |
| 3432 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3433 | // conversion of B* to A* is better than conversion of B* to |
| 3434 | // void*, and conversion of A* to void* is better than conversion |
| 3435 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3437 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3439 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3440 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3441 | // Exactly one of the conversion sequences is a conversion to |
| 3442 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3443 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3444 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3445 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3446 | // Neither conversion sequence converts to a void pointer; compare |
| 3447 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3448 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3449 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3450 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3451 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3452 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3453 | // Both conversion sequences are conversions to void |
| 3454 | // pointers. Compare the source types to determine if there's an |
| 3455 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3456 | QualType FromType1 = SCS1.getFromType(); |
| 3457 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3458 | |
| 3459 | // Adjust the types we're converting from via the array-to-pointer |
| 3460 | // conversion, if we need to. |
| 3461 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3462 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3463 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3464 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3465 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3466 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3467 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3468 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3469 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3470 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3471 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3472 | return ImplicitConversionSequence::Worse; |
| 3473 | |
| 3474 | // Objective-C++: If one interface is more specific than the |
| 3475 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3476 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3477 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3478 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3479 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3480 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3481 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3482 | FromObjCPtr2); |
| 3483 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3484 | FromObjCPtr1); |
| 3485 | if (AssignLeft != AssignRight) { |
| 3486 | return AssignLeft? ImplicitConversionSequence::Better |
| 3487 | : ImplicitConversionSequence::Worse; |
| 3488 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3489 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3490 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3491 | |
| 3492 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3493 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3494 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3495 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3496 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3498 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3499 | // Check for a better reference binding based on the kind of bindings. |
| 3500 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3501 | return ImplicitConversionSequence::Better; |
| 3502 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3503 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3505 | // C++ [over.ics.rank]p3b4: |
| 3506 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3507 | // which the references refer are the same type except for |
| 3508 | // top-level cv-qualifiers, and the type to which the reference |
| 3509 | // initialized by S2 refers is more cv-qualified than the type |
| 3510 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3511 | QualType T1 = SCS1.getToType(2); |
| 3512 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3513 | T1 = S.Context.getCanonicalType(T1); |
| 3514 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3515 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3516 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3517 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3518 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3519 | // Objective-C++ ARC: If the references refer to objects with different |
| 3520 | // lifetimes, prefer bindings that don't change lifetime. |
| 3521 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3522 | SCS2.ObjCLifetimeConversionBinding) { |
| 3523 | return SCS1.ObjCLifetimeConversionBinding |
| 3524 | ? ImplicitConversionSequence::Worse |
| 3525 | : ImplicitConversionSequence::Better; |
| 3526 | } |
| 3527 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3528 | // If the type is an array type, promote the element qualifiers to the |
| 3529 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3530 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3531 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3532 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3533 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3534 | if (T2.isMoreQualifiedThan(T1)) |
| 3535 | return ImplicitConversionSequence::Better; |
| 3536 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3537 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3538 | } |
| 3539 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3540 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3541 | // In Microsoft mode, prefer an integral conversion to a |
| 3542 | // floating-to-integral conversion if the integral conversion |
| 3543 | // is between types of the same size. |
| 3544 | // For example: |
| 3545 | // void f(float); |
| 3546 | // void f(int); |
| 3547 | // int main { |
| 3548 | // long a; |
| 3549 | // f(a); |
| 3550 | // } |
| 3551 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3552 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3553 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3554 | SCS1.Second == ICK_Integral_Conversion && |
| 3555 | SCS2.Second == ICK_Floating_Integral && |
| 3556 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3557 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3558 | return ImplicitConversionSequence::Better; |
| 3559 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3560 | return ImplicitConversionSequence::Indistinguishable; |
| 3561 | } |
| 3562 | |
| 3563 | /// CompareQualificationConversions - Compares two standard conversion |
| 3564 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3565 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3566 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3567 | CompareQualificationConversions(Sema &S, |
| 3568 | const StandardConversionSequence& SCS1, |
| 3569 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3570 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3571 | // -- S1 and S2 differ only in their qualification conversion and |
| 3572 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3573 | // cv-qualification signature of type T1 is a proper subset of |
| 3574 | // the cv-qualification signature of type T2, and S1 is not the |
| 3575 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3576 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3577 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3578 | return ImplicitConversionSequence::Indistinguishable; |
| 3579 | |
| 3580 | // FIXME: the example in the standard doesn't use a qualification |
| 3581 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3582 | QualType T1 = SCS1.getToType(2); |
| 3583 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3584 | T1 = S.Context.getCanonicalType(T1); |
| 3585 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3586 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3587 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3588 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3589 | |
| 3590 | // If the types are the same, we won't learn anything by unwrapped |
| 3591 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3592 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3593 | return ImplicitConversionSequence::Indistinguishable; |
| 3594 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3595 | // If the type is an array type, promote the element qualifiers to the type |
| 3596 | // for comparison. |
| 3597 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3598 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3599 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3600 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3601 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3603 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3604 | |
| 3605 | // Objective-C++ ARC: |
| 3606 | // Prefer qualification conversions not involving a change in lifetime |
| 3607 | // to qualification conversions that do not change lifetime. |
| 3608 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3609 | SCS2.QualificationIncludesObjCLifetime) { |
| 3610 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3611 | ? ImplicitConversionSequence::Worse |
| 3612 | : ImplicitConversionSequence::Better; |
| 3613 | } |
| 3614 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3615 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3616 | // Within each iteration of the loop, we check the qualifiers to |
| 3617 | // determine if this still looks like a qualification |
| 3618 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3619 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3620 | // until there are no more pointers or pointers-to-members left |
| 3621 | // to unwrap. This essentially mimics what |
| 3622 | // IsQualificationConversion does, but here we're checking for a |
| 3623 | // strict subset of qualifiers. |
| 3624 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3625 | // The qualifiers are the same, so this doesn't tell us anything |
| 3626 | // about how the sequences rank. |
| 3627 | ; |
| 3628 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3629 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3630 | if (Result == ImplicitConversionSequence::Worse) |
| 3631 | // Neither has qualifiers that are a subset of the other's |
| 3632 | // qualifiers. |
| 3633 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3635 | Result = ImplicitConversionSequence::Better; |
| 3636 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3637 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3638 | if (Result == ImplicitConversionSequence::Better) |
| 3639 | // Neither has qualifiers that are a subset of the other's |
| 3640 | // qualifiers. |
| 3641 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3642 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3643 | Result = ImplicitConversionSequence::Worse; |
| 3644 | } else { |
| 3645 | // Qualifiers are disjoint. |
| 3646 | return ImplicitConversionSequence::Indistinguishable; |
| 3647 | } |
| 3648 | |
| 3649 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3650 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3651 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3654 | // Check that the winning standard conversion sequence isn't using |
| 3655 | // the deprecated string literal array to pointer conversion. |
| 3656 | switch (Result) { |
| 3657 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3658 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3659 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3660 | break; |
| 3661 | |
| 3662 | case ImplicitConversionSequence::Indistinguishable: |
| 3663 | break; |
| 3664 | |
| 3665 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3666 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3667 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3668 | break; |
| 3669 | } |
| 3670 | |
| 3671 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3674 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3675 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3676 | /// various kinds of derived-to-base conversions (C++ |
| 3677 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3678 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3679 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3680 | CompareDerivedToBaseConversions(Sema &S, |
| 3681 | const StandardConversionSequence& SCS1, |
| 3682 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3683 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3684 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3685 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3686 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3687 | |
| 3688 | // Adjust the types we're converting from via the array-to-pointer |
| 3689 | // conversion, if we need to. |
| 3690 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3691 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3692 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3693 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3694 | |
| 3695 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3696 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3697 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3698 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3699 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3701 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3702 | // |
| 3703 | // If class B is derived directly or indirectly from class A and |
| 3704 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3705 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3706 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3708 | SCS2.Second == ICK_Pointer_Conversion && |
| 3709 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3710 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3711 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3712 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3713 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3715 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3716 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3717 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3718 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3719 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3720 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3721 | // -- 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] | 3722 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3723 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3724 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3725 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3726 | return ImplicitConversionSequence::Worse; |
| 3727 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3728 | |
| 3729 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3730 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3731 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3732 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3733 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3734 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3735 | } |
| 3736 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3737 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3738 | const ObjCObjectPointerType *FromPtr1 |
| 3739 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3740 | const ObjCObjectPointerType *FromPtr2 |
| 3741 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3742 | const ObjCObjectPointerType *ToPtr1 |
| 3743 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3744 | const ObjCObjectPointerType *ToPtr2 |
| 3745 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3746 | |
| 3747 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3748 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3749 | // that we do for C++ pointers to class types. However, we employ the |
| 3750 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3751 | // Objective-C pointer types. |
| 3752 | bool FromAssignLeft |
| 3753 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3754 | bool FromAssignRight |
| 3755 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3756 | bool ToAssignLeft |
| 3757 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3758 | bool ToAssignRight |
| 3759 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3760 | |
| 3761 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3762 | // type is better than a conversion to 'id'. |
| 3763 | if (ToPtr1->isObjCIdType() && |
| 3764 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3765 | return ImplicitConversionSequence::Worse; |
| 3766 | if (ToPtr2->isObjCIdType() && |
| 3767 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3768 | return ImplicitConversionSequence::Better; |
| 3769 | |
| 3770 | // A conversion to a non-id object pointer type is better than a |
| 3771 | // conversion to a qualified 'id' type |
| 3772 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3773 | return ImplicitConversionSequence::Worse; |
| 3774 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3775 | return ImplicitConversionSequence::Better; |
| 3776 | |
| 3777 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3778 | // type is better than a conversion to 'Class'. |
| 3779 | if (ToPtr1->isObjCClassType() && |
| 3780 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3781 | return ImplicitConversionSequence::Worse; |
| 3782 | if (ToPtr2->isObjCClassType() && |
| 3783 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3784 | return ImplicitConversionSequence::Better; |
| 3785 | |
| 3786 | // A conversion to a non-Class object pointer type is better than a |
| 3787 | // conversion to a qualified 'Class' type. |
| 3788 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3789 | return ImplicitConversionSequence::Worse; |
| 3790 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3791 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3793 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3794 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3795 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3796 | (ToAssignLeft != ToAssignRight)) |
| 3797 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3798 | : ImplicitConversionSequence::Better; |
| 3799 | |
| 3800 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3801 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3802 | (FromAssignLeft != FromAssignRight)) |
| 3803 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3804 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3805 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3806 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3807 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3808 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3809 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3810 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3811 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3813 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3815 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3816 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3817 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3818 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3819 | ToType2->getAs<MemberPointerType>(); |
| 3820 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3821 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3822 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3823 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3824 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3825 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3826 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3827 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3828 | // 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] | 3829 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3830 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3831 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3832 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3833 | return ImplicitConversionSequence::Better; |
| 3834 | } |
| 3835 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3836 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3837 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3838 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3839 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3840 | return ImplicitConversionSequence::Worse; |
| 3841 | } |
| 3842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3844 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3845 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3846 | // -- binding of an expression of type C to a reference of type |
| 3847 | // B& is better than binding an expression of type C to a |
| 3848 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3849 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3850 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3851 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3852 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3853 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3854 | return ImplicitConversionSequence::Worse; |
| 3855 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3857 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3858 | // -- binding of an expression of type B to a reference of type |
| 3859 | // A& is better than binding an expression of type C to a |
| 3860 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3861 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3862 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3863 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3864 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3865 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3866 | return ImplicitConversionSequence::Worse; |
| 3867 | } |
| 3868 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3870 | return ImplicitConversionSequence::Indistinguishable; |
| 3871 | } |
| 3872 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3873 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3874 | /// determine whether they are reference-related, |
| 3875 | /// reference-compatible, reference-compatible with added |
| 3876 | /// qualification, or incompatible, for use in C++ initialization by |
| 3877 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3878 | /// type, and the first type (T1) is the pointee type of the reference |
| 3879 | /// type being initialized. |
| 3880 | Sema::ReferenceCompareResult |
| 3881 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3882 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3883 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3884 | bool &ObjCConversion, |
| 3885 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3886 | assert(!OrigT1->isReferenceType() && |
| 3887 | "T1 must be the pointee type of the reference type"); |
| 3888 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3889 | |
| 3890 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3891 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3892 | Qualifiers T1Quals, T2Quals; |
| 3893 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3894 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3895 | |
| 3896 | // C++ [dcl.init.ref]p4: |
| 3897 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3898 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3899 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3900 | DerivedToBase = false; |
| 3901 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3902 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3903 | if (UnqualT1 == UnqualT2) { |
| 3904 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3905 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3906 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3907 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3908 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3909 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3910 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3911 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3912 | else |
| 3913 | return Ref_Incompatible; |
| 3914 | |
| 3915 | // At this point, we know that T1 and T2 are reference-related (at |
| 3916 | // least). |
| 3917 | |
| 3918 | // If the type is an array type, promote the element qualifiers to the type |
| 3919 | // for comparison. |
| 3920 | if (isa<ArrayType>(T1) && T1Quals) |
| 3921 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3922 | if (isa<ArrayType>(T2) && T2Quals) |
| 3923 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3924 | |
| 3925 | // C++ [dcl.init.ref]p4: |
| 3926 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3927 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3928 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3929 | // overload resolution, cases for which cv1 is greater |
| 3930 | // cv-qualification than cv2 are identified as |
| 3931 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3932 | // |
| 3933 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3934 | // qualifiers when performing these computations, so that e.g., an int in |
| 3935 | // address space 1 is not reference-compatible with an int in address |
| 3936 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3937 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3938 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3939 | T1Quals.removeObjCLifetime(); |
| 3940 | T2Quals.removeObjCLifetime(); |
| 3941 | ObjCLifetimeConversion = true; |
| 3942 | } |
| 3943 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3944 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3945 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3946 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3947 | return Ref_Compatible_With_Added_Qualification; |
| 3948 | else |
| 3949 | return Ref_Related; |
| 3950 | } |
| 3951 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3952 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3953 | /// with DeclType. Return true if something definite is found. |
| 3954 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3955 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3956 | QualType DeclType, SourceLocation DeclLoc, |
| 3957 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3958 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3959 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3960 | CXXRecordDecl *T2RecordDecl |
| 3961 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3962 | |
| 3963 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3964 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3965 | CXXRecordDecl::conversion_iterator> |
| 3966 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3967 | for (CXXRecordDecl::conversion_iterator |
| 3968 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3969 | NamedDecl *D = *I; |
| 3970 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3971 | if (isa<UsingShadowDecl>(D)) |
| 3972 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3973 | |
| 3974 | FunctionTemplateDecl *ConvTemplate |
| 3975 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3976 | CXXConversionDecl *Conv; |
| 3977 | if (ConvTemplate) |
| 3978 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3979 | else |
| 3980 | Conv = cast<CXXConversionDecl>(D); |
| 3981 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | // If this is an explicit conversion, and we're not allowed to consider |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3983 | // explicit conversions, skip it. |
| 3984 | if (!AllowExplicit && Conv->isExplicit()) |
| 3985 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3987 | if (AllowRvalues) { |
| 3988 | bool DerivedToBase = false; |
| 3989 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3990 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 3991 | |
| 3992 | // If we are initializing an rvalue reference, don't permit conversion |
| 3993 | // functions that return lvalues. |
| 3994 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 3995 | const ReferenceType *RefType |
| 3996 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 3997 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 3998 | continue; |
| 3999 | } |
| 4000 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4001 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4002 | S.CompareReferenceRelationship( |
| 4003 | DeclLoc, |
| 4004 | Conv->getConversionType().getNonReferenceType() |
| 4005 | .getUnqualifiedType(), |
| 4006 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4007 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4008 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4009 | continue; |
| 4010 | } else { |
| 4011 | // If the conversion function doesn't return a reference type, |
| 4012 | // it can't be considered for this conversion. An rvalue reference |
| 4013 | // is only acceptable if its referencee is a function type. |
| 4014 | |
| 4015 | const ReferenceType *RefType = |
| 4016 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4017 | if (!RefType || |
| 4018 | (!RefType->isLValueReferenceType() && |
| 4019 | !RefType->getPointeeType()->isFunctionType())) |
| 4020 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4021 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4023 | if (ConvTemplate) |
| 4024 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4025 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4026 | else |
| 4027 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4028 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4031 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4032 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4033 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4034 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4035 | case OR_Success: |
| 4036 | // C++ [over.ics.ref]p1: |
| 4037 | // |
| 4038 | // [...] If the parameter binds directly to the result of |
| 4039 | // applying a conversion function to the argument |
| 4040 | // expression, the implicit conversion sequence is a |
| 4041 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4042 | // second standard conversion sequence either an identity |
| 4043 | // conversion or, if the conversion function returns an |
| 4044 | // entity of a type that is a derived class of the parameter |
| 4045 | // type, a derived-to-base Conversion. |
| 4046 | if (!Best->FinalConversion.DirectBinding) |
| 4047 | return false; |
| 4048 | |
| 4049 | ICS.setUserDefined(); |
| 4050 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4051 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4052 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4053 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4054 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4055 | ICS.UserDefined.EllipsisConversion = false; |
| 4056 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4057 | ICS.UserDefined.After.DirectBinding && |
| 4058 | "Expected a direct reference binding!"); |
| 4059 | return true; |
| 4060 | |
| 4061 | case OR_Ambiguous: |
| 4062 | ICS.setAmbiguous(); |
| 4063 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4064 | Cand != CandidateSet.end(); ++Cand) |
| 4065 | if (Cand->Viable) |
| 4066 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4067 | return true; |
| 4068 | |
| 4069 | case OR_No_Viable_Function: |
| 4070 | case OR_Deleted: |
| 4071 | // There was no suitable conversion, or we found a deleted |
| 4072 | // conversion; continue with other checks. |
| 4073 | return false; |
| 4074 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4075 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4076 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4079 | /// \brief Compute an implicit conversion sequence for reference |
| 4080 | /// initialization. |
| 4081 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4082 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4083 | SourceLocation DeclLoc, |
| 4084 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4085 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4086 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4087 | |
| 4088 | // Most paths end in a failed conversion. |
| 4089 | ImplicitConversionSequence ICS; |
| 4090 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4091 | |
| 4092 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4093 | QualType T2 = Init->getType(); |
| 4094 | |
| 4095 | // If the initializer is the address of an overloaded function, try |
| 4096 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4097 | // type of the resulting function. |
| 4098 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4099 | DeclAccessPair Found; |
| 4100 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4101 | false, Found)) |
| 4102 | T2 = Fn->getType(); |
| 4103 | } |
| 4104 | |
| 4105 | // Compute some basic properties of the types and the initializer. |
| 4106 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4107 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4108 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4109 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4110 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4111 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4112 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4113 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4115 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4116 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4117 | // A reference to type "cv1 T1" is initialized by an expression |
| 4118 | // of type "cv2 T2" as follows: |
| 4119 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4120 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4121 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4122 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4123 | // reference-compatible with "cv2 T2," or |
| 4124 | // |
| 4125 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4126 | if (InitCategory.isLValue() && |
| 4127 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4128 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4129 | // When a parameter of reference type binds directly (8.5.3) |
| 4130 | // to an argument expression, the implicit conversion sequence |
| 4131 | // is the identity conversion, unless the argument expression |
| 4132 | // has a type that is a derived class of the parameter type, |
| 4133 | // in which case the implicit conversion sequence is a |
| 4134 | // derived-to-base Conversion (13.3.3.1). |
| 4135 | ICS.setStandard(); |
| 4136 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4137 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4138 | : ObjCConversion? ICK_Compatible_Conversion |
| 4139 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4140 | ICS.Standard.Third = ICK_Identity; |
| 4141 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4142 | ICS.Standard.setToType(0, T2); |
| 4143 | ICS.Standard.setToType(1, T1); |
| 4144 | ICS.Standard.setToType(2, T1); |
| 4145 | ICS.Standard.ReferenceBinding = true; |
| 4146 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4147 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4148 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4149 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4150 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4151 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4152 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4153 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4154 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4155 | // derived-to-base conversions is suppressed when we're |
| 4156 | // computing the implicit conversion sequence (C++ |
| 4157 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4158 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4159 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4160 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4161 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4162 | // not reference-related to T2, and can be implicitly |
| 4163 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4164 | // is reference-compatible with "cv3 T3" 92) (this |
| 4165 | // conversion is selected by enumerating the applicable |
| 4166 | // conversion functions (13.3.1.6) and choosing the best |
| 4167 | // one through overload resolution (13.3)), |
| 4168 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4169 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4170 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4171 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4172 | Init, T2, /*AllowRvalues=*/false, |
| 4173 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4174 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4178 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4179 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4180 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4182 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4183 | // point, which is that, due to p2 (which short-circuits reference |
| 4184 | // binding by only attempting a simple conversion for non-direct |
| 4185 | // bindings) and p3's strange wording, we allow a const volatile |
| 4186 | // reference to bind to an rvalue. Hence the check for the presence |
| 4187 | // of "const" rather than checking for "const" being the only |
| 4188 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4189 | // This is also the point where rvalue references and lvalue inits no longer |
| 4190 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4191 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4192 | return ICS; |
| 4193 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4194 | // -- If the initializer expression |
| 4195 | // |
| 4196 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4197 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4198 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4199 | (InitCategory.isXValue() || |
| 4200 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4201 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4202 | ICS.setStandard(); |
| 4203 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4205 | : ObjCConversion? ICK_Compatible_Conversion |
| 4206 | : ICK_Identity; |
| 4207 | ICS.Standard.Third = ICK_Identity; |
| 4208 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4209 | ICS.Standard.setToType(0, T2); |
| 4210 | ICS.Standard.setToType(1, T1); |
| 4211 | ICS.Standard.setToType(2, T1); |
| 4212 | ICS.Standard.ReferenceBinding = true; |
| 4213 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4214 | // binding unless we're binding to a class prvalue. |
| 4215 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4216 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4217 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | ICS.Standard.DirectBinding = |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4219 | S.getLangOpts().CPlusPlus0x || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4220 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4221 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4222 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4224 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4225 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4226 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4228 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4230 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4231 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4232 | // an xvalue, class prvalue, or function lvalue of type |
| 4233 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4234 | // "cv3 T3", |
| 4235 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4236 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4237 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4238 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4239 | // class subobject). |
| 4240 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4241 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4242 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4243 | Init, T2, /*AllowRvalues=*/true, |
| 4244 | AllowExplicit)) { |
| 4245 | // In the second case, if the reference is an rvalue reference |
| 4246 | // and the second standard conversion sequence of the |
| 4247 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4248 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4250 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4251 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4252 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4253 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4254 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4256 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4257 | // initialized from the initializer expression using the |
| 4258 | // rules for a non-reference copy initialization (8.5). The |
| 4259 | // reference is then bound to the temporary. If T1 is |
| 4260 | // reference-related to T2, cv1 must be the same |
| 4261 | // cv-qualification as, or greater cv-qualification than, |
| 4262 | // cv2; otherwise, the program is ill-formed. |
| 4263 | if (RefRelationship == Sema::Ref_Related) { |
| 4264 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4265 | // we would be reference-compatible or reference-compatible with |
| 4266 | // added qualification. But that wasn't the case, so the reference |
| 4267 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4268 | // |
| 4269 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4270 | // ObjC GC and lifetime qualifiers aren't important. |
| 4271 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4272 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4273 | T1Quals.removeObjCGCAttr(); |
| 4274 | T1Quals.removeObjCLifetime(); |
| 4275 | T2Quals.removeObjCGCAttr(); |
| 4276 | T2Quals.removeObjCLifetime(); |
| 4277 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4278 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | // If at least one of the types is a class type, the types are not |
| 4282 | // related, and we aren't allowed any user conversions, the |
| 4283 | // reference binding fails. This case is important for breaking |
| 4284 | // recursion, since TryImplicitConversion below will attempt to |
| 4285 | // create a temporary through the use of a copy constructor. |
| 4286 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4287 | (T1->isRecordType() || T2->isRecordType())) |
| 4288 | return ICS; |
| 4289 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4290 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4291 | // reference, the initializer expression shall not be an lvalue. |
| 4292 | if (RefRelationship >= Sema::Ref_Related && |
| 4293 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4294 | return ICS; |
| 4295 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4296 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4297 | // When a parameter of reference type is not bound directly to |
| 4298 | // an argument expression, the conversion sequence is the one |
| 4299 | // required to convert the argument expression to the |
| 4300 | // underlying type of the reference according to |
| 4301 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4302 | // to copy-initializing a temporary of the underlying type with |
| 4303 | // the argument expression. Any difference in top-level |
| 4304 | // cv-qualification is subsumed by the initialization itself |
| 4305 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4306 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4307 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4308 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4309 | /*CStyle=*/false, |
| 4310 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4311 | |
| 4312 | // Of course, that's still a reference binding. |
| 4313 | if (ICS.isStandard()) { |
| 4314 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4315 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4316 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4317 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4318 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4319 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4320 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4321 | // Don't allow rvalue references to bind to lvalues. |
| 4322 | if (DeclType->isRValueReferenceType()) { |
| 4323 | if (const ReferenceType *RefType |
| 4324 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4325 | ->getAs<LValueReferenceType>()) { |
| 4326 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4327 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4328 | DeclType); |
| 4329 | return ICS; |
| 4330 | } |
| 4331 | } |
| 4332 | } |
| 4333 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4334 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4335 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4336 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4337 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4338 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4339 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4340 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4341 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4342 | return ICS; |
| 4343 | } |
| 4344 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4345 | static ImplicitConversionSequence |
| 4346 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4347 | bool SuppressUserConversions, |
| 4348 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4349 | bool AllowObjCWritebackConversion, |
| 4350 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4351 | |
| 4352 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4353 | /// initializer list From. |
| 4354 | static ImplicitConversionSequence |
| 4355 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4356 | bool SuppressUserConversions, |
| 4357 | bool InOverloadResolution, |
| 4358 | bool AllowObjCWritebackConversion) { |
| 4359 | // C++11 [over.ics.list]p1: |
| 4360 | // When an argument is an initializer list, it is not an expression and |
| 4361 | // special rules apply for converting it to a parameter type. |
| 4362 | |
| 4363 | ImplicitConversionSequence Result; |
| 4364 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4365 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4366 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4367 | // We need a complete type for what follows. Incomplete types can never be |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4368 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4369 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4370 | return Result; |
| 4371 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4372 | // C++11 [over.ics.list]p2: |
| 4373 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4374 | // all the elements can be implicitly converted to X, the implicit |
| 4375 | // conversion sequence is the worst conversion necessary to convert an |
| 4376 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4377 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4378 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4379 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4380 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4381 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4382 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4383 | if (!X.isNull()) { |
| 4384 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4385 | Expr *Init = From->getInit(i); |
| 4386 | ImplicitConversionSequence ICS = |
| 4387 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4388 | InOverloadResolution, |
| 4389 | AllowObjCWritebackConversion); |
| 4390 | // If a single element isn't convertible, fail. |
| 4391 | if (ICS.isBad()) { |
| 4392 | Result = ICS; |
| 4393 | break; |
| 4394 | } |
| 4395 | // Otherwise, look for the worst conversion. |
| 4396 | if (Result.isBad() || |
| 4397 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4398 | ImplicitConversionSequence::Worse) |
| 4399 | Result = ICS; |
| 4400 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4401 | |
| 4402 | // For an empty list, we won't have computed any conversion sequence. |
| 4403 | // Introduce the identity conversion sequence. |
| 4404 | if (From->getNumInits() == 0) { |
| 4405 | Result.setStandard(); |
| 4406 | Result.Standard.setAsIdentityConversion(); |
| 4407 | Result.Standard.setFromType(ToType); |
| 4408 | Result.Standard.setAllToTypes(ToType); |
| 4409 | } |
| 4410 | |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4411 | Result.setListInitializationSequence(); |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4412 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4413 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4414 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4415 | |
| 4416 | // C++11 [over.ics.list]p3: |
| 4417 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4418 | // resolution chooses a single best constructor [...] the implicit |
| 4419 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4420 | // constructors are viable but none is better than the others, the |
| 4421 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4422 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4423 | // This function can deal with initializer lists. |
| 4424 | Result = TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4425 | /*AllowExplicit=*/false, |
| 4426 | InOverloadResolution, /*CStyle=*/false, |
| 4427 | AllowObjCWritebackConversion); |
| 4428 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4429 | return Result; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4430 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4431 | |
| 4432 | // C++11 [over.ics.list]p4: |
| 4433 | // Otherwise, if the parameter has an aggregate type which can be |
| 4434 | // initialized from the initializer list [...] the implicit conversion |
| 4435 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4436 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4437 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4438 | // down to checking whether the initialization works. |
| 4439 | // FIXME: Find out whether this parameter is consumed or not. |
| 4440 | InitializedEntity Entity = |
| 4441 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4442 | /*Consumed=*/false); |
| 4443 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4444 | Result.setUserDefined(); |
| 4445 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4446 | // Initializer lists don't have a type. |
| 4447 | Result.UserDefined.Before.setFromType(QualType()); |
| 4448 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4449 | |
| 4450 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4451 | Result.UserDefined.After.setFromType(ToType); |
| 4452 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4453 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4454 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4455 | return Result; |
| 4456 | } |
| 4457 | |
| 4458 | // C++11 [over.ics.list]p5: |
| 4459 | // Otherwise, if the parameter is a reference, see 13.3.3.1.4. |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4460 | if (ToType->isReferenceType()) { |
| 4461 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4462 | // mention initializer lists in any way. So we go by what list- |
| 4463 | // initialization would do and try to extrapolate from that. |
| 4464 | |
| 4465 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4466 | |
| 4467 | // If the initializer list has a single element that is reference-related |
| 4468 | // to the parameter type, we initialize the reference from that. |
| 4469 | if (From->getNumInits() == 1) { |
| 4470 | Expr *Init = From->getInit(0); |
| 4471 | |
| 4472 | QualType T2 = Init->getType(); |
| 4473 | |
| 4474 | // If the initializer is the address of an overloaded function, try |
| 4475 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4476 | // type of the resulting function. |
| 4477 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4478 | DeclAccessPair Found; |
| 4479 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4480 | Init, ToType, false, Found)) |
| 4481 | T2 = Fn->getType(); |
| 4482 | } |
| 4483 | |
| 4484 | // Compute some basic properties of the types and the initializer. |
| 4485 | bool dummy1 = false; |
| 4486 | bool dummy2 = false; |
| 4487 | bool dummy3 = false; |
| 4488 | Sema::ReferenceCompareResult RefRelationship |
| 4489 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4490 | dummy2, dummy3); |
| 4491 | |
| 4492 | if (RefRelationship >= Sema::Ref_Related) |
| 4493 | return TryReferenceInit(S, Init, ToType, |
| 4494 | /*FIXME:*/From->getLocStart(), |
| 4495 | SuppressUserConversions, |
| 4496 | /*AllowExplicit=*/false); |
| 4497 | } |
| 4498 | |
| 4499 | // Otherwise, we bind the reference to a temporary created from the |
| 4500 | // initializer list. |
| 4501 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4502 | InOverloadResolution, |
| 4503 | AllowObjCWritebackConversion); |
| 4504 | if (Result.isFailure()) |
| 4505 | return Result; |
| 4506 | assert(!Result.isEllipsis() && |
| 4507 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4508 | |
| 4509 | // Can we even bind to a temporary? |
| 4510 | if (ToType->isRValueReferenceType() || |
| 4511 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4512 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4513 | Result.UserDefined.After; |
| 4514 | SCS.ReferenceBinding = true; |
| 4515 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4516 | SCS.BindsToRvalue = true; |
| 4517 | SCS.BindsToFunctionLvalue = false; |
| 4518 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4519 | SCS.ObjCLifetimeConversionBinding = false; |
| 4520 | } else |
| 4521 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4522 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4523 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4524 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4525 | |
| 4526 | // C++11 [over.ics.list]p6: |
| 4527 | // Otherwise, if the parameter type is not a class: |
| 4528 | if (!ToType->isRecordType()) { |
| 4529 | // - if the initializer list has one element, the implicit conversion |
| 4530 | // sequence is the one required to convert the element to the |
| 4531 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4532 | unsigned NumInits = From->getNumInits(); |
| 4533 | if (NumInits == 1) |
| 4534 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4535 | SuppressUserConversions, |
| 4536 | InOverloadResolution, |
| 4537 | AllowObjCWritebackConversion); |
| 4538 | // - if the initializer list has no elements, the implicit conversion |
| 4539 | // sequence is the identity conversion. |
| 4540 | else if (NumInits == 0) { |
| 4541 | Result.setStandard(); |
| 4542 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4543 | Result.Standard.setFromType(ToType); |
| 4544 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4545 | } |
Sebastian Redl | 2422e82 | 2012-02-28 23:36:38 +0000 | [diff] [blame] | 4546 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4547 | return Result; |
| 4548 | } |
| 4549 | |
| 4550 | // C++11 [over.ics.list]p7: |
| 4551 | // In all cases other than those enumerated above, no conversion is possible |
| 4552 | return Result; |
| 4553 | } |
| 4554 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4555 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4556 | /// ToType from the expression From. Return the implicit conversion |
| 4557 | /// sequence required to pass this argument, which may be a bad |
| 4558 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4559 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4560 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4561 | static ImplicitConversionSequence |
| 4562 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4564 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4565 | bool AllowObjCWritebackConversion, |
| 4566 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4567 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4568 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4569 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4570 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4571 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4572 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4573 | /*FIXME:*/From->getLocStart(), |
| 4574 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4575 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4576 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4577 | return TryImplicitConversion(S, From, ToType, |
| 4578 | SuppressUserConversions, |
| 4579 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4580 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4581 | /*CStyle=*/false, |
| 4582 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4585 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4586 | const CanQualType ToQTy, |
| 4587 | Sema &S, |
| 4588 | SourceLocation Loc, |
| 4589 | ExprValueKind FromVK) { |
| 4590 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4591 | ImplicitConversionSequence ICS = |
| 4592 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4593 | |
| 4594 | return !ICS.isBad(); |
| 4595 | } |
| 4596 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4597 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4598 | /// parameter of the given member function (@c Method) from the |
| 4599 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4600 | static ImplicitConversionSequence |
| 4601 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4602 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4603 | CXXMethodDecl *Method, |
| 4604 | CXXRecordDecl *ActingContext) { |
| 4605 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4606 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4607 | // const volatile object. |
| 4608 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4609 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4610 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4613 | // to exit early. |
| 4614 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4615 | |
| 4616 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4617 | QualType FromType = OrigFromType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4618 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4619 | FromType = PT->getPointeeType(); |
| 4620 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4621 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4622 | // better have an lvalue. |
| 4623 | assert(FromClassification.isLValue()); |
| 4624 | } |
| 4625 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4626 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4627 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4628 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4629 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4630 | // parameter is |
| 4631 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4632 | // - "lvalue reference to cv X" for functions declared without a |
| 4633 | // ref-qualifier or with the & ref-qualifier |
| 4634 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4635 | // ref-qualifier |
| 4636 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4637 | // where X is the class of which the function is a member and cv is the |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4638 | // cv-qualification on the member function declaration. |
| 4639 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4641 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4642 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4643 | // reference binding here, that allows class rvalues to bind to |
| 4644 | // non-constant references. |
| 4645 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4646 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4647 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4649 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4650 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4651 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 4652 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4653 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4654 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4655 | |
| 4656 | // Check that we have either the same type or a derived type. It |
| 4657 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4658 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4659 | ImplicitConversionKind SecondKind; |
| 4660 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4661 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4662 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4663 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4664 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4665 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4666 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4667 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4668 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4670 | // Check the ref-qualifier. |
| 4671 | switch (Method->getRefQualifier()) { |
| 4672 | case RQ_None: |
| 4673 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4674 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4676 | case RQ_LValue: |
| 4677 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4678 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4679 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4680 | ImplicitParamType); |
| 4681 | return ICS; |
| 4682 | } |
| 4683 | break; |
| 4684 | |
| 4685 | case RQ_RValue: |
| 4686 | if (!FromClassification.isRValue()) { |
| 4687 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4689 | ImplicitParamType); |
| 4690 | return ICS; |
| 4691 | } |
| 4692 | break; |
| 4693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4695 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4696 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4697 | ICS.Standard.setAsIdentityConversion(); |
| 4698 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4699 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4700 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4701 | ICS.Standard.ReferenceBinding = true; |
| 4702 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4703 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4704 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4705 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4706 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4707 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4708 | return ICS; |
| 4709 | } |
| 4710 | |
| 4711 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4712 | /// the implicit object parameter for the given Method with the given |
| 4713 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4714 | ExprResult |
| 4715 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4717 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4718 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4719 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4721 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4723 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4724 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4725 | FromRecordType = PT->getPointeeType(); |
| 4726 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4727 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4728 | } else { |
| 4729 | FromRecordType = From->getType(); |
| 4730 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4731 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4734 | // Note that we always use the true parent context when performing |
| 4735 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4737 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4738 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4739 | if (ICS.isBad()) { |
| 4740 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4741 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4742 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4743 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4744 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4745 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4746 | diag::err_member_function_call_bad_cvr) |
| 4747 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4748 | << From->getSourceRange(); |
| 4749 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4750 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4751 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4752 | } |
| 4753 | } |
| 4754 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4755 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4756 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4757 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4760 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4761 | ExprResult FromRes = |
| 4762 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4763 | if (FromRes.isInvalid()) |
| 4764 | return ExprError(); |
| 4765 | From = FromRes.take(); |
| 4766 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4767 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4768 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4769 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4770 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4771 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4774 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4775 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4776 | static ImplicitConversionSequence |
| 4777 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4778 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4779 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4780 | // FIXME: Are these flags correct? |
| 4781 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4782 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4783 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4784 | /*CStyle=*/false, |
| 4785 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4789 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4790 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4791 | if (checkPlaceholderForOverload(*this, From)) |
| 4792 | return ExprError(); |
| 4793 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4794 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4795 | if (!ICS.isBad()) |
| 4796 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4797 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4798 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4799 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4800 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4801 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4802 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4803 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4804 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4805 | /// Check that the specified conversion is permitted in a converted constant |
| 4806 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4807 | /// is acceptable. |
| 4808 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4809 | StandardConversionSequence &SCS) { |
| 4810 | // Since we know that the target type is an integral or unscoped enumeration |
| 4811 | // type, most conversion kinds are impossible. All possible First and Third |
| 4812 | // conversions are fine. |
| 4813 | switch (SCS.Second) { |
| 4814 | case ICK_Identity: |
| 4815 | case ICK_Integral_Promotion: |
| 4816 | case ICK_Integral_Conversion: |
| 4817 | return true; |
| 4818 | |
| 4819 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4820 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4821 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4822 | // conversion, so it's permitted in a converted constant expression. |
| 4823 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4824 | SCS.getToType(2)->isBooleanType(); |
| 4825 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4826 | case ICK_Floating_Integral: |
| 4827 | case ICK_Complex_Real: |
| 4828 | return false; |
| 4829 | |
| 4830 | case ICK_Lvalue_To_Rvalue: |
| 4831 | case ICK_Array_To_Pointer: |
| 4832 | case ICK_Function_To_Pointer: |
| 4833 | case ICK_NoReturn_Adjustment: |
| 4834 | case ICK_Qualification: |
| 4835 | case ICK_Compatible_Conversion: |
| 4836 | case ICK_Vector_Conversion: |
| 4837 | case ICK_Vector_Splat: |
| 4838 | case ICK_Derived_To_Base: |
| 4839 | case ICK_Pointer_Conversion: |
| 4840 | case ICK_Pointer_Member: |
| 4841 | case ICK_Block_Pointer_Conversion: |
| 4842 | case ICK_Writeback_Conversion: |
| 4843 | case ICK_Floating_Promotion: |
| 4844 | case ICK_Complex_Promotion: |
| 4845 | case ICK_Complex_Conversion: |
| 4846 | case ICK_Floating_Conversion: |
| 4847 | case ICK_TransparentUnionConversion: |
| 4848 | llvm_unreachable("unexpected second conversion kind"); |
| 4849 | |
| 4850 | case ICK_Num_Conversion_Kinds: |
| 4851 | break; |
| 4852 | } |
| 4853 | |
| 4854 | llvm_unreachable("unknown conversion kind"); |
| 4855 | } |
| 4856 | |
| 4857 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4858 | /// converted constant expression of type T, perform the conversion and produce |
| 4859 | /// the converted expression, per C++11 [expr.const]p3. |
| 4860 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4861 | llvm::APSInt &Value, |
| 4862 | CCEKind CCE) { |
| 4863 | assert(LangOpts.CPlusPlus0x && "converted constant expression outside C++11"); |
| 4864 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4865 | |
| 4866 | if (checkPlaceholderForOverload(*this, From)) |
| 4867 | return ExprError(); |
| 4868 | |
| 4869 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4870 | // A converted constant expression of type T is a core constant expression, |
| 4871 | // implicitly converted to a prvalue of type T, where the converted |
| 4872 | // expression is a literal constant expression and the implicit conversion |
| 4873 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4874 | // conversions, integral promotions, and integral conversions other than |
| 4875 | // narrowing conversions. |
| 4876 | ImplicitConversionSequence ICS = |
| 4877 | TryImplicitConversion(From, T, |
| 4878 | /*SuppressUserConversions=*/false, |
| 4879 | /*AllowExplicit=*/false, |
| 4880 | /*InOverloadResolution=*/false, |
| 4881 | /*CStyle=*/false, |
| 4882 | /*AllowObjcWritebackConversion=*/false); |
| 4883 | StandardConversionSequence *SCS = 0; |
| 4884 | switch (ICS.getKind()) { |
| 4885 | case ImplicitConversionSequence::StandardConversion: |
| 4886 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4887 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4888 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4889 | << From->getType() << From->getSourceRange() << T; |
| 4890 | SCS = &ICS.Standard; |
| 4891 | break; |
| 4892 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4893 | // We are converting from class type to an integral or enumeration type, so |
| 4894 | // the Before sequence must be trivial. |
| 4895 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4896 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4897 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4898 | << From->getType() << From->getSourceRange() << T; |
| 4899 | SCS = &ICS.UserDefined.After; |
| 4900 | break; |
| 4901 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4902 | case ImplicitConversionSequence::BadConversion: |
| 4903 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4904 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4905 | diag::err_typecheck_converted_constant_expression) |
| 4906 | << From->getType() << From->getSourceRange() << T; |
| 4907 | return ExprError(); |
| 4908 | |
| 4909 | case ImplicitConversionSequence::EllipsisConversion: |
| 4910 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4911 | } |
| 4912 | |
| 4913 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4914 | if (Result.isInvalid()) |
| 4915 | return Result; |
| 4916 | |
| 4917 | // Check for a narrowing implicit conversion. |
| 4918 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4919 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4920 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4921 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4922 | case NK_Variable_Narrowing: |
| 4923 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4924 | // expression. We'll diagnose this in a moment. |
| 4925 | case NK_Not_Narrowing: |
| 4926 | break; |
| 4927 | |
| 4928 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4929 | Diag(From->getLocStart(), |
| 4930 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4931 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4932 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4933 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4934 | break; |
| 4935 | |
| 4936 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4937 | Diag(From->getLocStart(), |
| 4938 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4939 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4940 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4941 | break; |
| 4942 | } |
| 4943 | |
| 4944 | // Check the expression is a constant expression. |
| 4945 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 4946 | Expr::EvalResult Eval; |
| 4947 | Eval.Diag = &Notes; |
| 4948 | |
| 4949 | if (!Result.get()->EvaluateAsRValue(Eval, Context)) { |
| 4950 | // The expression can't be folded, so we can't keep it at this position in |
| 4951 | // the AST. |
| 4952 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4953 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4954 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4955 | |
| 4956 | if (Notes.empty()) { |
| 4957 | // It's a constant expression. |
| 4958 | return Result; |
| 4959 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 4963 | if (Notes.size() == 1 && |
| 4964 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 4965 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 4966 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4967 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4968 | << CCE << From->getSourceRange(); |
| 4969 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 4970 | Diag(Notes[I].first, Notes[I].second); |
| 4971 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4972 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4975 | /// dropPointerConversions - If the given standard conversion sequence |
| 4976 | /// involves any pointer conversions, remove them. This may change |
| 4977 | /// the result type of the conversion sequence. |
| 4978 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 4979 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 4980 | SCS.Second = ICK_Identity; |
| 4981 | SCS.Third = ICK_Identity; |
| 4982 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 4983 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4984 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4985 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4986 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 4987 | /// convert the expression From to an Objective-C pointer type. |
| 4988 | static ImplicitConversionSequence |
| 4989 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 4990 | // Do an implicit conversion to 'id'. |
| 4991 | QualType Ty = S.Context.getObjCIdType(); |
| 4992 | ImplicitConversionSequence ICS |
| 4993 | = TryImplicitConversion(S, From, Ty, |
| 4994 | // FIXME: Are these flags correct? |
| 4995 | /*SuppressUserConversions=*/false, |
| 4996 | /*AllowExplicit=*/true, |
| 4997 | /*InOverloadResolution=*/false, |
| 4998 | /*CStyle=*/false, |
| 4999 | /*AllowObjCWritebackConversion=*/false); |
| 5000 | |
| 5001 | // Strip off any final conversions to 'id'. |
| 5002 | switch (ICS.getKind()) { |
| 5003 | case ImplicitConversionSequence::BadConversion: |
| 5004 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5005 | case ImplicitConversionSequence::EllipsisConversion: |
| 5006 | break; |
| 5007 | |
| 5008 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5009 | dropPointerConversion(ICS.UserDefined.After); |
| 5010 | break; |
| 5011 | |
| 5012 | case ImplicitConversionSequence::StandardConversion: |
| 5013 | dropPointerConversion(ICS.Standard); |
| 5014 | break; |
| 5015 | } |
| 5016 | |
| 5017 | return ICS; |
| 5018 | } |
| 5019 | |
| 5020 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5021 | /// conversion of the expression From to an Objective-C pointer type. |
| 5022 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5023 | if (checkPlaceholderForOverload(*this, From)) |
| 5024 | return ExprError(); |
| 5025 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5026 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5027 | ImplicitConversionSequence ICS = |
| 5028 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5029 | if (!ICS.isBad()) |
| 5030 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5031 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5032 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5033 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5034 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5035 | /// type of a permitted flavor. |
| 5036 | static bool isIntegralOrEnumerationType(QualType T, bool AllowScopedEnum) { |
| 5037 | return AllowScopedEnum ? T->isIntegralOrEnumerationType() |
| 5038 | : T->isIntegralOrUnscopedEnumerationType(); |
| 5039 | } |
| 5040 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5042 | /// enumeration type. |
| 5043 | /// |
| 5044 | /// This routine will attempt to convert an expression of class type to an |
| 5045 | /// integral or enumeration type, if that class type only has a single |
| 5046 | /// conversion to an integral or enumeration type. |
| 5047 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5048 | /// \param Loc The source location of the construct that requires the |
| 5049 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5050 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5051 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5052 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5053 | /// \param Diagnoser Used to output any diagnostics. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5054 | /// |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5055 | /// \param AllowScopedEnumerations Specifies whether conversions to scoped |
| 5056 | /// enumerations should be considered. |
| 5057 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5058 | /// \returns The expression, converted to an integral or enumeration type if |
| 5059 | /// successful. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5060 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5061 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5062 | ICEConvertDiagnoser &Diagnoser, |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5063 | bool AllowScopedEnumerations) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5064 | // We can't perform any more checking for type-dependent expressions. |
| 5065 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5066 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5067 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5068 | // Process placeholders immediately. |
| 5069 | if (From->hasPlaceholderType()) { |
| 5070 | ExprResult result = CheckPlaceholderExpr(From); |
| 5071 | if (result.isInvalid()) return result; |
| 5072 | From = result.take(); |
| 5073 | } |
| 5074 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5075 | // If the expression already has integral or enumeration type, we're golden. |
| 5076 | QualType T = From->getType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5077 | if (isIntegralOrEnumerationType(T, AllowScopedEnumerations)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5078 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5079 | |
| 5080 | // FIXME: Check for missing '()' if T is a function type? |
| 5081 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | // If we don't have a class type in C++, there's no way we can get an |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5083 | // expression of integral or enumeration type. |
| 5084 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5085 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5086 | if (!Diagnoser.Suppress) |
| 5087 | Diagnoser.diagnoseNotInt(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5088 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5089 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5090 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5091 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5092 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5093 | ICEConvertDiagnoser &Diagnoser; |
| 5094 | Expr *From; |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5096 | TypeDiagnoserPartialDiag(ICEConvertDiagnoser &Diagnoser, Expr *From) |
| 5097 | : TypeDiagnoser(Diagnoser.Suppress), Diagnoser(Diagnoser), From(From) {} |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5098 | |
| 5099 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5100 | Diagnoser.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5101 | } |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5102 | } IncompleteDiagnoser(Diagnoser, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5103 | |
| 5104 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5105 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5106 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5107 | // Look for a conversion to an integral or enumeration type. |
| 5108 | UnresolvedSet<4> ViableConversions; |
| 5109 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5110 | std::pair<CXXRecordDecl::conversion_iterator, |
| 5111 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5112 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5113 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5114 | bool HadMultipleCandidates |
| 5115 | = (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5116 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5117 | for (CXXRecordDecl::conversion_iterator |
| 5118 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5119 | if (CXXConversionDecl *Conversion |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5120 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) { |
| 5121 | if (isIntegralOrEnumerationType( |
| 5122 | Conversion->getConversionType().getNonReferenceType(), |
| 5123 | AllowScopedEnumerations)) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5124 | if (Conversion->isExplicit()) |
| 5125 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 5126 | else |
| 5127 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 5128 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5129 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5130 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5131 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5132 | switch (ViableConversions.size()) { |
| 5133 | case 0: |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5134 | if (ExplicitConversions.size() == 1 && !Diagnoser.Suppress) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5135 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5136 | CXXConversionDecl *Conversion |
| 5137 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5138 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5139 | // The user probably meant to invoke the given explicit |
| 5140 | // conversion; use it. |
| 5141 | QualType ConvTy |
| 5142 | = Conversion->getConversionType().getNonReferenceType(); |
| 5143 | std::string TypeStr; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5144 | ConvTy.getAsStringInternal(TypeStr, getPrintingPolicy()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5146 | Diagnoser.diagnoseExplicitConv(*this, Loc, T, ConvTy) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5147 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5148 | "static_cast<" + TypeStr + ">(") |
| 5149 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 5150 | ")"); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5151 | Diagnoser.noteExplicitConv(*this, Conversion, ConvTy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | |
| 5153 | // If we aren't in a SFINAE context, build a call to the |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5154 | // explicit conversion function. |
| 5155 | if (isSFINAEContext()) |
| 5156 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5157 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5158 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5159 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5160 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 5161 | if (Result.isInvalid()) |
| 5162 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5163 | // Record usage of conversion in an implicit cast. |
| 5164 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 5165 | CK_UserDefinedConversion, |
| 5166 | Result.get(), 0, |
| 5167 | Result.get()->getValueKind()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5168 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5170 | // We'll complain below about a non-integral condition type. |
| 5171 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5172 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5173 | case 1: { |
| 5174 | // Apply this conversion. |
| 5175 | DeclAccessPair Found = ViableConversions[0]; |
| 5176 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5177 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5178 | CXXConversionDecl *Conversion |
| 5179 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5180 | QualType ConvTy |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5182 | if (!Diagnoser.SuppressConversion) { |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5183 | if (isSFINAEContext()) |
| 5184 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5185 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5186 | Diagnoser.diagnoseConversion(*this, Loc, T, ConvTy) |
| 5187 | << From->getSourceRange(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5188 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5189 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5190 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5191 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 5192 | if (Result.isInvalid()) |
| 5193 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5194 | // Record usage of conversion in an implicit cast. |
| 5195 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 5196 | CK_UserDefinedConversion, |
| 5197 | Result.get(), 0, |
| 5198 | Result.get()->getValueKind()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5199 | break; |
| 5200 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5202 | default: |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5203 | if (Diagnoser.Suppress) |
| 5204 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5205 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5206 | Diagnoser.diagnoseAmbiguous(*this, Loc, T) << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5207 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5208 | CXXConversionDecl *Conv |
| 5209 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5210 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5211 | Diagnoser.noteAmbiguous(*this, Conv, ConvTy); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5212 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5213 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5214 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5215 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5216 | if (!isIntegralOrEnumerationType(From->getType(), AllowScopedEnumerations) && |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5217 | !Diagnoser.Suppress) { |
| 5218 | Diagnoser.diagnoseNotInt(*this, Loc, From->getType()) |
| 5219 | << From->getSourceRange(); |
| 5220 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5221 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5222 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5225 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5226 | /// candidate functions, using the given function call arguments. If |
| 5227 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5228 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5229 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5230 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5231 | /// based on an incomplete set of function arguments. This feature is used by |
| 5232 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5233 | void |
| 5234 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5235 | DeclAccessPair FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5236 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5237 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5238 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5239 | bool PartialOverloading, |
| 5240 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5242 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5243 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5244 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5245 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5247 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5248 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5249 | // If we get here, it's because we're calling a member function |
| 5250 | // that is named without a member access expression (e.g., |
| 5251 | // "this->f") that was either written explicitly or created |
| 5252 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5253 | // function, e.g., X::f(). We use an empty type for the implied |
| 5254 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5255 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5256 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5258 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5259 | return; |
| 5260 | } |
| 5261 | // We treat a constructor like a non-member function, since its object |
| 5262 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5265 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5266 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5268 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5269 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5270 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5271 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5272 | // C++ [class.copy]p3: |
| 5273 | // A member function template is never instantiated to perform the copy |
| 5274 | // of a class object to an object of its class type. |
| 5275 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5276 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5277 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5278 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5279 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5280 | return; |
| 5281 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5283 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5284 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5285 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5286 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5287 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5288 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5289 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5290 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5292 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5293 | |
| 5294 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5295 | // parameters is viable only if it has an ellipsis in its parameter |
| 5296 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5297 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5298 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5299 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5300 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5301 | return; |
| 5302 | } |
| 5303 | |
| 5304 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5305 | // is viable only if the (m+1)st parameter has a default argument |
| 5306 | // (8.3.6). For the purposes of overload resolution, the |
| 5307 | // parameter list is truncated on the right, so that there are |
| 5308 | // exactly m parameters. |
| 5309 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5310 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5311 | // Not enough arguments. |
| 5312 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5313 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5314 | return; |
| 5315 | } |
| 5316 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5317 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5318 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5319 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5320 | if (CheckCUDATarget(Caller, Function)) { |
| 5321 | Candidate.Viable = false; |
| 5322 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5323 | return; |
| 5324 | } |
| 5325 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5326 | // Determine the implicit conversion sequences for each of the |
| 5327 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5328 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5329 | if (ArgIdx < NumArgsInProto) { |
| 5330 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5331 | // exist for each argument an implicit conversion sequence |
| 5332 | // (13.3.3.1) that converts that argument to the corresponding |
| 5333 | // parameter of F. |
| 5334 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5336 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5338 | /*InOverloadResolution=*/true, |
| 5339 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5340 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5341 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5342 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5343 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5344 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5345 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5346 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5347 | } else { |
| 5348 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5349 | // argument for which there is no corresponding parameter is |
| 5350 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5351 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5352 | } |
| 5353 | } |
| 5354 | } |
| 5355 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5356 | /// \brief Add all of the function declarations in the given function set to |
| 5357 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5358 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5359 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5360 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5361 | bool SuppressUserConversions, |
| 5362 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5363 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5364 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5365 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5366 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5367 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5368 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5369 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5370 | Args.slice(1), CandidateSet, |
| 5371 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5372 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5373 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5374 | SuppressUserConversions); |
| 5375 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5376 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5377 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5378 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5379 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5380 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5381 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5383 | Args[0]->Classify(Context), Args.slice(1), |
| 5384 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5385 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5386 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5387 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5388 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5389 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5390 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5393 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5394 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5395 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5396 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5397 | Expr::Classification ObjectClassification, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5398 | Expr **Args, unsigned NumArgs, |
| 5399 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5400 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5401 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5402 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5403 | |
| 5404 | if (isa<UsingShadowDecl>(Decl)) |
| 5405 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5407 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5408 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5409 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5410 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5411 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5412 | ObjectType, ObjectClassification, |
| 5413 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5414 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5415 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5416 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5417 | ObjectType, ObjectClassification, |
| 5418 | llvm::makeArrayRef(Args, NumArgs), |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5419 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5423 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5424 | /// of candidate functions, using the given function call arguments |
| 5425 | /// and the object argument (@c Object). For example, in a call |
| 5426 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5427 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5428 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5429 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5430 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5431 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5432 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5433 | Expr::Classification ObjectClassification, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5434 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5435 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5436 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5437 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5438 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5439 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5440 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5441 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5442 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5443 | if (!CandidateSet.isNewCandidate(Method)) |
| 5444 | return; |
| 5445 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5446 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5447 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5448 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5449 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5450 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5451 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5452 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5453 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5454 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5455 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5456 | |
| 5457 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5458 | |
| 5459 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5460 | // parameters is viable only if it has an ellipsis in its parameter |
| 5461 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5462 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5463 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5464 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5465 | return; |
| 5466 | } |
| 5467 | |
| 5468 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5469 | // is viable only if the (m+1)st parameter has a default argument |
| 5470 | // (8.3.6). For the purposes of overload resolution, the |
| 5471 | // parameter list is truncated on the right, so that there are |
| 5472 | // exactly m parameters. |
| 5473 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5474 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5475 | // Not enough arguments. |
| 5476 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5477 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5478 | return; |
| 5479 | } |
| 5480 | |
| 5481 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5482 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5483 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5484 | // The implicit object argument is ignored. |
| 5485 | Candidate.IgnoreObjectArgument = true; |
| 5486 | else { |
| 5487 | // Determine the implicit conversion sequence for the object |
| 5488 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5489 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5490 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5491 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5492 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5493 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5494 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5495 | return; |
| 5496 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
| 5499 | // Determine the implicit conversion sequences for each of the |
| 5500 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5501 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5502 | if (ArgIdx < NumArgsInProto) { |
| 5503 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5504 | // exist for each argument an implicit conversion sequence |
| 5505 | // (13.3.3.1) that converts that argument to the corresponding |
| 5506 | // parameter of F. |
| 5507 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5508 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5509 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5510 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5511 | /*InOverloadResolution=*/true, |
| 5512 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5513 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5514 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5515 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5516 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5517 | break; |
| 5518 | } |
| 5519 | } else { |
| 5520 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5521 | // argument for which there is no corresponding parameter is |
| 5522 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5523 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5524 | } |
| 5525 | } |
| 5526 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5527 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5528 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5529 | /// set, using template argument deduction to produce an appropriate member |
| 5530 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5531 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5532 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5533 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5534 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5535 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5536 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5537 | Expr::Classification ObjectClassification, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5538 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5539 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5540 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5541 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5542 | return; |
| 5543 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5544 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5545 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5546 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5547 | // 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] | 5548 | // candidate functions in the usual way.113) A given name can refer to one |
| 5549 | // or more function templates and also to a set of overloaded non-template |
| 5550 | // functions. In such a case, the candidate functions generated from each |
| 5551 | // function template are combined with the set of non-template candidate |
| 5552 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5553 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5554 | FunctionDecl *Specialization = 0; |
| 5555 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5556 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5557 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5558 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5559 | Candidate.FoundDecl = FoundDecl; |
| 5560 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5561 | Candidate.Viable = false; |
| 5562 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5563 | Candidate.IsSurrogate = false; |
| 5564 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5565 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5566 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5567 | Info); |
| 5568 | return; |
| 5569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5570 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5571 | // Add the function template specialization produced by template argument |
| 5572 | // deduction as a candidate. |
| 5573 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5574 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5575 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5576 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5577 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5578 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5581 | /// \brief Add a C++ function template specialization as a candidate |
| 5582 | /// in the candidate set, using template argument deduction to produce |
| 5583 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5584 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5585 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5586 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5587 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5588 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5589 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5590 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5591 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5592 | return; |
| 5593 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5594 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5595 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5596 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5597 | // 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] | 5598 | // candidate functions in the usual way.113) A given name can refer to one |
| 5599 | // or more function templates and also to a set of overloaded non-template |
| 5600 | // functions. In such a case, the candidate functions generated from each |
| 5601 | // function template are combined with the set of non-template candidate |
| 5602 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5603 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5604 | FunctionDecl *Specialization = 0; |
| 5605 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5606 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5607 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5608 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5609 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5610 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5611 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5612 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5613 | Candidate.IsSurrogate = false; |
| 5614 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5615 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5616 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5617 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5618 | return; |
| 5619 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5621 | // Add the function template specialization produced by template argument |
| 5622 | // deduction as a candidate. |
| 5623 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5624 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5625 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5628 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5629 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5630 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5631 | /// 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] | 5632 | /// (which may or may not be the same type as the type that the |
| 5633 | /// conversion function produces). |
| 5634 | void |
| 5635 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5636 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5637 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5638 | Expr *From, QualType ToType, |
| 5639 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5640 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5641 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5642 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5643 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5644 | return; |
| 5645 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5646 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5647 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5648 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5649 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5650 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5651 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5652 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5653 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5654 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5655 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5656 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5657 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5658 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5659 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5660 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5661 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5662 | // For conversion functions, the function is considered to be a member of |
| 5663 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5664 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5665 | // |
| 5666 | // Determine the implicit conversion sequence for the implicit |
| 5667 | // object parameter. |
| 5668 | QualType ImplicitParamType = From->getType(); |
| 5669 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5670 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5671 | CXXRecordDecl *ConversionContext |
| 5672 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5673 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5674 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5675 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5676 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5677 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5678 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5679 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5680 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5681 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5682 | return; |
| 5683 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5684 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5685 | // We won't go through a user-define type conversion function to convert a |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5686 | // derived to base as such conversions are given Conversion Rank. They only |
| 5687 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5688 | QualType FromCanon |
| 5689 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5690 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5691 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5692 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5693 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5694 | return; |
| 5695 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5696 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5697 | // To determine what the conversion from the result of calling the |
| 5698 | // conversion function to the type we're eventually trying to |
| 5699 | // convert to (ToType), we need to synthesize a call to the |
| 5700 | // conversion function and attempt copy initialization from it. This |
| 5701 | // makes sure that we get the right semantics with respect to |
| 5702 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5703 | // call on the stack and we don't need its arguments to be |
| 5704 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5705 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5706 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5707 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5708 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5709 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5710 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5711 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5712 | QualType ConversionType = Conversion->getConversionType(); |
| 5713 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5714 | Candidate.Viable = false; |
| 5715 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5716 | return; |
| 5717 | } |
| 5718 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5719 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5720 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5721 | // 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] | 5722 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5723 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5724 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5725 | CallExpr Call(Context, &ConversionFn, MultiExprArg(), CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5726 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5727 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5728 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5729 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5730 | /*InOverloadResolution=*/false, |
| 5731 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5732 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5733 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5734 | case ImplicitConversionSequence::StandardConversion: |
| 5735 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5736 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5737 | // C++ [over.ics.user]p3: |
| 5738 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5739 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5740 | // shall have exact match rank. |
| 5741 | if (Conversion->getPrimaryTemplate() && |
| 5742 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5743 | Candidate.Viable = false; |
| 5744 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5747 | // C++0x [dcl.init.ref]p5: |
| 5748 | // In the second case, if the reference is an rvalue reference and |
| 5749 | // the second standard conversion sequence of the user-defined |
| 5750 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5751 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5752 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5753 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5754 | Candidate.Viable = false; |
| 5755 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5756 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5757 | break; |
| 5758 | |
| 5759 | case ImplicitConversionSequence::BadConversion: |
| 5760 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5761 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5762 | break; |
| 5763 | |
| 5764 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5765 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5766 | "Can only end up with a standard conversion sequence or failure"); |
| 5767 | } |
| 5768 | } |
| 5769 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5770 | /// \brief Adds a conversion function template specialization |
| 5771 | /// candidate to the overload set, using template argument deduction |
| 5772 | /// to deduce the template arguments of the conversion function |
| 5773 | /// template from the type that we are converting to (C++ |
| 5774 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5776 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5777 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5778 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5779 | Expr *From, QualType ToType, |
| 5780 | OverloadCandidateSet &CandidateSet) { |
| 5781 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5782 | "Only conversion function templates permitted here"); |
| 5783 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5784 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5785 | return; |
| 5786 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5787 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5788 | CXXConversionDecl *Specialization = 0; |
| 5789 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5790 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5791 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5792 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5793 | Candidate.FoundDecl = FoundDecl; |
| 5794 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5795 | Candidate.Viable = false; |
| 5796 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5797 | Candidate.IsSurrogate = false; |
| 5798 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5799 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5800 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5801 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5802 | return; |
| 5803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5804 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5805 | // Add the conversion function template specialization produced by |
| 5806 | // template argument deduction as a candidate. |
| 5807 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5808 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5809 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5812 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 5813 | /// converts the given @c Object to a function pointer via the |
| 5814 | /// conversion function @c Conversion, and then attempts to call it |
| 5815 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 5816 | /// the type of function that we'll eventually be calling. |
| 5817 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5818 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5819 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5820 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5821 | Expr *Object, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5822 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5823 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5824 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5825 | return; |
| 5826 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5827 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5828 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5829 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5830 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5831 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5832 | Candidate.Function = 0; |
| 5833 | Candidate.Surrogate = Conversion; |
| 5834 | Candidate.Viable = true; |
| 5835 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5836 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5837 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5838 | |
| 5839 | // Determine the implicit conversion sequence for the implicit |
| 5840 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5842 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5843 | Object->Classify(Context), |
| 5844 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5845 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5846 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5847 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5848 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5849 | return; |
| 5850 | } |
| 5851 | |
| 5852 | // The first conversion is actually a user-defined conversion whose |
| 5853 | // first conversion is ObjectInit's standard conversion (which is |
| 5854 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5855 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5856 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 5857 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5858 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5859 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 5860 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5861 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5862 | = Candidate.Conversions[0].UserDefined.Before; |
| 5863 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 5864 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5865 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5866 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5867 | |
| 5868 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5869 | // parameters is viable only if it has an ellipsis in its parameter |
| 5870 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5871 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5872 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5873 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5874 | return; |
| 5875 | } |
| 5876 | |
| 5877 | // Function types don't have any default arguments, so just check if |
| 5878 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5879 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5880 | // Not enough arguments. |
| 5881 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5882 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5883 | return; |
| 5884 | } |
| 5885 | |
| 5886 | // Determine the implicit conversion sequences for each of the |
| 5887 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5888 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5889 | if (ArgIdx < NumArgsInProto) { |
| 5890 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5891 | // exist for each argument an implicit conversion sequence |
| 5892 | // (13.3.3.1) that converts that argument to the corresponding |
| 5893 | // parameter of F. |
| 5894 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5895 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5896 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5897 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5898 | /*InOverloadResolution=*/false, |
| 5899 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5900 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5901 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5902 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5903 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5904 | break; |
| 5905 | } |
| 5906 | } else { |
| 5907 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5908 | // argument for which there is no corresponding parameter is |
| 5909 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5910 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5911 | } |
| 5912 | } |
| 5913 | } |
| 5914 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5915 | /// \brief Add overload candidates for overloaded operators that are |
| 5916 | /// member functions. |
| 5917 | /// |
| 5918 | /// Add the overloaded operator candidates that are member functions |
| 5919 | /// for the operator Op that was used in an operator expression such |
| 5920 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 5921 | /// CandidateSet will store the added overload candidates. (C++ |
| 5922 | /// [over.match.oper]). |
| 5923 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 5924 | SourceLocation OpLoc, |
| 5925 | Expr **Args, unsigned NumArgs, |
| 5926 | OverloadCandidateSet& CandidateSet, |
| 5927 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5928 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5929 | |
| 5930 | // C++ [over.match.oper]p3: |
| 5931 | // For a unary operator @ with an operand of a type whose |
| 5932 | // cv-unqualified version is T1, and for a binary operator @ with |
| 5933 | // a left operand of a type whose cv-unqualified version is T1 and |
| 5934 | // a right operand of a type whose cv-unqualified version is T2, |
| 5935 | // three sets of candidate functions, designated member |
| 5936 | // candidates, non-member candidates and built-in candidates, are |
| 5937 | // constructed as follows: |
| 5938 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5939 | |
| 5940 | // -- If T1 is a class type, the set of member candidates is the |
| 5941 | // result of the qualified lookup of T1::operator@ |
| 5942 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 5943 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5944 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5945 | // Complete the type if it can be completed. Otherwise, we're done. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5946 | if (RequireCompleteType(OpLoc, T1, 0)) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5947 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5948 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5949 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5950 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 5951 | Operators.suppressDiagnostics(); |
| 5952 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5953 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5954 | OperEnd = Operators.end(); |
| 5955 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5956 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5957 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5958 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5959 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5960 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5961 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5964 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 5965 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 5966 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5967 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 5968 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5969 | /// operator. NumContextualBoolArguments is the number of arguments |
| 5970 | /// (at the beginning of the argument list) that will be contextually |
| 5971 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5972 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5973 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5974 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5975 | bool IsAssignmentOperator, |
| 5976 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5977 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5978 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5979 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5980 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5981 | OverloadCandidate &Candidate = CandidateSet.addCandidate(NumArgs); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5982 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5983 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 5984 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5985 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5986 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 5987 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5988 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 5989 | |
| 5990 | // Determine the implicit conversion sequences for each of the |
| 5991 | // arguments. |
| 5992 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5993 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5994 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5995 | // C++ [over.match.oper]p4: |
| 5996 | // For the built-in assignment operators, conversions of the |
| 5997 | // left operand are restricted as follows: |
| 5998 | // -- no temporaries are introduced to hold the left operand, and |
| 5999 | // -- no user-defined conversions are applied to the left |
| 6000 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6001 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6002 | // |
| 6003 | // We block these conversions by turning off user-defined |
| 6004 | // conversions, since that is the only way that initialization of |
| 6005 | // a reference to a non-class type can occur from something that |
| 6006 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6007 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6008 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6009 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6010 | Candidate.Conversions[ArgIdx] |
| 6011 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6012 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6013 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6014 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6015 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6016 | /*InOverloadResolution=*/false, |
| 6017 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6018 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6019 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6020 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6021 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6022 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6023 | break; |
| 6024 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6025 | } |
| 6026 | } |
| 6027 | |
| 6028 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6029 | /// candidate operator functions for built-in operators (C++ |
| 6030 | /// [over.built]). The types are separated into pointer types and |
| 6031 | /// enumeration types. |
| 6032 | class BuiltinCandidateTypeSet { |
| 6033 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6034 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6035 | |
| 6036 | /// PointerTypes - The set of pointer types that will be used in the |
| 6037 | /// built-in candidates. |
| 6038 | TypeSet PointerTypes; |
| 6039 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6040 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6041 | /// used in the built-in candidates. |
| 6042 | TypeSet MemberPointerTypes; |
| 6043 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6044 | /// EnumerationTypes - The set of enumeration types that will be |
| 6045 | /// used in the built-in candidates. |
| 6046 | TypeSet EnumerationTypes; |
| 6047 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6048 | /// \brief The set of vector types that will be used in the built-in |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6049 | /// candidates. |
| 6050 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6051 | |
| 6052 | /// \brief A flag indicating non-record types are viable candidates |
| 6053 | bool HasNonRecordTypes; |
| 6054 | |
| 6055 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6056 | /// were present in the candidate set. |
| 6057 | bool HasArithmeticOrEnumeralTypes; |
| 6058 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6059 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6060 | /// candidate set. |
| 6061 | bool HasNullPtrType; |
| 6062 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6063 | /// Sema - The semantic analysis instance where we are building the |
| 6064 | /// candidate type set. |
| 6065 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6066 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6067 | /// Context - The AST context in which we will build the type sets. |
| 6068 | ASTContext &Context; |
| 6069 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6070 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6071 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6072 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6073 | |
| 6074 | public: |
| 6075 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6076 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6077 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6078 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6079 | : HasNonRecordTypes(false), |
| 6080 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6081 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6082 | SemaRef(SemaRef), |
| 6083 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6084 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6085 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6086 | SourceLocation Loc, |
| 6087 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6088 | bool AllowExplicitConversions, |
| 6089 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6090 | |
| 6091 | /// pointer_begin - First pointer type found; |
| 6092 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6093 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6094 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6095 | iterator pointer_end() { return PointerTypes.end(); } |
| 6096 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6097 | /// member_pointer_begin - First member pointer type found; |
| 6098 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6099 | |
| 6100 | /// member_pointer_end - Past the last member pointer type found; |
| 6101 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6102 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6103 | /// enumeration_begin - First enumeration type found; |
| 6104 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6105 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6106 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6107 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6108 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6109 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6110 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6111 | |
| 6112 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6113 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6114 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6115 | }; |
| 6116 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6117 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6118 | /// the set of pointer types along with any more-qualified variants of |
| 6119 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6120 | /// will add "int const *", "int const volatile *", "int const |
| 6121 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6122 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6123 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6124 | /// |
| 6125 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6126 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6127 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6128 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6129 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6130 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6131 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6132 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6133 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6134 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6135 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6136 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6137 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6138 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6139 | PointeeTy = PTy->getPointeeType(); |
| 6140 | buildObjCPtr = true; |
| 6141 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6142 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6145 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6146 | // (the qualifier would sink to the element type), and for another, the |
| 6147 | // only overload situation where it matters is subscript or pointer +- int, |
| 6148 | // and those shouldn't have qualifier variants anyway. |
| 6149 | if (PointeeTy->isArrayType()) |
| 6150 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6151 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6152 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6153 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6154 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6155 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6156 | // Iterate through all strict supersets of BaseCVR. |
| 6157 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6158 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6159 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6160 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6161 | |
| 6162 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6163 | // the type cannot be restrict-qualified. |
| 6164 | if ((CVR & Qualifiers::Restrict) && |
| 6165 | (!hasRestrict || |
| 6166 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6167 | continue; |
| 6168 | |
| 6169 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6170 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6171 | |
| 6172 | // Build qualified pointer type. |
| 6173 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6174 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6175 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6176 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6177 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6178 | |
| 6179 | // Insert qualified pointer type. |
| 6180 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6181 | } |
| 6182 | |
| 6183 | return true; |
| 6184 | } |
| 6185 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6186 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6187 | /// to the set of pointer types along with any more-qualified variants of |
| 6188 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6189 | /// will add "int const *", "int const volatile *", "int const |
| 6190 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6191 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6192 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6193 | /// |
| 6194 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6195 | bool |
| 6196 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6197 | QualType Ty) { |
| 6198 | // Insert this type. |
| 6199 | if (!MemberPointerTypes.insert(Ty)) |
| 6200 | return false; |
| 6201 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6202 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6203 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6204 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6205 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6206 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6207 | // (the qualifier would sink to the element type), and for another, the |
| 6208 | // only overload situation where it matters is subscript or pointer +- int, |
| 6209 | // and those shouldn't have qualifier variants anyway. |
| 6210 | if (PointeeTy->isArrayType()) |
| 6211 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6212 | const Type *ClassTy = PointerTy->getClass(); |
| 6213 | |
| 6214 | // Iterate through all strict supersets of the pointee type's CVR |
| 6215 | // qualifiers. |
| 6216 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6217 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6218 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6219 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6220 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6221 | MemberPointerTypes.insert( |
| 6222 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6223 | } |
| 6224 | |
| 6225 | return true; |
| 6226 | } |
| 6227 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6228 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6229 | /// 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] | 6230 | /// primarily interested in pointer types and enumeration types. We also |
| 6231 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6232 | /// AllowUserConversions is true if we should look at the conversion |
| 6233 | /// functions of a class type, and AllowExplicitConversions if we |
| 6234 | /// should also include the explicit conversion functions of a class |
| 6235 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6236 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6237 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6238 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6239 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6240 | bool AllowExplicitConversions, |
| 6241 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6242 | // Only deal with canonical types. |
| 6243 | Ty = Context.getCanonicalType(Ty); |
| 6244 | |
| 6245 | // Look through reference types; they aren't part of the type of an |
| 6246 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6247 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6248 | Ty = RefTy->getPointeeType(); |
| 6249 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6250 | // If we're dealing with an array type, decay to the pointer. |
| 6251 | if (Ty->isArrayType()) |
| 6252 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6253 | |
| 6254 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6255 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6256 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6257 | // Flag if we ever add a non-record type. |
| 6258 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6259 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6260 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6261 | // Flag if we encounter an arithmetic type. |
| 6262 | HasArithmeticOrEnumeralTypes = |
| 6263 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6264 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6265 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6266 | PointerTypes.insert(Ty); |
| 6267 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6268 | // Insert our type, and its more-qualified variants, into the set |
| 6269 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6270 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6271 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6272 | } else if (Ty->isMemberPointerType()) { |
| 6273 | // Member pointers are far easier, since the pointee can't be converted. |
| 6274 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6275 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6276 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6277 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6278 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6279 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6280 | // We treat vector types as arithmetic types in many contexts as an |
| 6281 | // extension. |
| 6282 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6283 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6284 | } else if (Ty->isNullPtrType()) { |
| 6285 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6286 | } else if (AllowUserConversions && TyRec) { |
| 6287 | // No conversion functions in incomplete types. |
| 6288 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6289 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6290 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6291 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6292 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6293 | CXXRecordDecl::conversion_iterator> |
| 6294 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6295 | for (CXXRecordDecl::conversion_iterator |
| 6296 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6297 | NamedDecl *D = I.getDecl(); |
| 6298 | if (isa<UsingShadowDecl>(D)) |
| 6299 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6300 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6301 | // Skip conversion function templates; they don't tell us anything |
| 6302 | // about which builtin types we can convert to. |
| 6303 | if (isa<FunctionTemplateDecl>(D)) |
| 6304 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6305 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6306 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6307 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6308 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6309 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6310 | } |
| 6311 | } |
| 6312 | } |
| 6313 | } |
| 6314 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6315 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6316 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6317 | /// given type to the candidate set. |
| 6318 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6319 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6320 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6321 | unsigned NumArgs, |
| 6322 | OverloadCandidateSet &CandidateSet) { |
| 6323 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6325 | // T& operator=(T&, T) |
| 6326 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6327 | ParamTypes[1] = T; |
| 6328 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6329 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6330 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6331 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6332 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6333 | ParamTypes[0] |
| 6334 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6335 | ParamTypes[1] = T; |
| 6336 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6337 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6338 | } |
| 6339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6340 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6341 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6342 | /// 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] | 6343 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6344 | Qualifiers VRQuals; |
| 6345 | const RecordType *TyRec; |
| 6346 | if (const MemberPointerType *RHSMPType = |
| 6347 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6348 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6349 | else |
| 6350 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6351 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6352 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6353 | VRQuals.addVolatile(); |
| 6354 | VRQuals.addRestrict(); |
| 6355 | return VRQuals; |
| 6356 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6357 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6358 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6359 | if (!ClassDecl->hasDefinition()) |
| 6360 | return VRQuals; |
| 6361 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6362 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6363 | CXXRecordDecl::conversion_iterator> |
| 6364 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6365 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6366 | for (CXXRecordDecl::conversion_iterator |
| 6367 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6368 | NamedDecl *D = I.getDecl(); |
| 6369 | if (isa<UsingShadowDecl>(D)) |
| 6370 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6371 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6372 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6373 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6374 | CanTy = ResTypeRef->getPointeeType(); |
| 6375 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6376 | // as see them. |
| 6377 | bool done = false; |
| 6378 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6379 | if (CanTy.isRestrictQualified()) |
| 6380 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6381 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6382 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6384 | CanTy->getAs<MemberPointerType>()) |
| 6385 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6386 | else |
| 6387 | done = true; |
| 6388 | if (CanTy.isVolatileQualified()) |
| 6389 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6390 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6391 | return VRQuals; |
| 6392 | } |
| 6393 | } |
| 6394 | } |
| 6395 | return VRQuals; |
| 6396 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6397 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6398 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6399 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6400 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6401 | /// candidates. It provides shared state and utility methods used throughout |
| 6402 | /// the process, as well as a helper method to add each group of builtin |
| 6403 | /// operator overloads from the standard to a candidate set. |
| 6404 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6405 | // Common instance state available to all overload candidate addition methods. |
| 6406 | Sema &S; |
| 6407 | Expr **Args; |
| 6408 | unsigned NumArgs; |
| 6409 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6410 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6411 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6412 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6413 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6414 | // Define some constants used to index and iterate over the arithemetic types |
| 6415 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6416 | // The "promoted arithmetic types" are the arithmetic |
| 6417 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6418 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6419 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6420 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6421 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6422 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6423 | LastPromotedArithmeticType = 11; |
| 6424 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6425 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6426 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6427 | CanQualType getArithmeticType(unsigned index) { |
| 6428 | assert(index < NumArithmeticTypes); |
| 6429 | static CanQualType ASTContext::* const |
| 6430 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6431 | // Start of promoted types. |
| 6432 | &ASTContext::FloatTy, |
| 6433 | &ASTContext::DoubleTy, |
| 6434 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6435 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6436 | // Start of integral types. |
| 6437 | &ASTContext::IntTy, |
| 6438 | &ASTContext::LongTy, |
| 6439 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6440 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6441 | &ASTContext::UnsignedIntTy, |
| 6442 | &ASTContext::UnsignedLongTy, |
| 6443 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6444 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6445 | // End of promoted types. |
| 6446 | |
| 6447 | &ASTContext::BoolTy, |
| 6448 | &ASTContext::CharTy, |
| 6449 | &ASTContext::WCharTy, |
| 6450 | &ASTContext::Char16Ty, |
| 6451 | &ASTContext::Char32Ty, |
| 6452 | &ASTContext::SignedCharTy, |
| 6453 | &ASTContext::ShortTy, |
| 6454 | &ASTContext::UnsignedCharTy, |
| 6455 | &ASTContext::UnsignedShortTy, |
| 6456 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6457 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6458 | }; |
| 6459 | return S.Context.*ArithmeticTypes[index]; |
| 6460 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6461 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6462 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6463 | /// converions for the given arithmetic types. |
| 6464 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6465 | // Accelerator table for performing the usual arithmetic conversions. |
| 6466 | // The rules are basically: |
| 6467 | // - if either is floating-point, use the wider floating-point |
| 6468 | // - if same signedness, use the higher rank |
| 6469 | // - if same size, use unsigned of the higher rank |
| 6470 | // - use the larger type |
| 6471 | // These rules, together with the axiom that higher ranks are |
| 6472 | // never smaller, are sufficient to precompute all of these results |
| 6473 | // *except* when dealing with signed types of higher rank. |
| 6474 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6475 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6476 | // We assume that int128 has a higher rank than long long on all platforms. |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6477 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6478 | Dep=-1, |
| 6479 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6480 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6481 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6482 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6483 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6484 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6485 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6486 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6487 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6488 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6489 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6490 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6491 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6492 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6493 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6494 | }; |
| 6495 | |
| 6496 | assert(L < LastPromotedArithmeticType); |
| 6497 | assert(R < LastPromotedArithmeticType); |
| 6498 | int Idx = ConversionsTable[L][R]; |
| 6499 | |
| 6500 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6501 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6502 | |
| 6503 | // Slow path: we need to compare widths. |
| 6504 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6505 | CanQualType LT = getArithmeticType(L), |
| 6506 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6507 | unsigned LW = S.Context.getIntWidth(LT), |
| 6508 | RW = S.Context.getIntWidth(RT); |
| 6509 | |
| 6510 | // If they're different widths, use the signed type. |
| 6511 | if (LW > RW) return LT; |
| 6512 | else if (LW < RW) return RT; |
| 6513 | |
| 6514 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6515 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6516 | assert(L == SLL || R == SLL); |
| 6517 | return S.Context.UnsignedLongLongTy; |
| 6518 | } |
| 6519 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6520 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6521 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6522 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6523 | bool HasVolatile, |
| 6524 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6525 | QualType ParamTypes[2] = { |
| 6526 | S.Context.getLValueReferenceType(CandidateTy), |
| 6527 | S.Context.IntTy |
| 6528 | }; |
| 6529 | |
| 6530 | // Non-volatile version. |
| 6531 | if (NumArgs == 1) |
| 6532 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6533 | else |
| 6534 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6535 | |
| 6536 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6537 | // add volatile version only if there are conversions to a volatile type. |
| 6538 | if (HasVolatile) { |
| 6539 | ParamTypes[0] = |
| 6540 | S.Context.getLValueReferenceType( |
| 6541 | S.Context.getVolatileType(CandidateTy)); |
| 6542 | if (NumArgs == 1) |
| 6543 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6544 | else |
| 6545 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6546 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6547 | |
| 6548 | // Add restrict version only if there are conversions to a restrict type |
| 6549 | // and our candidate type is a non-restrict-qualified pointer. |
| 6550 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6551 | !CandidateTy.isRestrictQualified()) { |
| 6552 | ParamTypes[0] |
| 6553 | = S.Context.getLValueReferenceType( |
| 6554 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
| 6555 | if (NumArgs == 1) |
| 6556 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6557 | else |
| 6558 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6559 | |
| 6560 | if (HasVolatile) { |
| 6561 | ParamTypes[0] |
| 6562 | = S.Context.getLValueReferenceType( |
| 6563 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6564 | (Qualifiers::Volatile | |
| 6565 | Qualifiers::Restrict))); |
| 6566 | if (NumArgs == 1) |
| 6567 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, |
| 6568 | CandidateSet); |
| 6569 | else |
| 6570 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6571 | } |
| 6572 | } |
| 6573 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6574 | } |
| 6575 | |
| 6576 | public: |
| 6577 | BuiltinOperatorOverloadBuilder( |
| 6578 | Sema &S, Expr **Args, unsigned NumArgs, |
| 6579 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6580 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6581 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6582 | OverloadCandidateSet &CandidateSet) |
| 6583 | : S(S), Args(Args), NumArgs(NumArgs), |
| 6584 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6585 | HasArithmeticOrEnumeralCandidateType( |
| 6586 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6587 | CandidateTypes(CandidateTypes), |
| 6588 | CandidateSet(CandidateSet) { |
| 6589 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6590 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6591 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6592 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6593 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6594 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6595 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6596 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6597 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6598 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6599 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6600 | "Invalid last promoted arithmetic type"); |
| 6601 | } |
| 6602 | |
| 6603 | // C++ [over.built]p3: |
| 6604 | // |
| 6605 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6606 | // is either volatile or empty, there exist candidate operator |
| 6607 | // functions of the form |
| 6608 | // |
| 6609 | // VQ T& operator++(VQ T&); |
| 6610 | // T operator++(VQ T&, int); |
| 6611 | // |
| 6612 | // C++ [over.built]p4: |
| 6613 | // |
| 6614 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6615 | // than bool, and VQ is either volatile or empty, there exist |
| 6616 | // candidate operator functions of the form |
| 6617 | // |
| 6618 | // VQ T& operator--(VQ T&); |
| 6619 | // T operator--(VQ T&, int); |
| 6620 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6621 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6622 | return; |
| 6623 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6624 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6625 | Arith < NumArithmeticTypes; ++Arith) { |
| 6626 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6627 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6628 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6629 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6630 | } |
| 6631 | } |
| 6632 | |
| 6633 | // C++ [over.built]p5: |
| 6634 | // |
| 6635 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6636 | // cv-unqualified object type, and VQ is either volatile or |
| 6637 | // empty, there exist candidate operator functions of the form |
| 6638 | // |
| 6639 | // T*VQ& operator++(T*VQ&); |
| 6640 | // T*VQ& operator--(T*VQ&); |
| 6641 | // T* operator++(T*VQ&, int); |
| 6642 | // T* operator--(T*VQ&, int); |
| 6643 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6644 | for (BuiltinCandidateTypeSet::iterator |
| 6645 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6646 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6647 | Ptr != PtrEnd; ++Ptr) { |
| 6648 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6649 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6650 | continue; |
| 6651 | |
| 6652 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6653 | (!(*Ptr).isVolatileQualified() && |
| 6654 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6655 | (!(*Ptr).isRestrictQualified() && |
| 6656 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6657 | } |
| 6658 | } |
| 6659 | |
| 6660 | // C++ [over.built]p6: |
| 6661 | // For every cv-qualified or cv-unqualified object type T, there |
| 6662 | // exist candidate operator functions of the form |
| 6663 | // |
| 6664 | // T& operator*(T*); |
| 6665 | // |
| 6666 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6667 | // For every function type T that does not have cv-qualifiers or a |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6668 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6669 | // T& operator*(T*); |
| 6670 | void addUnaryStarPointerOverloads() { |
| 6671 | for (BuiltinCandidateTypeSet::iterator |
| 6672 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6673 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6674 | Ptr != PtrEnd; ++Ptr) { |
| 6675 | QualType ParamTy = *Ptr; |
| 6676 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6677 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6678 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6679 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6680 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6681 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6682 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6683 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6684 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 6685 | &ParamTy, Args, 1, CandidateSet); |
| 6686 | } |
| 6687 | } |
| 6688 | |
| 6689 | // C++ [over.built]p9: |
| 6690 | // For every promoted arithmetic type T, there exist candidate |
| 6691 | // operator functions of the form |
| 6692 | // |
| 6693 | // T operator+(T); |
| 6694 | // T operator-(T); |
| 6695 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6696 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6697 | return; |
| 6698 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6699 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6700 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6701 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6702 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 6703 | } |
| 6704 | |
| 6705 | // Extension: We also add these operators for vector types. |
| 6706 | for (BuiltinCandidateTypeSet::iterator |
| 6707 | Vec = CandidateTypes[0].vector_begin(), |
| 6708 | VecEnd = CandidateTypes[0].vector_end(); |
| 6709 | Vec != VecEnd; ++Vec) { |
| 6710 | QualType VecTy = *Vec; |
| 6711 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6712 | } |
| 6713 | } |
| 6714 | |
| 6715 | // C++ [over.built]p8: |
| 6716 | // For every type T, there exist candidate operator functions of |
| 6717 | // the form |
| 6718 | // |
| 6719 | // T* operator+(T*); |
| 6720 | void addUnaryPlusPointerOverloads() { |
| 6721 | for (BuiltinCandidateTypeSet::iterator |
| 6722 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6723 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6724 | Ptr != PtrEnd; ++Ptr) { |
| 6725 | QualType ParamTy = *Ptr; |
| 6726 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 6727 | } |
| 6728 | } |
| 6729 | |
| 6730 | // C++ [over.built]p10: |
| 6731 | // For every promoted integral type T, there exist candidate |
| 6732 | // operator functions of the form |
| 6733 | // |
| 6734 | // T operator~(T); |
| 6735 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6736 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6737 | return; |
| 6738 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6739 | for (unsigned Int = FirstPromotedIntegralType; |
| 6740 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6741 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6742 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 6743 | } |
| 6744 | |
| 6745 | // Extension: We also add this operator for vector types. |
| 6746 | for (BuiltinCandidateTypeSet::iterator |
| 6747 | Vec = CandidateTypes[0].vector_begin(), |
| 6748 | VecEnd = CandidateTypes[0].vector_end(); |
| 6749 | Vec != VecEnd; ++Vec) { |
| 6750 | QualType VecTy = *Vec; |
| 6751 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6752 | } |
| 6753 | } |
| 6754 | |
| 6755 | // C++ [over.match.oper]p16: |
| 6756 | // For every pointer to member type T, there exist candidate operator |
| 6757 | // functions of the form |
| 6758 | // |
| 6759 | // bool operator==(T,T); |
| 6760 | // bool operator!=(T,T); |
| 6761 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6762 | /// Set of (canonical) types that we've already handled. |
| 6763 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6764 | |
| 6765 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6766 | for (BuiltinCandidateTypeSet::iterator |
| 6767 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6768 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6769 | MemPtr != MemPtrEnd; |
| 6770 | ++MemPtr) { |
| 6771 | // Don't add the same builtin candidate twice. |
| 6772 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6773 | continue; |
| 6774 | |
| 6775 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6776 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6777 | CandidateSet); |
| 6778 | } |
| 6779 | } |
| 6780 | } |
| 6781 | |
| 6782 | // C++ [over.built]p15: |
| 6783 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6784 | // For every T, where T is an enumeration type, a pointer type, or |
| 6785 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6786 | // |
| 6787 | // bool operator<(T, T); |
| 6788 | // bool operator>(T, T); |
| 6789 | // bool operator<=(T, T); |
| 6790 | // bool operator>=(T, T); |
| 6791 | // bool operator==(T, T); |
| 6792 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6793 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6794 | // C++ [over.match.oper]p3: |
| 6795 | // [...]the built-in candidates include all of the candidate operator |
| 6796 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 6797 | // do not have the same parameter-type-list as any non-template non-member |
| 6798 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6799 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6800 | // Note that in practice, this only affects enumeration types because there |
| 6801 | // aren't any built-in candidates of record type, and a user-defined operator |
| 6802 | // must have an operand of record or enumeration type. Also, the only other |
| 6803 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6804 | // cannot be overloaded for enumeration types, so this is the only place |
| 6805 | // where we must suppress candidates like this. |
| 6806 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 6807 | UserDefinedBinaryOperators; |
| 6808 | |
| 6809 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6810 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 6811 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 6812 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 6813 | CEnd = CandidateSet.end(); |
| 6814 | C != CEnd; ++C) { |
| 6815 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 6816 | continue; |
| 6817 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6818 | if (C->Function->isFunctionTemplateSpecialization()) |
| 6819 | continue; |
| 6820 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6821 | QualType FirstParamType = |
| 6822 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 6823 | QualType SecondParamType = |
| 6824 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 6825 | |
| 6826 | // Skip if either parameter isn't of enumeral type. |
| 6827 | if (!FirstParamType->isEnumeralType() || |
| 6828 | !SecondParamType->isEnumeralType()) |
| 6829 | continue; |
| 6830 | |
| 6831 | // Add this operator to the set of known user-defined operators. |
| 6832 | UserDefinedBinaryOperators.insert( |
| 6833 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 6834 | S.Context.getCanonicalType(SecondParamType))); |
| 6835 | } |
| 6836 | } |
| 6837 | } |
| 6838 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6839 | /// Set of (canonical) types that we've already handled. |
| 6840 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6841 | |
| 6842 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6843 | for (BuiltinCandidateTypeSet::iterator |
| 6844 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 6845 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 6846 | Ptr != PtrEnd; ++Ptr) { |
| 6847 | // Don't add the same builtin candidate twice. |
| 6848 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6849 | continue; |
| 6850 | |
| 6851 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6852 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6853 | CandidateSet); |
| 6854 | } |
| 6855 | for (BuiltinCandidateTypeSet::iterator |
| 6856 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6857 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6858 | Enum != EnumEnd; ++Enum) { |
| 6859 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 6860 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6861 | // Don't add the same builtin candidate twice, or if a user defined |
| 6862 | // candidate exists. |
| 6863 | if (!AddedTypes.insert(CanonType) || |
| 6864 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 6865 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6866 | continue; |
| 6867 | |
| 6868 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6869 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6870 | CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6871 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6872 | |
| 6873 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 6874 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 6875 | if (AddedTypes.insert(NullPtrTy) && |
| 6876 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
| 6877 | NullPtrTy))) { |
| 6878 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| 6879 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6880 | CandidateSet); |
| 6881 | } |
| 6882 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6883 | } |
| 6884 | } |
| 6885 | |
| 6886 | // C++ [over.built]p13: |
| 6887 | // |
| 6888 | // For every cv-qualified or cv-unqualified object type T |
| 6889 | // there exist candidate operator functions of the form |
| 6890 | // |
| 6891 | // T* operator+(T*, ptrdiff_t); |
| 6892 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 6893 | // T* operator-(T*, ptrdiff_t); |
| 6894 | // T* operator+(ptrdiff_t, T*); |
| 6895 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 6896 | // |
| 6897 | // C++ [over.built]p14: |
| 6898 | // |
| 6899 | // For every T, where T is a pointer to object type, there |
| 6900 | // exist candidate operator functions of the form |
| 6901 | // |
| 6902 | // ptrdiff_t operator-(T, T); |
| 6903 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 6904 | /// Set of (canonical) types that we've already handled. |
| 6905 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6906 | |
| 6907 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 6908 | QualType AsymetricParamTypes[2] = { |
| 6909 | S.Context.getPointerDiffType(), |
| 6910 | S.Context.getPointerDiffType(), |
| 6911 | }; |
| 6912 | for (BuiltinCandidateTypeSet::iterator |
| 6913 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 6914 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 6915 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6916 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 6917 | if (!PointeeTy->isObjectType()) |
| 6918 | continue; |
| 6919 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6920 | AsymetricParamTypes[Arg] = *Ptr; |
| 6921 | if (Arg == 0 || Op == OO_Plus) { |
| 6922 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 6923 | // T* operator+(ptrdiff_t, T*); |
| 6924 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 6925 | CandidateSet); |
| 6926 | } |
| 6927 | if (Op == OO_Minus) { |
| 6928 | // ptrdiff_t operator-(T, T); |
| 6929 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6930 | continue; |
| 6931 | |
| 6932 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6933 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 6934 | Args, 2, CandidateSet); |
| 6935 | } |
| 6936 | } |
| 6937 | } |
| 6938 | } |
| 6939 | |
| 6940 | // C++ [over.built]p12: |
| 6941 | // |
| 6942 | // For every pair of promoted arithmetic types L and R, there |
| 6943 | // exist candidate operator functions of the form |
| 6944 | // |
| 6945 | // LR operator*(L, R); |
| 6946 | // LR operator/(L, R); |
| 6947 | // LR operator+(L, R); |
| 6948 | // LR operator-(L, R); |
| 6949 | // bool operator<(L, R); |
| 6950 | // bool operator>(L, R); |
| 6951 | // bool operator<=(L, R); |
| 6952 | // bool operator>=(L, R); |
| 6953 | // bool operator==(L, R); |
| 6954 | // bool operator!=(L, R); |
| 6955 | // |
| 6956 | // where LR is the result of the usual arithmetic conversions |
| 6957 | // between types L and R. |
| 6958 | // |
| 6959 | // C++ [over.built]p24: |
| 6960 | // |
| 6961 | // For every pair of promoted arithmetic types L and R, there exist |
| 6962 | // candidate operator functions of the form |
| 6963 | // |
| 6964 | // LR operator?(bool, L, R); |
| 6965 | // |
| 6966 | // where LR is the result of the usual arithmetic conversions |
| 6967 | // between types L and R. |
| 6968 | // Our candidates ignore the first parameter. |
| 6969 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6970 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6971 | return; |
| 6972 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6973 | for (unsigned Left = FirstPromotedArithmeticType; |
| 6974 | Left < LastPromotedArithmeticType; ++Left) { |
| 6975 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6976 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6977 | QualType LandR[2] = { getArithmeticType(Left), |
| 6978 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6979 | QualType Result = |
| 6980 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6981 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6982 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6983 | } |
| 6984 | } |
| 6985 | |
| 6986 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 6987 | // conditional operator for vector types. |
| 6988 | for (BuiltinCandidateTypeSet::iterator |
| 6989 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6990 | Vec1End = CandidateTypes[0].vector_end(); |
| 6991 | Vec1 != Vec1End; ++Vec1) { |
| 6992 | for (BuiltinCandidateTypeSet::iterator |
| 6993 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6994 | Vec2End = CandidateTypes[1].vector_end(); |
| 6995 | Vec2 != Vec2End; ++Vec2) { |
| 6996 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 6997 | QualType Result = S.Context.BoolTy; |
| 6998 | if (!isComparison) { |
| 6999 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7000 | Result = *Vec1; |
| 7001 | else |
| 7002 | Result = *Vec2; |
| 7003 | } |
| 7004 | |
| 7005 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 7006 | } |
| 7007 | } |
| 7008 | } |
| 7009 | |
| 7010 | // C++ [over.built]p17: |
| 7011 | // |
| 7012 | // For every pair of promoted integral types L and R, there |
| 7013 | // exist candidate operator functions of the form |
| 7014 | // |
| 7015 | // LR operator%(L, R); |
| 7016 | // LR operator&(L, R); |
| 7017 | // LR operator^(L, R); |
| 7018 | // LR operator|(L, R); |
| 7019 | // L operator<<(L, R); |
| 7020 | // L operator>>(L, R); |
| 7021 | // |
| 7022 | // where LR is the result of the usual arithmetic conversions |
| 7023 | // between types L and R. |
| 7024 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7025 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7026 | return; |
| 7027 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7028 | for (unsigned Left = FirstPromotedIntegralType; |
| 7029 | Left < LastPromotedIntegralType; ++Left) { |
| 7030 | for (unsigned Right = FirstPromotedIntegralType; |
| 7031 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7032 | QualType LandR[2] = { getArithmeticType(Left), |
| 7033 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7034 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7035 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7036 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7037 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 7038 | } |
| 7039 | } |
| 7040 | } |
| 7041 | |
| 7042 | // C++ [over.built]p20: |
| 7043 | // |
| 7044 | // For every pair (T, VQ), where T is an enumeration or |
| 7045 | // pointer to member type and VQ is either volatile or |
| 7046 | // empty, there exist candidate operator functions of the form |
| 7047 | // |
| 7048 | // VQ T& operator=(VQ T&, T); |
| 7049 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7050 | /// Set of (canonical) types that we've already handled. |
| 7051 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7052 | |
| 7053 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7054 | for (BuiltinCandidateTypeSet::iterator |
| 7055 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7056 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7057 | Enum != EnumEnd; ++Enum) { |
| 7058 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7059 | continue; |
| 7060 | |
| 7061 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 7062 | CandidateSet); |
| 7063 | } |
| 7064 | |
| 7065 | for (BuiltinCandidateTypeSet::iterator |
| 7066 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7067 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7068 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7069 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7070 | continue; |
| 7071 | |
| 7072 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 7073 | CandidateSet); |
| 7074 | } |
| 7075 | } |
| 7076 | } |
| 7077 | |
| 7078 | // C++ [over.built]p19: |
| 7079 | // |
| 7080 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7081 | // volatile or empty, there exist candidate operator functions |
| 7082 | // of the form |
| 7083 | // |
| 7084 | // T*VQ& operator=(T*VQ&, T*); |
| 7085 | // |
| 7086 | // C++ [over.built]p21: |
| 7087 | // |
| 7088 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7089 | // cv-unqualified object type and VQ is either volatile or |
| 7090 | // empty, there exist candidate operator functions of the form |
| 7091 | // |
| 7092 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7093 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7094 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7095 | /// Set of (canonical) types that we've already handled. |
| 7096 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7097 | |
| 7098 | for (BuiltinCandidateTypeSet::iterator |
| 7099 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7100 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7101 | Ptr != PtrEnd; ++Ptr) { |
| 7102 | // If this is operator=, keep track of the builtin candidates we added. |
| 7103 | if (isEqualOp) |
| 7104 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7105 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7106 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7107 | |
| 7108 | // non-volatile version |
| 7109 | QualType ParamTypes[2] = { |
| 7110 | S.Context.getLValueReferenceType(*Ptr), |
| 7111 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7112 | }; |
| 7113 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7114 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7115 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7116 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7117 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7118 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7119 | // volatile version |
| 7120 | ParamTypes[0] = |
| 7121 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 7122 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7123 | /*IsAssigmentOperator=*/isEqualOp); |
| 7124 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7125 | |
| 7126 | if (!(*Ptr).isRestrictQualified() && |
| 7127 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7128 | // restrict version |
| 7129 | ParamTypes[0] |
| 7130 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
| 7131 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7132 | /*IsAssigmentOperator=*/isEqualOp); |
| 7133 | |
| 7134 | if (NeedVolatile) { |
| 7135 | // volatile restrict version |
| 7136 | ParamTypes[0] |
| 7137 | = S.Context.getLValueReferenceType( |
| 7138 | S.Context.getCVRQualifiedType(*Ptr, |
| 7139 | (Qualifiers::Volatile | |
| 7140 | Qualifiers::Restrict))); |
| 7141 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7142 | CandidateSet, |
| 7143 | /*IsAssigmentOperator=*/isEqualOp); |
| 7144 | } |
| 7145 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7146 | } |
| 7147 | |
| 7148 | if (isEqualOp) { |
| 7149 | for (BuiltinCandidateTypeSet::iterator |
| 7150 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7151 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7152 | Ptr != PtrEnd; ++Ptr) { |
| 7153 | // Make sure we don't add the same candidate twice. |
| 7154 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7155 | continue; |
| 7156 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7157 | QualType ParamTypes[2] = { |
| 7158 | S.Context.getLValueReferenceType(*Ptr), |
| 7159 | *Ptr, |
| 7160 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7161 | |
| 7162 | // non-volatile version |
| 7163 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7164 | /*IsAssigmentOperator=*/true); |
| 7165 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7166 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7167 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7168 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7169 | // volatile version |
| 7170 | ParamTypes[0] = |
| 7171 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7172 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7173 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7174 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7175 | |
| 7176 | if (!(*Ptr).isRestrictQualified() && |
| 7177 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7178 | // restrict version |
| 7179 | ParamTypes[0] |
| 7180 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
| 7181 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7182 | CandidateSet, /*IsAssigmentOperator=*/true); |
| 7183 | |
| 7184 | if (NeedVolatile) { |
| 7185 | // volatile restrict version |
| 7186 | ParamTypes[0] |
| 7187 | = S.Context.getLValueReferenceType( |
| 7188 | S.Context.getCVRQualifiedType(*Ptr, |
| 7189 | (Qualifiers::Volatile | |
| 7190 | Qualifiers::Restrict))); |
| 7191 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7192 | CandidateSet, /*IsAssigmentOperator=*/true); |
| 7193 | |
| 7194 | } |
| 7195 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7196 | } |
| 7197 | } |
| 7198 | } |
| 7199 | |
| 7200 | // C++ [over.built]p18: |
| 7201 | // |
| 7202 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7203 | // VQ is either volatile or empty, and R is a promoted |
| 7204 | // arithmetic type, there exist candidate operator functions of |
| 7205 | // the form |
| 7206 | // |
| 7207 | // VQ L& operator=(VQ L&, R); |
| 7208 | // VQ L& operator*=(VQ L&, R); |
| 7209 | // VQ L& operator/=(VQ L&, R); |
| 7210 | // VQ L& operator+=(VQ L&, R); |
| 7211 | // VQ L& operator-=(VQ L&, R); |
| 7212 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7213 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7214 | return; |
| 7215 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7216 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7217 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7218 | Right < LastPromotedArithmeticType; ++Right) { |
| 7219 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7220 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7221 | |
| 7222 | // Add this built-in operator as a candidate (VQ is empty). |
| 7223 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7224 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7225 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7226 | /*IsAssigmentOperator=*/isEqualOp); |
| 7227 | |
| 7228 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7229 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7230 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7231 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7232 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7233 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7234 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7235 | /*IsAssigmentOperator=*/isEqualOp); |
| 7236 | } |
| 7237 | } |
| 7238 | } |
| 7239 | |
| 7240 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7241 | for (BuiltinCandidateTypeSet::iterator |
| 7242 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7243 | Vec1End = CandidateTypes[0].vector_end(); |
| 7244 | Vec1 != Vec1End; ++Vec1) { |
| 7245 | for (BuiltinCandidateTypeSet::iterator |
| 7246 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7247 | Vec2End = CandidateTypes[1].vector_end(); |
| 7248 | Vec2 != Vec2End; ++Vec2) { |
| 7249 | QualType ParamTypes[2]; |
| 7250 | ParamTypes[1] = *Vec2; |
| 7251 | // Add this built-in operator as a candidate (VQ is empty). |
| 7252 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 7253 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7254 | /*IsAssigmentOperator=*/isEqualOp); |
| 7255 | |
| 7256 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7257 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7258 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7259 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7260 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7261 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7262 | /*IsAssigmentOperator=*/isEqualOp); |
| 7263 | } |
| 7264 | } |
| 7265 | } |
| 7266 | } |
| 7267 | |
| 7268 | // C++ [over.built]p22: |
| 7269 | // |
| 7270 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7271 | // is either volatile or empty, and R is a promoted integral |
| 7272 | // type, there exist candidate operator functions of the form |
| 7273 | // |
| 7274 | // VQ L& operator%=(VQ L&, R); |
| 7275 | // VQ L& operator<<=(VQ L&, R); |
| 7276 | // VQ L& operator>>=(VQ L&, R); |
| 7277 | // VQ L& operator&=(VQ L&, R); |
| 7278 | // VQ L& operator^=(VQ L&, R); |
| 7279 | // VQ L& operator|=(VQ L&, R); |
| 7280 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7281 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7282 | return; |
| 7283 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7284 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7285 | for (unsigned Right = FirstPromotedIntegralType; |
| 7286 | Right < LastPromotedIntegralType; ++Right) { |
| 7287 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7288 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7289 | |
| 7290 | // Add this built-in operator as a candidate (VQ is empty). |
| 7291 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7292 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7293 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 7294 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7295 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7296 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7297 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7298 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 7299 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7300 | CandidateSet); |
| 7301 | } |
| 7302 | } |
| 7303 | } |
| 7304 | } |
| 7305 | |
| 7306 | // C++ [over.operator]p23: |
| 7307 | // |
| 7308 | // There also exist candidate operator functions of the form |
| 7309 | // |
| 7310 | // bool operator!(bool); |
| 7311 | // bool operator&&(bool, bool); |
| 7312 | // bool operator||(bool, bool); |
| 7313 | void addExclaimOverload() { |
| 7314 | QualType ParamTy = S.Context.BoolTy; |
| 7315 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 7316 | /*IsAssignmentOperator=*/false, |
| 7317 | /*NumContextualBoolArguments=*/1); |
| 7318 | } |
| 7319 | void addAmpAmpOrPipePipeOverload() { |
| 7320 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 7321 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 7322 | /*IsAssignmentOperator=*/false, |
| 7323 | /*NumContextualBoolArguments=*/2); |
| 7324 | } |
| 7325 | |
| 7326 | // C++ [over.built]p13: |
| 7327 | // |
| 7328 | // For every cv-qualified or cv-unqualified object type T there |
| 7329 | // exist candidate operator functions of the form |
| 7330 | // |
| 7331 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7332 | // T& operator[](T*, ptrdiff_t); |
| 7333 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7334 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7335 | // T& operator[](ptrdiff_t, T*); |
| 7336 | void addSubscriptOverloads() { |
| 7337 | for (BuiltinCandidateTypeSet::iterator |
| 7338 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7339 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7340 | Ptr != PtrEnd; ++Ptr) { |
| 7341 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7342 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7343 | if (!PointeeType->isObjectType()) |
| 7344 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7345 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7346 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7347 | |
| 7348 | // T& operator[](T*, ptrdiff_t) |
| 7349 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7350 | } |
| 7351 | |
| 7352 | for (BuiltinCandidateTypeSet::iterator |
| 7353 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7354 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7355 | Ptr != PtrEnd; ++Ptr) { |
| 7356 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7357 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7358 | if (!PointeeType->isObjectType()) |
| 7359 | continue; |
| 7360 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7361 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7362 | |
| 7363 | // T& operator[](ptrdiff_t, T*) |
| 7364 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | // C++ [over.built]p11: |
| 7369 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7370 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7371 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7372 | // there exist candidate operator functions of the form |
| 7373 | // |
| 7374 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7375 | // |
| 7376 | // where CV12 is the union of CV1 and CV2. |
| 7377 | void addArrowStarOverloads() { |
| 7378 | for (BuiltinCandidateTypeSet::iterator |
| 7379 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7380 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7381 | Ptr != PtrEnd; ++Ptr) { |
| 7382 | QualType C1Ty = (*Ptr); |
| 7383 | QualType C1; |
| 7384 | QualifierCollector Q1; |
| 7385 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7386 | if (!isa<RecordType>(C1)) |
| 7387 | continue; |
| 7388 | // heuristic to reduce number of builtin candidates in the set. |
| 7389 | // Add volatile/restrict version only if there are conversions to a |
| 7390 | // volatile/restrict type. |
| 7391 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7392 | continue; |
| 7393 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7394 | continue; |
| 7395 | for (BuiltinCandidateTypeSet::iterator |
| 7396 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7397 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7398 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7399 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7400 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7401 | C2 = C2.getUnqualifiedType(); |
| 7402 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7403 | break; |
| 7404 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7405 | // build CV12 T& |
| 7406 | QualType T = mptr->getPointeeType(); |
| 7407 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7408 | T.isVolatileQualified()) |
| 7409 | continue; |
| 7410 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7411 | T.isRestrictQualified()) |
| 7412 | continue; |
| 7413 | T = Q1.apply(S.Context, T); |
| 7414 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 7415 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7416 | } |
| 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | // Note that we don't consider the first argument, since it has been |
| 7421 | // contextually converted to bool long ago. The candidates below are |
| 7422 | // therefore added as binary. |
| 7423 | // |
| 7424 | // C++ [over.built]p25: |
| 7425 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7426 | // enumeration type, there exist candidate operator functions of the form |
| 7427 | // |
| 7428 | // T operator?(bool, T, T); |
| 7429 | // |
| 7430 | void addConditionalOperatorOverloads() { |
| 7431 | /// Set of (canonical) types that we've already handled. |
| 7432 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7433 | |
| 7434 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7435 | for (BuiltinCandidateTypeSet::iterator |
| 7436 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7437 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7438 | Ptr != PtrEnd; ++Ptr) { |
| 7439 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7440 | continue; |
| 7441 | |
| 7442 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7443 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 7444 | } |
| 7445 | |
| 7446 | for (BuiltinCandidateTypeSet::iterator |
| 7447 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7448 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7449 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7450 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7451 | continue; |
| 7452 | |
| 7453 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 7454 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 7455 | } |
| 7456 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7457 | if (S.getLangOpts().CPlusPlus0x) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7458 | for (BuiltinCandidateTypeSet::iterator |
| 7459 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7460 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7461 | Enum != EnumEnd; ++Enum) { |
| 7462 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7463 | continue; |
| 7464 | |
| 7465 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7466 | continue; |
| 7467 | |
| 7468 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 7469 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 7470 | } |
| 7471 | } |
| 7472 | } |
| 7473 | } |
| 7474 | }; |
| 7475 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7476 | } // end anonymous namespace |
| 7477 | |
| 7478 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7479 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7480 | /// on the operator @p Op and the arguments given. For example, if the |
| 7481 | /// operator is a binary '+', this routine might add "int |
| 7482 | /// operator+(int, int)" to cover integer addition. |
| 7483 | void |
| 7484 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7485 | SourceLocation OpLoc, |
| 7486 | Expr **Args, unsigned NumArgs, |
| 7487 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7488 | // Find all of the types that the arguments can convert to, but only |
| 7489 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7490 | // that make use of these types. Also record whether we encounter non-record |
| 7491 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7492 | Qualifiers VisibleTypeConversionsQuals; |
| 7493 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7494 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7495 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7496 | |
| 7497 | bool HasNonRecordCandidateType = false; |
| 7498 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7499 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7500 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 7501 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7502 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7503 | OpLoc, |
| 7504 | true, |
| 7505 | (Op == OO_Exclaim || |
| 7506 | Op == OO_AmpAmp || |
| 7507 | Op == OO_PipePipe), |
| 7508 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7509 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7510 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7511 | HasArithmeticOrEnumeralCandidateType = |
| 7512 | HasArithmeticOrEnumeralCandidateType || |
| 7513 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7514 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7515 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7516 | // Exit early when no non-record types have been added to the candidate set |
| 7517 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7518 | // |
| 7519 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7520 | // 'bool' overloads. |
| 7521 | if (!HasNonRecordCandidateType && |
| 7522 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7523 | return; |
| 7524 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7525 | // Setup an object to manage the common state for building overloads. |
| 7526 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 7527 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7528 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7529 | CandidateTypes, CandidateSet); |
| 7530 | |
| 7531 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7532 | switch (Op) { |
| 7533 | case OO_None: |
| 7534 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7535 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7536 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7537 | case OO_New: |
| 7538 | case OO_Delete: |
| 7539 | case OO_Array_New: |
| 7540 | case OO_Array_Delete: |
| 7541 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7542 | llvm_unreachable( |
| 7543 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7544 | |
| 7545 | case OO_Comma: |
| 7546 | case OO_Arrow: |
| 7547 | // C++ [over.match.oper]p3: |
| 7548 | // -- For the operator ',', the unary operator '&', or the |
| 7549 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7550 | break; |
| 7551 | |
| 7552 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7553 | if (NumArgs == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7554 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7555 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7556 | |
| 7557 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7558 | if (NumArgs == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7559 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7560 | } else { |
| 7561 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7562 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7563 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7564 | break; |
| 7565 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7566 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7567 | if (NumArgs == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7568 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7569 | else |
| 7570 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7571 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7572 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7573 | case OO_Slash: |
| 7574 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7575 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7576 | |
| 7577 | case OO_PlusPlus: |
| 7578 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7579 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7580 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7581 | break; |
| 7582 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7583 | case OO_EqualEqual: |
| 7584 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7585 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7586 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7587 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7588 | case OO_Less: |
| 7589 | case OO_Greater: |
| 7590 | case OO_LessEqual: |
| 7591 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7592 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7593 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7594 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7595 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7596 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7597 | case OO_Caret: |
| 7598 | case OO_Pipe: |
| 7599 | case OO_LessLess: |
| 7600 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7601 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7602 | break; |
| 7603 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7604 | case OO_Amp: // '&' is either unary or binary |
| 7605 | if (NumArgs == 1) |
| 7606 | // C++ [over.match.oper]p3: |
| 7607 | // -- For the operator ',', the unary operator '&', or the |
| 7608 | // operator '->', the built-in candidates set is empty. |
| 7609 | break; |
| 7610 | |
| 7611 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7612 | break; |
| 7613 | |
| 7614 | case OO_Tilde: |
| 7615 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7616 | break; |
| 7617 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7618 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7619 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7620 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7621 | |
| 7622 | case OO_PlusEqual: |
| 7623 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7624 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7625 | // Fall through. |
| 7626 | |
| 7627 | case OO_StarEqual: |
| 7628 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7629 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7630 | break; |
| 7631 | |
| 7632 | case OO_PercentEqual: |
| 7633 | case OO_LessLessEqual: |
| 7634 | case OO_GreaterGreaterEqual: |
| 7635 | case OO_AmpEqual: |
| 7636 | case OO_CaretEqual: |
| 7637 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7638 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7639 | break; |
| 7640 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7641 | case OO_Exclaim: |
| 7642 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7643 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7644 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7645 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7646 | case OO_PipePipe: |
| 7647 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7648 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7649 | |
| 7650 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7651 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7652 | break; |
| 7653 | |
| 7654 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7655 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7656 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7657 | |
| 7658 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7659 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7660 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7661 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7662 | } |
| 7663 | } |
| 7664 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7665 | /// \brief Add function candidates found via argument-dependent lookup |
| 7666 | /// to the set of overloading candidates. |
| 7667 | /// |
| 7668 | /// This routine performs argument-dependent name lookup based on the |
| 7669 | /// given function name (which may also be an operator name) and adds |
| 7670 | /// all of the overload candidates found by ADL to the overload |
| 7671 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7672 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7673 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7674 | bool Operator, SourceLocation Loc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7675 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7676 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7677 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7678 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7679 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7680 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7681 | // FIXME: This approach for uniquing ADL results (and removing |
| 7682 | // redundant candidates from the set) relies on pointer-equality, |
| 7683 | // which means we need to key off the canonical decl. However, |
| 7684 | // always going back to the canonical decl might not get us the |
| 7685 | // right set of default arguments. What default arguments are |
| 7686 | // we supposed to consider on ADL candidates, anyway? |
| 7687 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7688 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7689 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7690 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7691 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7692 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7693 | CandEnd = CandidateSet.end(); |
| 7694 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7695 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7696 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7697 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7698 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7699 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7700 | |
| 7701 | // For each of the ADL candidates we found, add it to the overload |
| 7702 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7703 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7704 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7705 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7706 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7707 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7708 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7709 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7710 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7711 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7712 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7713 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7714 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7715 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7718 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7719 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7720 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7721 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7722 | const OverloadCandidate &Cand1, |
| 7723 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7724 | SourceLocation Loc, |
| 7725 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7726 | // Define viable functions to be better candidates than non-viable |
| 7727 | // functions. |
| 7728 | if (!Cand2.Viable) |
| 7729 | return Cand1.Viable; |
| 7730 | else if (!Cand1.Viable) |
| 7731 | return false; |
| 7732 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7733 | // C++ [over.match.best]p1: |
| 7734 | // |
| 7735 | // -- if F is a static member function, ICS1(F) is defined such |
| 7736 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7737 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7738 | // better nor worse than ICS1(F). |
| 7739 | unsigned StartArg = 0; |
| 7740 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7741 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7742 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7743 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7744 | // A viable function F1 is defined to be a better function than another |
| 7745 | // 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] | 7746 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7747 | unsigned NumArgs = Cand1.NumConversions; |
| 7748 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7749 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7750 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7751 | switch (CompareImplicitConversionSequences(S, |
| 7752 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7753 | Cand2.Conversions[ArgIdx])) { |
| 7754 | case ImplicitConversionSequence::Better: |
| 7755 | // Cand1 has a better conversion sequence. |
| 7756 | HasBetterConversion = true; |
| 7757 | break; |
| 7758 | |
| 7759 | case ImplicitConversionSequence::Worse: |
| 7760 | // Cand1 can't be better than Cand2. |
| 7761 | return false; |
| 7762 | |
| 7763 | case ImplicitConversionSequence::Indistinguishable: |
| 7764 | // Do nothing. |
| 7765 | break; |
| 7766 | } |
| 7767 | } |
| 7768 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7769 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7770 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7771 | if (HasBetterConversion) |
| 7772 | return true; |
| 7773 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7774 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7775 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7776 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7777 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7778 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7779 | |
| 7780 | // -- F1 and F2 are function template specializations, and the function |
| 7781 | // template for F1 is more specialized than the template for F2 |
| 7782 | // 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] | 7783 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7784 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7785 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7786 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7787 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7788 | Cand2.Function->getPrimaryTemplate(), |
| 7789 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7790 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7791 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7792 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7793 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7794 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7795 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7796 | // -- the context is an initialization by user-defined conversion |
| 7797 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7798 | // from the return type of F1 to the destination type (i.e., |
| 7799 | // the type of the entity being initialized) is a better |
| 7800 | // conversion sequence than the standard conversion sequence |
| 7801 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7802 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7803 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7804 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 7805 | // First check whether we prefer one of the conversion functions over the |
| 7806 | // other. This only distinguishes the results in non-standard, extension |
| 7807 | // cases such as the conversion from a lambda closure type to a function |
| 7808 | // pointer or block. |
| 7809 | ImplicitConversionSequence::CompareKind FuncResult |
| 7810 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 7811 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 7812 | return FuncResult; |
| 7813 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7814 | switch (CompareStandardConversionSequences(S, |
| 7815 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7816 | Cand2.FinalConversion)) { |
| 7817 | case ImplicitConversionSequence::Better: |
| 7818 | // Cand1 has a better conversion sequence. |
| 7819 | return true; |
| 7820 | |
| 7821 | case ImplicitConversionSequence::Worse: |
| 7822 | // Cand1 can't be better than Cand2. |
| 7823 | return false; |
| 7824 | |
| 7825 | case ImplicitConversionSequence::Indistinguishable: |
| 7826 | // Do nothing |
| 7827 | break; |
| 7828 | } |
| 7829 | } |
| 7830 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7831 | return false; |
| 7832 | } |
| 7833 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7834 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7835 | /// within an overload candidate set. |
| 7836 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7837 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7838 | /// which overload resolution occurs. |
| 7839 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 7840 | /// \param Best If overload resolution was successful or found a deleted |
| 7841 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7842 | /// |
| 7843 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7844 | OverloadingResult |
| 7845 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7846 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7847 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7848 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7849 | Best = end(); |
| 7850 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 7851 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7852 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7853 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7854 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7855 | } |
| 7856 | |
| 7857 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7858 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7859 | return OR_No_Viable_Function; |
| 7860 | |
| 7861 | // Make sure that this function is better than every other viable |
| 7862 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7863 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7864 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7865 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7866 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7867 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7868 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7869 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7870 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7871 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7872 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7873 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7874 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 7875 | (Best->Function->isDeleted() || |
| 7876 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7877 | return OR_Deleted; |
| 7878 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7879 | return OR_Success; |
| 7880 | } |
| 7881 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7882 | namespace { |
| 7883 | |
| 7884 | enum OverloadCandidateKind { |
| 7885 | oc_function, |
| 7886 | oc_method, |
| 7887 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7888 | oc_function_template, |
| 7889 | oc_method_template, |
| 7890 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7891 | oc_implicit_default_constructor, |
| 7892 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7893 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7894 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7895 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7896 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7897 | }; |
| 7898 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7899 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 7900 | FunctionDecl *Fn, |
| 7901 | std::string &Description) { |
| 7902 | bool isTemplate = false; |
| 7903 | |
| 7904 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 7905 | isTemplate = true; |
| 7906 | Description = S.getTemplateArgumentBindingsText( |
| 7907 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 7908 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7909 | |
| 7910 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7911 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7912 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7913 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7914 | if (Ctor->getInheritedConstructor()) |
| 7915 | return oc_implicit_inherited_constructor; |
| 7916 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7917 | if (Ctor->isDefaultConstructor()) |
| 7918 | return oc_implicit_default_constructor; |
| 7919 | |
| 7920 | if (Ctor->isMoveConstructor()) |
| 7921 | return oc_implicit_move_constructor; |
| 7922 | |
| 7923 | assert(Ctor->isCopyConstructor() && |
| 7924 | "unexpected sort of implicit constructor"); |
| 7925 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7926 | } |
| 7927 | |
| 7928 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7929 | // This actually gets spelled 'candidate function' for now, but |
| 7930 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7931 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7932 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7933 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7934 | if (Meth->isMoveAssignmentOperator()) |
| 7935 | return oc_implicit_move_assignment; |
| 7936 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 7937 | if (Meth->isCopyAssignmentOperator()) |
| 7938 | return oc_implicit_copy_assignment; |
| 7939 | |
| 7940 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 7941 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7942 | } |
| 7943 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7944 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7945 | } |
| 7946 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7947 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 7948 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 7949 | if (!Ctor) return; |
| 7950 | |
| 7951 | Ctor = Ctor->getInheritedConstructor(); |
| 7952 | if (!Ctor) return; |
| 7953 | |
| 7954 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 7955 | } |
| 7956 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7957 | } // end anonymous namespace |
| 7958 | |
| 7959 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7960 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7961 | std::string FnDesc; |
| 7962 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7963 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 7964 | << (unsigned) K << FnDesc; |
| 7965 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 7966 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7967 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7970 | //Notes the location of all overload candidates designated through |
| 7971 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7972 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7973 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 7974 | |
| 7975 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 7976 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 7977 | |
| 7978 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7979 | IEnd = OvlExpr->decls_end(); |
| 7980 | I != IEnd; ++I) { |
| 7981 | if (FunctionTemplateDecl *FunTmpl = |
| 7982 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7983 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7984 | } else if (FunctionDecl *Fun |
| 7985 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7986 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7987 | } |
| 7988 | } |
| 7989 | } |
| 7990 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7991 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 7992 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 7993 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7994 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 7995 | Sema &S, |
| 7996 | SourceLocation CaretLoc, |
| 7997 | const PartialDiagnostic &PDiag) const { |
| 7998 | S.Diag(CaretLoc, PDiag) |
| 7999 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8000 | // FIXME: The note limiting machinery is borrowed from |
| 8001 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8002 | // refactoring here. |
| 8003 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8004 | unsigned CandsShown = 0; |
| 8005 | AmbiguousConversionSequence::const_iterator I, E; |
| 8006 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8007 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8008 | break; |
| 8009 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8010 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8011 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8012 | if (I != E) |
| 8013 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8014 | } |
| 8015 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8016 | namespace { |
| 8017 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8018 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8019 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8020 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8021 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8022 | FunctionDecl *Fn = Cand->Function; |
| 8023 | |
| 8024 | // There's a conversion slot for the object argument if this is a |
| 8025 | // non-constructor method. Note that 'I' corresponds the |
| 8026 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8027 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8028 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8029 | if (I == 0) |
| 8030 | isObjectArgument = true; |
| 8031 | else |
| 8032 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8035 | std::string FnDesc; |
| 8036 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8037 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8038 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8039 | QualType FromTy = Conv.Bad.getFromType(); |
| 8040 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8041 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8042 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8043 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8044 | Expr *E = FromExpr->IgnoreParens(); |
| 8045 | if (isa<UnaryOperator>(E)) |
| 8046 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8047 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8048 | |
| 8049 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8050 | << (unsigned) FnKind << FnDesc |
| 8051 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8052 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8053 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8054 | return; |
| 8055 | } |
| 8056 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8057 | // Do some hand-waving analysis to see if the non-viability is due |
| 8058 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8059 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8060 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8061 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8062 | CToTy = RT->getPointeeType(); |
| 8063 | else { |
| 8064 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8065 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8066 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8067 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8068 | } |
| 8069 | |
| 8070 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8071 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8072 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8073 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8074 | |
| 8075 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8076 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8077 | << (unsigned) FnKind << FnDesc |
| 8078 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8079 | << FromTy |
| 8080 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8081 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8082 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8083 | return; |
| 8084 | } |
| 8085 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8086 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8087 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8088 | << (unsigned) FnKind << FnDesc |
| 8089 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8090 | << FromTy |
| 8091 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8092 | << (unsigned) isObjectArgument << I+1; |
| 8093 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8094 | return; |
| 8095 | } |
| 8096 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8097 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8098 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8099 | << (unsigned) FnKind << FnDesc |
| 8100 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8101 | << FromTy |
| 8102 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8103 | << (unsigned) isObjectArgument << I+1; |
| 8104 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8105 | return; |
| 8106 | } |
| 8107 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8108 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8109 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8110 | |
| 8111 | if (isObjectArgument) { |
| 8112 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8113 | << (unsigned) FnKind << FnDesc |
| 8114 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8115 | << FromTy << (CVR - 1); |
| 8116 | } else { |
| 8117 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8118 | << (unsigned) FnKind << FnDesc |
| 8119 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8120 | << FromTy << (CVR - 1) << I+1; |
| 8121 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8122 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8123 | return; |
| 8124 | } |
| 8125 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8126 | // Special diagnostic for failure to convert an initializer list, since |
| 8127 | // telling the user that it has type void is not useful. |
| 8128 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8129 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8130 | << (unsigned) FnKind << FnDesc |
| 8131 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8132 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8133 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8134 | return; |
| 8135 | } |
| 8136 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8137 | // Diagnose references or pointers to incomplete types differently, |
| 8138 | // since it's far from impossible that the incompleteness triggered |
| 8139 | // the failure. |
| 8140 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8141 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8142 | TempFromTy = PTy->getPointeeType(); |
| 8143 | if (TempFromTy->isIncompleteType()) { |
| 8144 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8145 | << (unsigned) FnKind << FnDesc |
| 8146 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8147 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8148 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8149 | return; |
| 8150 | } |
| 8151 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8152 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8153 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8154 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8155 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8156 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8157 | FromPtrTy->getPointeeType()) && |
| 8158 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8159 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8160 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8161 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8162 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8163 | } |
| 8164 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8165 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8166 | if (const ObjCObjectPointerType *ToPtrTy |
| 8167 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8168 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8169 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8170 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8171 | FromPtrTy->getPointeeType()) && |
| 8172 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8173 | BaseToDerivedConversion = 2; |
| 8174 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8175 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8176 | !FromTy->isIncompleteType() && |
| 8177 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8178 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8179 | BaseToDerivedConversion = 3; |
| 8180 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8181 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8182 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8183 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8184 | << (unsigned) FnKind << FnDesc |
| 8185 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8186 | << (unsigned) isObjectArgument << I + 1; |
| 8187 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8188 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8189 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8190 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8191 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8192 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8193 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8194 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8195 | << (unsigned) FnKind << FnDesc |
| 8196 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8197 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8198 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8199 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8200 | return; |
| 8201 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8202 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8203 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8204 | isa<PointerType>(CToTy)) { |
| 8205 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8206 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8207 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8208 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8209 | << (unsigned) FnKind << FnDesc |
| 8210 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8211 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8212 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8213 | return; |
| 8214 | } |
| 8215 | } |
| 8216 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8217 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8218 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8219 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8220 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8221 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8222 | << (unsigned) (Cand->Fix.Kind); |
| 8223 | |
| 8224 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8225 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8226 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8227 | FDiag << *HI; |
| 8228 | S.Diag(Fn->getLocation(), FDiag); |
| 8229 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8230 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8231 | } |
| 8232 | |
| 8233 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8234 | unsigned NumFormalArgs) { |
| 8235 | // TODO: treat calls to a missing default constructor as a special case |
| 8236 | |
| 8237 | FunctionDecl *Fn = Cand->Function; |
| 8238 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8239 | |
| 8240 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8241 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8242 | // With invalid overloaded operators, it's possible that we think we |
| 8243 | // have an arity mismatch when it fact it looks like we have the |
| 8244 | // right number of arguments, because only overloaded operators have |
| 8245 | // the weird behavior of overloading member and non-member functions. |
| 8246 | // Just don't report anything. |
| 8247 | if (Fn->isInvalidDecl() && |
| 8248 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 8249 | return; |
| 8250 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8251 | // at least / at most / exactly |
| 8252 | unsigned mode, modeCount; |
| 8253 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8254 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8255 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8256 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8257 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8258 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8259 | mode = 0; // "at least" |
| 8260 | else |
| 8261 | mode = 2; // "exactly" |
| 8262 | modeCount = MinParams; |
| 8263 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8264 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8265 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8266 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8267 | if (MinParams != FnTy->getNumArgs()) |
| 8268 | mode = 1; // "at most" |
| 8269 | else |
| 8270 | mode = 2; // "exactly" |
| 8271 | modeCount = FnTy->getNumArgs(); |
| 8272 | } |
| 8273 | |
| 8274 | std::string Description; |
| 8275 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8276 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8277 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8278 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8279 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8280 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8281 | else |
| 8282 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8283 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8284 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8285 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8286 | } |
| 8287 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8288 | /// Diagnose a failed template-argument deduction. |
| 8289 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8290 | unsigned NumArgs) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8291 | FunctionDecl *Fn = Cand->Function; // pattern |
| 8292 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8293 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8294 | NamedDecl *ParamD; |
| 8295 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8296 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8297 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8298 | switch (Cand->DeductionFailure.Result) { |
| 8299 | case Sema::TDK_Success: |
| 8300 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8301 | |
| 8302 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8303 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 8304 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 8305 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8306 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8307 | return; |
| 8308 | } |
| 8309 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8310 | case Sema::TDK_Underqualified: { |
| 8311 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8312 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8313 | |
| 8314 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 8315 | |
| 8316 | // Param will have been canonicalized, but it should just be a |
| 8317 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8318 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8319 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8320 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8321 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8322 | |
| 8323 | // Arg has also been canonicalized, but there's nothing we can do |
| 8324 | // about that. It also doesn't matter as much, because it won't |
| 8325 | // have any template parameters in it (because deduction isn't |
| 8326 | // done on dependent types). |
| 8327 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 8328 | |
| 8329 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8330 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8331 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8332 | return; |
| 8333 | } |
| 8334 | |
| 8335 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8336 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8337 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8338 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8339 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8340 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8341 | which = 1; |
| 8342 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8343 | which = 2; |
| 8344 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8345 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8346 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8347 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8348 | << *Cand->DeductionFailure.getFirstArg() |
| 8349 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8350 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8351 | return; |
| 8352 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8353 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8354 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8355 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8356 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8357 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8358 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 8359 | << ParamD->getDeclName(); |
| 8360 | else { |
| 8361 | int index = 0; |
| 8362 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8363 | index = TTP->getIndex(); |
| 8364 | else if (NonTypeTemplateParmDecl *NTTP |
| 8365 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8366 | index = NTTP->getIndex(); |
| 8367 | else |
| 8368 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8369 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8370 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 8371 | << (index + 1); |
| 8372 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8373 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8374 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8375 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8376 | case Sema::TDK_TooManyArguments: |
| 8377 | case Sema::TDK_TooFewArguments: |
| 8378 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 8379 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8380 | |
| 8381 | case Sema::TDK_InstantiationDepth: |
| 8382 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8383 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8384 | return; |
| 8385 | |
| 8386 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8387 | // Format the template argument list into the argument string. |
| 8388 | llvm::SmallString<128> TemplateArgString; |
| 8389 | if (TemplateArgumentList *Args = |
| 8390 | Cand->DeductionFailure.getTemplateArgumentList()) { |
| 8391 | TemplateArgString = " "; |
| 8392 | TemplateArgString += S.getTemplateArgumentBindingsText( |
| 8393 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), *Args); |
| 8394 | } |
| 8395 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8396 | // If this candidate was disabled by enable_if, say so. |
| 8397 | PartialDiagnosticAt *PDiag = Cand->DeductionFailure.getSFINAEDiagnostic(); |
| 8398 | if (PDiag && PDiag->second.getDiagID() == |
| 8399 | diag::err_typename_nested_not_found_enable_if) { |
| 8400 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8401 | // name of the enable_if template. These are both present in PDiag. |
| 8402 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8403 | << "'enable_if'" << TemplateArgString; |
| 8404 | return; |
| 8405 | } |
| 8406 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8407 | // Format the SFINAE diagnostic into the argument string. |
| 8408 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8409 | // formatted message in another diagnostic. |
| 8410 | llvm::SmallString<128> SFINAEArgString; |
| 8411 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8412 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8413 | SFINAEArgString = ": "; |
| 8414 | R = SourceRange(PDiag->first, PDiag->first); |
| 8415 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8416 | } |
| 8417 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8418 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8419 | << TemplateArgString << SFINAEArgString << R; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8420 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8421 | return; |
| 8422 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8423 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8424 | // TODO: diagnose these individually, then kill off |
| 8425 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8426 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8427 | case Sema::TDK_FailedOverloadResolution: |
| 8428 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8429 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8430 | return; |
| 8431 | } |
| 8432 | } |
| 8433 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8434 | /// CUDA: diagnose an invalid call across targets. |
| 8435 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8436 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8437 | FunctionDecl *Callee = Cand->Function; |
| 8438 | |
| 8439 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8440 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8441 | |
| 8442 | std::string FnDesc; |
| 8443 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8444 | |
| 8445 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8446 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8447 | } |
| 8448 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8449 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8450 | /// already generated a primary error at the call site. |
| 8451 | /// |
| 8452 | /// It really does need to be a single diagnostic with its caret |
| 8453 | /// pointed at the candidate declaration. Yes, this creates some |
| 8454 | /// major challenges of technical writing. Yes, this makes pointing |
| 8455 | /// out problems with specific arguments quite awkward. It's still |
| 8456 | /// better than generating twenty screens of text for every failed |
| 8457 | /// overload. |
| 8458 | /// |
| 8459 | /// It would be great to be able to express per-candidate problems |
| 8460 | /// more richly for those diagnostic clients that cared, but we'd |
| 8461 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8462 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8463 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8464 | FunctionDecl *Fn = Cand->Function; |
| 8465 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8466 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8467 | if (Cand->Viable && (Fn->isDeleted() || |
| 8468 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8469 | std::string FnDesc; |
| 8470 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8471 | |
| 8472 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8473 | << FnKind << FnDesc |
| 8474 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8475 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8476 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8477 | } |
| 8478 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8479 | // We don't really have anything else to say about viable candidates. |
| 8480 | if (Cand->Viable) { |
| 8481 | S.NoteOverloadCandidate(Fn); |
| 8482 | return; |
| 8483 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8484 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8485 | switch (Cand->FailureKind) { |
| 8486 | case ovl_fail_too_many_arguments: |
| 8487 | case ovl_fail_too_few_arguments: |
| 8488 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8489 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8490 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8491 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8492 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8493 | case ovl_fail_trivial_conversion: |
| 8494 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8495 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8496 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8497 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8498 | case ovl_fail_bad_conversion: { |
| 8499 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8500 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8501 | if (Cand->Conversions[I].isBad()) |
| 8502 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8503 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8504 | // FIXME: this currently happens when we're called from SemaInit |
| 8505 | // when user-conversion overload fails. Figure out how to handle |
| 8506 | // those conditions and diagnose them well. |
| 8507 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8508 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8509 | |
| 8510 | case ovl_fail_bad_target: |
| 8511 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8512 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8513 | } |
| 8514 | |
| 8515 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8516 | // Desugar the type of the surrogate down to a function type, |
| 8517 | // retaining as many typedefs as possible while still showing |
| 8518 | // the function type (and, therefore, its parameter types). |
| 8519 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8520 | bool isLValueReference = false; |
| 8521 | bool isRValueReference = false; |
| 8522 | bool isPointer = false; |
| 8523 | if (const LValueReferenceType *FnTypeRef = |
| 8524 | FnType->getAs<LValueReferenceType>()) { |
| 8525 | FnType = FnTypeRef->getPointeeType(); |
| 8526 | isLValueReference = true; |
| 8527 | } else if (const RValueReferenceType *FnTypeRef = |
| 8528 | FnType->getAs<RValueReferenceType>()) { |
| 8529 | FnType = FnTypeRef->getPointeeType(); |
| 8530 | isRValueReference = true; |
| 8531 | } |
| 8532 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8533 | FnType = FnTypePtr->getPointeeType(); |
| 8534 | isPointer = true; |
| 8535 | } |
| 8536 | // Desugar down to a function type. |
| 8537 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8538 | // Reconstruct the pointer/reference as appropriate. |
| 8539 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8540 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8541 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8542 | |
| 8543 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8544 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8545 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8546 | } |
| 8547 | |
| 8548 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8549 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8550 | SourceLocation OpLoc, |
| 8551 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8552 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8553 | std::string TypeStr("operator"); |
| 8554 | TypeStr += Opc; |
| 8555 | TypeStr += "("; |
| 8556 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8557 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8558 | TypeStr += ")"; |
| 8559 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8560 | } else { |
| 8561 | TypeStr += ", "; |
| 8562 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8563 | TypeStr += ")"; |
| 8564 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8565 | } |
| 8566 | } |
| 8567 | |
| 8568 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8569 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8570 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8571 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8572 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8573 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8574 | if (!ICS.isAmbiguous()) continue; |
| 8575 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8576 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8577 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8578 | } |
| 8579 | } |
| 8580 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8581 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 8582 | if (Cand->Function) |
| 8583 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8584 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8585 | return Cand->Surrogate->getLocation(); |
| 8586 | return SourceLocation(); |
| 8587 | } |
| 8588 | |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8589 | static unsigned |
| 8590 | RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8591 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8592 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8593 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8594 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8595 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8596 | case Sema::TDK_Incomplete: |
| 8597 | return 1; |
| 8598 | |
| 8599 | case Sema::TDK_Underqualified: |
| 8600 | case Sema::TDK_Inconsistent: |
| 8601 | return 2; |
| 8602 | |
| 8603 | case Sema::TDK_SubstitutionFailure: |
| 8604 | case Sema::TDK_NonDeducedMismatch: |
| 8605 | return 3; |
| 8606 | |
| 8607 | case Sema::TDK_InstantiationDepth: |
| 8608 | case Sema::TDK_FailedOverloadResolution: |
| 8609 | return 4; |
| 8610 | |
| 8611 | case Sema::TDK_InvalidExplicitArguments: |
| 8612 | return 5; |
| 8613 | |
| 8614 | case Sema::TDK_TooManyArguments: |
| 8615 | case Sema::TDK_TooFewArguments: |
| 8616 | return 6; |
| 8617 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8618 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8619 | } |
| 8620 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8621 | struct CompareOverloadCandidatesForDisplay { |
| 8622 | Sema &S; |
| 8623 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8624 | |
| 8625 | bool operator()(const OverloadCandidate *L, |
| 8626 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8627 | // Fast-path this check. |
| 8628 | if (L == R) return false; |
| 8629 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8630 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8631 | if (L->Viable) { |
| 8632 | if (!R->Viable) return true; |
| 8633 | |
| 8634 | // TODO: introduce a tri-valued comparison for overload |
| 8635 | // candidates. Would be more worthwhile if we had a sort |
| 8636 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8637 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8638 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8639 | } else if (R->Viable) |
| 8640 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8641 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8642 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8643 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8644 | // Criteria by which we can sort non-viable candidates: |
| 8645 | if (!L->Viable) { |
| 8646 | // 1. Arity mismatches come after other candidates. |
| 8647 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8648 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8649 | return false; |
| 8650 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8651 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8652 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8653 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8654 | // 2. Bad conversions come first and are ordered by the number |
| 8655 | // of bad conversions and quality of good conversions. |
| 8656 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8657 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8658 | return true; |
| 8659 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8660 | // The conversion that can be fixed with a smaller number of changes, |
| 8661 | // comes first. |
| 8662 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8663 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8664 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8665 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8666 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8667 | if (numLFixes < numRFixes) |
| 8668 | return true; |
| 8669 | else |
| 8670 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8671 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8672 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8673 | // If there's any ordering between the defined conversions... |
| 8674 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8675 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8676 | |
| 8677 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8678 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8679 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8680 | switch (CompareImplicitConversionSequences(S, |
| 8681 | L->Conversions[I], |
| 8682 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8683 | case ImplicitConversionSequence::Better: |
| 8684 | leftBetter++; |
| 8685 | break; |
| 8686 | |
| 8687 | case ImplicitConversionSequence::Worse: |
| 8688 | leftBetter--; |
| 8689 | break; |
| 8690 | |
| 8691 | case ImplicitConversionSequence::Indistinguishable: |
| 8692 | break; |
| 8693 | } |
| 8694 | } |
| 8695 | if (leftBetter > 0) return true; |
| 8696 | if (leftBetter < 0) return false; |
| 8697 | |
| 8698 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8699 | return false; |
| 8700 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8701 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8702 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8703 | return true; |
| 8704 | |
| 8705 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8706 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8707 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8708 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8709 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8710 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8711 | // TODO: others? |
| 8712 | } |
| 8713 | |
| 8714 | // Sort everything else by location. |
| 8715 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8716 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8717 | |
| 8718 | // Put candidates without locations (e.g. builtins) at the end. |
| 8719 | if (LLoc.isInvalid()) return false; |
| 8720 | if (RLoc.isInvalid()) return true; |
| 8721 | |
| 8722 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8723 | } |
| 8724 | }; |
| 8725 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8726 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8727 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8728 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8729 | llvm::ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8730 | assert(!Cand->Viable); |
| 8731 | |
| 8732 | // Don't do anything on failures other than bad conversion. |
| 8733 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 8734 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8735 | // We only want the FixIts if all the arguments can be corrected. |
| 8736 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8737 | // Use a implicit copy initialization to check conversion fixes. |
| 8738 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8739 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8740 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8741 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8742 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8743 | while (true) { |
| 8744 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 8745 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8746 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8747 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8748 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8749 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8750 | } |
| 8751 | |
| 8752 | if (ConvIdx == ConvCount) |
| 8753 | return; |
| 8754 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8755 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 8756 | "remaining conversion is initialized?"); |
| 8757 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 8758 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8759 | // operation somehow. |
| 8760 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8761 | |
| 8762 | const FunctionProtoType* Proto; |
| 8763 | unsigned ArgIdx = ConvIdx; |
| 8764 | |
| 8765 | if (Cand->IsSurrogate) { |
| 8766 | QualType ConvType |
| 8767 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 8768 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 8769 | ConvType = ConvPtrType->getPointeeType(); |
| 8770 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 8771 | ArgIdx--; |
| 8772 | } else if (Cand->Function) { |
| 8773 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 8774 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 8775 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 8776 | ArgIdx--; |
| 8777 | } else { |
| 8778 | // Builtin binary operator with a bad first conversion. |
| 8779 | assert(ConvCount <= 3); |
| 8780 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 8781 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 8782 | = TryCopyInitialization(S, Args[ConvIdx], |
| 8783 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8784 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8785 | /*InOverloadResolution*/ true, |
| 8786 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8787 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8788 | return; |
| 8789 | } |
| 8790 | |
| 8791 | // Fill in the rest of the conversions. |
| 8792 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 8793 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8794 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8795 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 8796 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8797 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8798 | /*InOverloadResolution=*/true, |
| 8799 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8800 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8801 | // Store the FixIt in the candidate if it exists. |
| 8802 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8803 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8804 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8805 | else |
| 8806 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 8807 | } |
| 8808 | } |
| 8809 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8810 | } // end anonymous namespace |
| 8811 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8812 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 8813 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8814 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8815 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 8816 | OverloadCandidateDisplayKind OCD, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8817 | llvm::ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8818 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8819 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8820 | // Sort the candidates by viability and position. Sorting directly would |
| 8821 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8822 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8823 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 8824 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8825 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8826 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8827 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8828 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8829 | if (Cand->Function || Cand->IsSurrogate) |
| 8830 | Cands.push_back(Cand); |
| 8831 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 8832 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8833 | } |
| 8834 | } |
| 8835 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8836 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8837 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8838 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8839 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8840 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8841 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 8842 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8843 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8844 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 8845 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 8846 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8847 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 8848 | // the user with. FIXME: This limit should depend on details of the |
| 8849 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 8850 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8851 | break; |
| 8852 | } |
| 8853 | ++CandsShown; |
| 8854 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8855 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8856 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8857 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8858 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8859 | else { |
| 8860 | assert(Cand->Viable && |
| 8861 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8862 | // Generally we only see ambiguities including viable builtin |
| 8863 | // operators if overload resolution got screwed up by an |
| 8864 | // ambiguous user-defined conversion. |
| 8865 | // |
| 8866 | // FIXME: It's quite possible for different conversions to see |
| 8867 | // different ambiguities, though. |
| 8868 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8869 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8870 | ReportedAmbiguousConversions = true; |
| 8871 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8872 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8873 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8874 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8875 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8876 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8877 | |
| 8878 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8879 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8880 | } |
| 8881 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8882 | // [PossiblyAFunctionType] --> [Return] |
| 8883 | // NonFunctionType --> NonFunctionType |
| 8884 | // R (A) --> R(A) |
| 8885 | // R (*)(A) --> R (A) |
| 8886 | // R (&)(A) --> R (A) |
| 8887 | // R (S::*)(A) --> R (A) |
| 8888 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 8889 | QualType Ret = PossiblyAFunctionType; |
| 8890 | if (const PointerType *ToTypePtr = |
| 8891 | PossiblyAFunctionType->getAs<PointerType>()) |
| 8892 | Ret = ToTypePtr->getPointeeType(); |
| 8893 | else if (const ReferenceType *ToTypeRef = |
| 8894 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 8895 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8896 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8897 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 8898 | Ret = MemTypePtr->getPointeeType(); |
| 8899 | Ret = |
| 8900 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 8901 | return Ret; |
| 8902 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8903 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8904 | // A helper class to help with address of function resolution |
| 8905 | // - allows us to avoid passing around all those ugly parameters |
| 8906 | class AddressOfFunctionResolver |
| 8907 | { |
| 8908 | Sema& S; |
| 8909 | Expr* SourceExpr; |
| 8910 | const QualType& TargetType; |
| 8911 | QualType TargetFunctionType; // Extracted function type from target type |
| 8912 | |
| 8913 | bool Complain; |
| 8914 | //DeclAccessPair& ResultFunctionAccessPair; |
| 8915 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8916 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8917 | bool TargetTypeIsNonStaticMemberFunction; |
| 8918 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8919 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8920 | OverloadExpr::FindResult OvlExprInfo; |
| 8921 | OverloadExpr *OvlExpr; |
| 8922 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8923 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8924 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8925 | public: |
| 8926 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 8927 | const QualType& TargetType, bool Complain) |
| 8928 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 8929 | Complain(Complain), Context(S.getASTContext()), |
| 8930 | TargetTypeIsNonStaticMemberFunction( |
| 8931 | !!TargetType->getAs<MemberPointerType>()), |
| 8932 | FoundNonTemplateFunction(false), |
| 8933 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 8934 | OvlExpr(OvlExprInfo.Expression) |
| 8935 | { |
| 8936 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 8937 | |
| 8938 | if (!TargetFunctionType->isFunctionType()) { |
| 8939 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 8940 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8941 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8942 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 8943 | |
| 8944 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8945 | if (!Method->isStatic()) { |
| 8946 | // If the target type is a non-function type and the function |
| 8947 | // found is a non-static member function, pretend as if that was |
| 8948 | // the target, it's the only possible type to end up with. |
| 8949 | TargetTypeIsNonStaticMemberFunction = true; |
| 8950 | |
| 8951 | // And skip adding the function if its not in the proper form. |
| 8952 | // We'll diagnose this due to an empty set of functions. |
| 8953 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 8954 | return; |
| 8955 | } |
| 8956 | } |
| 8957 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8958 | Matches.push_back(std::make_pair(dap,Fn)); |
| 8959 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8960 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8961 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8962 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8963 | |
| 8964 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 8965 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8966 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8967 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 8968 | // C++ [over.over]p4: |
| 8969 | // If more than one function is selected, [...] |
| 8970 | if (Matches.size() > 1) { |
| 8971 | if (FoundNonTemplateFunction) |
| 8972 | EliminateAllTemplateMatches(); |
| 8973 | else |
| 8974 | EliminateAllExceptMostSpecializedTemplate(); |
| 8975 | } |
| 8976 | } |
| 8977 | } |
| 8978 | |
| 8979 | private: |
| 8980 | bool isTargetTypeAFunction() const { |
| 8981 | return TargetFunctionType->isFunctionType(); |
| 8982 | } |
| 8983 | |
| 8984 | // [ToType] [Return] |
| 8985 | |
| 8986 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8987 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8988 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 8989 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 8990 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 8991 | } |
| 8992 | |
| 8993 | // return true if any matching specializations were found |
| 8994 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 8995 | const DeclAccessPair& CurAccessFunPair) { |
| 8996 | if (CXXMethodDecl *Method |
| 8997 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 8998 | // Skip non-static function templates when converting to pointer, and |
| 8999 | // static when converting to member pointer. |
| 9000 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9001 | return false; |
| 9002 | } |
| 9003 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9004 | return false; |
| 9005 | |
| 9006 | // C++ [over.over]p2: |
| 9007 | // If the name is a function template, template argument deduction is |
| 9008 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9009 | // resulting template argument list is used to generate a single |
| 9010 | // function template specialization, which is added to the set of |
| 9011 | // overloaded functions considered. |
| 9012 | FunctionDecl *Specialization = 0; |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 9013 | TemplateDeductionInfo Info(OvlExpr->getNameLoc()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9014 | if (Sema::TemplateDeductionResult Result |
| 9015 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9016 | &OvlExplicitTemplateArgs, |
| 9017 | TargetFunctionType, Specialization, |
| 9018 | Info)) { |
| 9019 | // FIXME: make a note of the failed deduction for diagnostics. |
| 9020 | (void)Result; |
| 9021 | return false; |
| 9022 | } |
| 9023 | |
| 9024 | // Template argument deduction ensures that we have an exact match. |
| 9025 | // This function template specicalization works. |
| 9026 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 9027 | assert(TargetFunctionType |
| 9028 | == Context.getCanonicalType(Specialization->getType())); |
| 9029 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9030 | return true; |
| 9031 | } |
| 9032 | |
| 9033 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9034 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9035 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9036 | // Skip non-static functions when converting to pointer, and static |
| 9037 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9038 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9039 | return false; |
| 9040 | } |
| 9041 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9042 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9043 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9044 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9045 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9046 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9047 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9048 | return false; |
| 9049 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9050 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9051 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9052 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9053 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9054 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9055 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9056 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9057 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9058 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9059 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9060 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9061 | |
| 9062 | return false; |
| 9063 | } |
| 9064 | |
| 9065 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9066 | bool Ret = false; |
| 9067 | |
| 9068 | // If the overload expression doesn't have the form of a pointer to |
| 9069 | // member, don't try to convert it to a pointer-to-member type. |
| 9070 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9071 | return false; |
| 9072 | |
| 9073 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9074 | E = OvlExpr->decls_end(); |
| 9075 | I != E; ++I) { |
| 9076 | // Look through any using declarations to find the underlying function. |
| 9077 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9078 | |
| 9079 | // C++ [over.over]p3: |
| 9080 | // Non-member functions and static member functions match |
| 9081 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9082 | // Nonstatic member functions match targets of |
| 9083 | // type "pointer-to-member-function." |
| 9084 | // Note that according to DR 247, the containing class does not matter. |
| 9085 | if (FunctionTemplateDecl *FunctionTemplate |
| 9086 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9087 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9088 | Ret = true; |
| 9089 | } |
| 9090 | // If we have explicit template arguments supplied, skip non-templates. |
| 9091 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9092 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9093 | Ret = true; |
| 9094 | } |
| 9095 | assert(Ret || Matches.empty()); |
| 9096 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9097 | } |
| 9098 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9099 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9100 | // [...] and any given function template specialization F1 is |
| 9101 | // eliminated if the set contains a second function template |
| 9102 | // specialization whose function template is more specialized |
| 9103 | // than the function template of F1 according to the partial |
| 9104 | // ordering rules of 14.5.5.2. |
| 9105 | |
| 9106 | // The algorithm specified above is quadratic. We instead use a |
| 9107 | // two-pass algorithm (similar to the one used to identify the |
| 9108 | // best viable function in an overload set) that identifies the |
| 9109 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9110 | |
| 9111 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9112 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9113 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9114 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9115 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9116 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 9117 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 9118 | S.PDiag(), |
| 9119 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 9120 | << Matches[0].second->getDeclName(), |
| 9121 | S.PDiag(diag::note_ovl_candidate) |
| 9122 | << (unsigned) oc_function_template, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9123 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9124 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9125 | if (Result != MatchesCopy.end()) { |
| 9126 | // Make it the first and only element |
| 9127 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9128 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9129 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9130 | } |
| 9131 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9132 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9133 | void EliminateAllTemplateMatches() { |
| 9134 | // [...] any function template specializations in the set are |
| 9135 | // eliminated if the set also contains a non-template function, [...] |
| 9136 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9137 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9138 | ++I; |
| 9139 | else { |
| 9140 | Matches[I] = Matches[--N]; |
| 9141 | Matches.set_size(N); |
| 9142 | } |
| 9143 | } |
| 9144 | } |
| 9145 | |
| 9146 | public: |
| 9147 | void ComplainNoMatchesFound() const { |
| 9148 | assert(Matches.empty()); |
| 9149 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9150 | << OvlExpr->getName() << TargetFunctionType |
| 9151 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9152 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
| 9155 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9156 | return TargetTypeIsNonStaticMemberFunction && |
| 9157 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9158 | } |
| 9159 | |
| 9160 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9161 | // TODO: Should we condition this on whether any functions might |
| 9162 | // have matched, or is it more appropriate to do that in callers? |
| 9163 | // TODO: a fixit wouldn't hurt. |
| 9164 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9165 | << TargetType << OvlExpr->getSourceRange(); |
| 9166 | } |
| 9167 | |
| 9168 | void ComplainOfInvalidConversion() const { |
| 9169 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9170 | << OvlExpr->getName() << TargetType; |
| 9171 | } |
| 9172 | |
| 9173 | void ComplainMultipleMatchesFound() const { |
| 9174 | assert(Matches.size() > 1); |
| 9175 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9176 | << OvlExpr->getName() |
| 9177 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9178 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9179 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9180 | |
| 9181 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9182 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9183 | int getNumMatches() const { return Matches.size(); } |
| 9184 | |
| 9185 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9186 | if (Matches.size() != 1) return 0; |
| 9187 | return Matches[0].second; |
| 9188 | } |
| 9189 | |
| 9190 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9191 | if (Matches.size() != 1) return 0; |
| 9192 | return &Matches[0].first; |
| 9193 | } |
| 9194 | }; |
| 9195 | |
| 9196 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9197 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9198 | /// expression with overloaded function type and @p ToType is the type |
| 9199 | /// we're trying to resolve to. For example: |
| 9200 | /// |
| 9201 | /// @code |
| 9202 | /// int f(double); |
| 9203 | /// int f(int); |
| 9204 | /// |
| 9205 | /// int (*pfd)(double) = f; // selects f(double) |
| 9206 | /// @endcode |
| 9207 | /// |
| 9208 | /// This routine returns the resulting FunctionDecl if it could be |
| 9209 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9210 | /// routine will emit diagnostics if there is an error. |
| 9211 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9212 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9213 | QualType TargetType, |
| 9214 | bool Complain, |
| 9215 | DeclAccessPair &FoundResult, |
| 9216 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9217 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9218 | |
| 9219 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9220 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9221 | int NumMatches = Resolver.getNumMatches(); |
| 9222 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9223 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9224 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9225 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9226 | else |
| 9227 | Resolver.ComplainNoMatchesFound(); |
| 9228 | } |
| 9229 | else if (NumMatches > 1 && Complain) |
| 9230 | Resolver.ComplainMultipleMatchesFound(); |
| 9231 | else if (NumMatches == 1) { |
| 9232 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9233 | assert(Fn); |
| 9234 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 9235 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9236 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9237 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9238 | |
| 9239 | if (pHadMultipleCandidates) |
| 9240 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9241 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9244 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9245 | /// resolve that overloaded function expression down to a single function. |
| 9246 | /// |
| 9247 | /// This routine can only resolve template-ids that refer to a single function |
| 9248 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9249 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9250 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9251 | FunctionDecl * |
| 9252 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9253 | bool Complain, |
| 9254 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9255 | // C++ [over.over]p1: |
| 9256 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9257 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9258 | // C++ [over.over]p1: |
| 9259 | // [...] The overloaded function name can be preceded by the & |
| 9260 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9261 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9262 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9263 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9264 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9265 | |
| 9266 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9267 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9268 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9269 | // Look through all of the overloaded functions, searching for one |
| 9270 | // whose type matches exactly. |
| 9271 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9272 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9273 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9274 | // C++0x [temp.arg.explicit]p3: |
| 9275 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9276 | // where deduction is not done, if a template argument list is |
| 9277 | // specified and it, along with any default template arguments, |
| 9278 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9279 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9280 | FunctionTemplateDecl *FunctionTemplate |
| 9281 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9282 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9283 | // C++ [over.over]p2: |
| 9284 | // If the name is a function template, template argument deduction is |
| 9285 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9286 | // resulting template argument list is used to generate a single |
| 9287 | // function template specialization, which is added to the set of |
| 9288 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9289 | FunctionDecl *Specialization = 0; |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 9290 | TemplateDeductionInfo Info(ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9291 | if (TemplateDeductionResult Result |
| 9292 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 9293 | Specialization, Info)) { |
| 9294 | // FIXME: make a note of the failed deduction for diagnostics. |
| 9295 | (void)Result; |
| 9296 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9297 | } |
| 9298 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9299 | assert(Specialization && "no specialization and no error?"); |
| 9300 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9301 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9302 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9303 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9304 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9305 | << ovl->getName(); |
| 9306 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9307 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9308 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9309 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9310 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9311 | Matched = Specialization; |
| 9312 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9313 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9314 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9315 | return Matched; |
| 9316 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9317 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9318 | |
| 9319 | |
| 9320 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9321 | // Resolve and fix an overloaded expression that can be resolved |
| 9322 | // because it identifies a single function template specialization. |
| 9323 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9324 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9325 | // |
| 9326 | // Return true if it was logically possible to so resolve the |
| 9327 | // expression, regardless of whether or not it succeeded. Always |
| 9328 | // returns true if 'complain' is set. |
| 9329 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9330 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9331 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9332 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9333 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9334 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9335 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9336 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9337 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9338 | DeclAccessPair found; |
| 9339 | ExprResult SingleFunctionExpression; |
| 9340 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9341 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9342 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9343 | SrcExpr = ExprError(); |
| 9344 | return true; |
| 9345 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9346 | |
| 9347 | // It is only correct to resolve to an instance method if we're |
| 9348 | // resolving a form that's permitted to be a pointer to member. |
| 9349 | // Otherwise we'll end up making a bound member expression, which |
| 9350 | // is illegal in all the contexts we resolve like this. |
| 9351 | if (!ovl.HasFormOfMemberPointer && |
| 9352 | isa<CXXMethodDecl>(fn) && |
| 9353 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9354 | if (!complain) return false; |
| 9355 | |
| 9356 | Diag(ovl.Expression->getExprLoc(), |
| 9357 | diag::err_bound_member_function) |
| 9358 | << 0 << ovl.Expression->getSourceRange(); |
| 9359 | |
| 9360 | // TODO: I believe we only end up here if there's a mix of |
| 9361 | // static and non-static candidates (otherwise the expression |
| 9362 | // would have 'bound member' type, not 'overload' type). |
| 9363 | // Ideally we would note which candidate was chosen and why |
| 9364 | // the static candidates were rejected. |
| 9365 | SrcExpr = ExprError(); |
| 9366 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9367 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9368 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9369 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9370 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9371 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9372 | |
| 9373 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9374 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9375 | SingleFunctionExpression = |
| 9376 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9377 | if (SingleFunctionExpression.isInvalid()) { |
| 9378 | SrcExpr = ExprError(); |
| 9379 | return true; |
| 9380 | } |
| 9381 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9382 | } |
| 9383 | |
| 9384 | if (!SingleFunctionExpression.isUsable()) { |
| 9385 | if (complain) { |
| 9386 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9387 | << ovl.Expression->getName() |
| 9388 | << DestTypeForComplaining |
| 9389 | << OpRangeForComplaining |
| 9390 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9391 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9392 | |
| 9393 | SrcExpr = ExprError(); |
| 9394 | return true; |
| 9395 | } |
| 9396 | |
| 9397 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9398 | } |
| 9399 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9400 | SrcExpr = SingleFunctionExpression; |
| 9401 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9402 | } |
| 9403 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9404 | /// \brief Add a single candidate to the overload set. |
| 9405 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9406 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9407 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9408 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9409 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9410 | bool PartialOverloading, |
| 9411 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9412 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9413 | if (isa<UsingShadowDecl>(Callee)) |
| 9414 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9415 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9416 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9417 | if (ExplicitTemplateArgs) { |
| 9418 | assert(!KnownValid && "Explicit template arguments?"); |
| 9419 | return; |
| 9420 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9421 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9422 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9423 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
| 9426 | if (FunctionTemplateDecl *FuncTemplate |
| 9427 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9428 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9429 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9430 | return; |
| 9431 | } |
| 9432 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9433 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9434 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9435 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9436 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9437 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9438 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9439 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9440 | OverloadCandidateSet &CandidateSet, |
| 9441 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9442 | |
| 9443 | #ifndef NDEBUG |
| 9444 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9445 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9446 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9447 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9448 | // and let Y be the lookup set produced by argument dependent |
| 9449 | // lookup (defined as follows). If X contains |
| 9450 | // |
| 9451 | // -- a declaration of a class member, or |
| 9452 | // |
| 9453 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9454 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9455 | // |
| 9456 | // -- a declaration that is neither a function or a function |
| 9457 | // template |
| 9458 | // |
| 9459 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9460 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9461 | if (ULE->requiresADL()) { |
| 9462 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9463 | E = ULE->decls_end(); I != E; ++I) { |
| 9464 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9465 | assert(isa<UsingShadowDecl>(*I) || |
| 9466 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9467 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9468 | } |
| 9469 | } |
| 9470 | #endif |
| 9471 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9472 | // It would be nice to avoid this copy. |
| 9473 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9474 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9475 | if (ULE->hasExplicitTemplateArgs()) { |
| 9476 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9477 | ExplicitTemplateArgs = &TABuffer; |
| 9478 | } |
| 9479 | |
| 9480 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9481 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9482 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9483 | CandidateSet, PartialOverloading, |
| 9484 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9485 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9486 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9487 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9488 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9489 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9490 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9491 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9492 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9493 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9494 | /// template, where the non-dependent name was declared after the template |
| 9495 | /// was defined. This is common in code written for a compilers which do not |
| 9496 | /// correctly implement two-stage name lookup. |
| 9497 | /// |
| 9498 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9499 | static bool |
| 9500 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9501 | const CXXScopeSpec &SS, LookupResult &R, |
| 9502 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9503 | llvm::ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9504 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9505 | return false; |
| 9506 | |
| 9507 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9508 | if (DC->isTransparentContext()) |
| 9509 | continue; |
| 9510 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9511 | SemaRef.LookupQualifiedName(R, DC); |
| 9512 | |
| 9513 | if (!R.empty()) { |
| 9514 | R.suppressDiagnostics(); |
| 9515 | |
| 9516 | if (isa<CXXRecordDecl>(DC)) { |
| 9517 | // Don't diagnose names we find in classes; we get much better |
| 9518 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9519 | R.clear(); |
| 9520 | return false; |
| 9521 | } |
| 9522 | |
| 9523 | OverloadCandidateSet Candidates(FnLoc); |
| 9524 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9525 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9526 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9527 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9528 | |
| 9529 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9530 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9531 | // No viable functions. Don't bother the user with notes for functions |
| 9532 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9533 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9534 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9535 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9536 | |
| 9537 | // Find the namespaces where ADL would have looked, and suggest |
| 9538 | // declaring the function there instead. |
| 9539 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9540 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9541 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9542 | AssociatedNamespaces, |
| 9543 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9544 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Nick Lewycky | d05df51 | 2012-11-13 00:08:34 +0000 | [diff] [blame] | 9545 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 9546 | for (Sema::AssociatedNamespaceSet::iterator |
| 9547 | it = AssociatedNamespaces.begin(), |
| 9548 | end = AssociatedNamespaces.end(); it != end; ++it) { |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 9549 | // Never suggest declaring a function within namespace 'std'. |
| 9550 | if (Std && Std->Encloses(*it)) |
| 9551 | continue; |
| 9552 | |
| 9553 | // Never suggest declaring a function within a namespace with a reserved |
| 9554 | // name, like __gnu_cxx. |
| 9555 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 9556 | if (NS && |
| 9557 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 9558 | continue; |
| 9559 | |
| 9560 | SuggestedNamespaces.insert(*it); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
| 9563 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 9564 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9565 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9566 | SemaRef.Diag(Best->Function->getLocation(), |
| 9567 | diag::note_not_found_by_two_phase_lookup) |
| 9568 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9569 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9570 | SemaRef.Diag(Best->Function->getLocation(), |
| 9571 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9572 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9573 | } else { |
| 9574 | // FIXME: It would be useful to list the associated namespaces here, |
| 9575 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 9576 | // a localized representation of a list of items. |
| 9577 | SemaRef.Diag(Best->Function->getLocation(), |
| 9578 | diag::note_not_found_by_two_phase_lookup) |
| 9579 | << R.getLookupName() << 2; |
| 9580 | } |
| 9581 | |
| 9582 | // Try to recover by calling this function. |
| 9583 | return true; |
| 9584 | } |
| 9585 | |
| 9586 | R.clear(); |
| 9587 | } |
| 9588 | |
| 9589 | return false; |
| 9590 | } |
| 9591 | |
| 9592 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 9593 | /// template, where the non-dependent operator was declared after the template |
| 9594 | /// was defined. |
| 9595 | /// |
| 9596 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9597 | static bool |
| 9598 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 9599 | SourceLocation OpLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9600 | llvm::ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9601 | DeclarationName OpName = |
| 9602 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 9603 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 9604 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9605 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9606 | } |
| 9607 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9608 | namespace { |
| 9609 | // Callback to limit the allowed keywords and to only accept typo corrections |
| 9610 | // that are keywords or whose decls refer to functions (or template functions) |
| 9611 | // that accept the given number of arguments. |
| 9612 | class RecoveryCallCCC : public CorrectionCandidateCallback { |
| 9613 | public: |
| 9614 | RecoveryCallCCC(Sema &SemaRef, unsigned NumArgs, bool HasExplicitTemplateArgs) |
| 9615 | : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9616 | WantTypeSpecifiers = SemaRef.getLangOpts().CPlusPlus; |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9617 | WantRemainingKeywords = false; |
| 9618 | } |
| 9619 | |
| 9620 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9621 | if (!candidate.getCorrectionDecl()) |
| 9622 | return candidate.isKeyword(); |
| 9623 | |
| 9624 | for (TypoCorrection::const_decl_iterator DI = candidate.begin(), |
| 9625 | DIEnd = candidate.end(); DI != DIEnd; ++DI) { |
| 9626 | FunctionDecl *FD = 0; |
| 9627 | NamedDecl *ND = (*DI)->getUnderlyingDecl(); |
| 9628 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 9629 | FD = FTD->getTemplatedDecl(); |
| 9630 | if (!HasExplicitTemplateArgs && !FD) { |
| 9631 | if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { |
| 9632 | // If the Decl is neither a function nor a template function, |
| 9633 | // determine if it is a pointer or reference to a function. If so, |
| 9634 | // check against the number of arguments expected for the pointee. |
| 9635 | QualType ValType = cast<ValueDecl>(ND)->getType(); |
| 9636 | if (ValType->isAnyPointerType() || ValType->isReferenceType()) |
| 9637 | ValType = ValType->getPointeeType(); |
| 9638 | if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) |
| 9639 | if (FPT->getNumArgs() == NumArgs) |
| 9640 | return true; |
| 9641 | } |
| 9642 | } |
| 9643 | if (FD && FD->getNumParams() >= NumArgs && |
| 9644 | FD->getMinRequiredArguments() <= NumArgs) |
| 9645 | return true; |
| 9646 | } |
| 9647 | return false; |
| 9648 | } |
| 9649 | |
| 9650 | private: |
| 9651 | unsigned NumArgs; |
| 9652 | bool HasExplicitTemplateArgs; |
| 9653 | }; |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9654 | |
| 9655 | // Callback that effectively disabled typo correction |
| 9656 | class NoTypoCorrectionCCC : public CorrectionCandidateCallback { |
| 9657 | public: |
| 9658 | NoTypoCorrectionCCC() { |
| 9659 | WantTypeSpecifiers = false; |
| 9660 | WantExpressionKeywords = false; |
| 9661 | WantCXXNamedCasts = false; |
| 9662 | WantRemainingKeywords = false; |
| 9663 | } |
| 9664 | |
| 9665 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9666 | return false; |
| 9667 | } |
| 9668 | }; |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 9669 | |
| 9670 | class BuildRecoveryCallExprRAII { |
| 9671 | Sema &SemaRef; |
| 9672 | public: |
| 9673 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 9674 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 9675 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 9676 | } |
| 9677 | |
| 9678 | ~BuildRecoveryCallExprRAII() { |
| 9679 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 9680 | } |
| 9681 | }; |
| 9682 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9683 | } |
| 9684 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9685 | /// Attempts to recover from a call where no functions were found. |
| 9686 | /// |
| 9687 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9688 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9689 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9690 | UnresolvedLookupExpr *ULE, |
| 9691 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9692 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9693 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9694 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 9695 | // Do not try to recover if it is already building a recovery call. |
| 9696 | // This stops infinite loops for template instantiations like |
| 9697 | // |
| 9698 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 9699 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 9700 | // |
| 9701 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 9702 | return ExprError(); |
| 9703 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9704 | |
| 9705 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9706 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9707 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9708 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9709 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9710 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9711 | if (ULE->hasExplicitTemplateArgs()) { |
| 9712 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9713 | ExplicitTemplateArgs = &TABuffer; |
| 9714 | } |
| 9715 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9716 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 9717 | Sema::LookupOrdinaryName); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9718 | RecoveryCallCCC Validator(SemaRef, Args.size(), ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9719 | NoTypoCorrectionCCC RejectAll; |
| 9720 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 9721 | (CorrectionCandidateCallback*)&Validator : |
| 9722 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9723 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9724 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9725 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9726 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9727 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9728 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9729 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9730 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 9731 | |
| 9732 | // Build an implicit member call if appropriate. Just drop the |
| 9733 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9734 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9735 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9736 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 9737 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 9738 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9739 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 9740 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9741 | else |
| 9742 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 9743 | |
| 9744 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9745 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9746 | |
| 9747 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9748 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9749 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9750 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9751 | MultiExprArg(Args.data(), Args.size()), |
| 9752 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9753 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 9754 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9755 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 9756 | /// the given function. |
| 9757 | /// \returns true when an the ExprResult output parameter has been set. |
| 9758 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 9759 | UnresolvedLookupExpr *ULE, |
| 9760 | Expr **Args, unsigned NumArgs, |
| 9761 | SourceLocation RParenLoc, |
| 9762 | OverloadCandidateSet *CandidateSet, |
| 9763 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9764 | #ifndef NDEBUG |
| 9765 | if (ULE->requiresADL()) { |
| 9766 | // To do ADL, we must have found an unqualified name. |
| 9767 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 9768 | |
| 9769 | // We don't perform ADL for implicit declarations of builtins. |
| 9770 | // Verify that this was correctly set up. |
| 9771 | FunctionDecl *F; |
| 9772 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 9773 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 9774 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9775 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9776 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9777 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9778 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9779 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9780 | #endif |
| 9781 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9782 | UnbridgedCastsSet UnbridgedCasts; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9783 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) { |
| 9784 | *Result = ExprError(); |
| 9785 | return true; |
| 9786 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 9787 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9788 | // Add the functions denoted by the callee to the set of candidate |
| 9789 | // functions, including those from argument-dependent lookup. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9790 | AddOverloadedCallCandidates(ULE, llvm::makeArrayRef(Args, NumArgs), |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9791 | *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9792 | |
| 9793 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9794 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 9795 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9796 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9797 | // In Microsoft mode, if we are inside a template class member function then |
| 9798 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9799 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9800 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9801 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 9802 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9803 | CallExpr *CE = new (Context) CallExpr(Context, Fn, |
| 9804 | llvm::makeArrayRef(Args, NumArgs), |
| 9805 | Context.DependentTy, VK_RValue, |
| 9806 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9807 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9808 | *Result = Owned(CE); |
| 9809 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9810 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9811 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9812 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9813 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9814 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9815 | return false; |
| 9816 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9817 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9818 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 9819 | /// the completed call expression. If overload resolution fails, emits |
| 9820 | /// diagnostics and returns ExprError() |
| 9821 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 9822 | UnresolvedLookupExpr *ULE, |
| 9823 | SourceLocation LParenLoc, |
| 9824 | Expr **Args, unsigned NumArgs, |
| 9825 | SourceLocation RParenLoc, |
| 9826 | Expr *ExecConfig, |
| 9827 | OverloadCandidateSet *CandidateSet, |
| 9828 | OverloadCandidateSet::iterator *Best, |
| 9829 | OverloadingResult OverloadResult, |
| 9830 | bool AllowTypoCorrection) { |
| 9831 | if (CandidateSet->empty()) |
| 9832 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
| 9833 | llvm::MutableArrayRef<Expr *>(Args, NumArgs), |
| 9834 | RParenLoc, /*EmptyLookup=*/true, |
| 9835 | AllowTypoCorrection); |
| 9836 | |
| 9837 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9838 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9839 | FunctionDecl *FDecl = (*Best)->Function; |
| 9840 | SemaRef.MarkFunctionReferenced(Fn->getExprLoc(), FDecl); |
| 9841 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
| 9842 | SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc()); |
| 9843 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
| 9844 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, |
| 9845 | RParenLoc, ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9846 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9847 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9848 | case OR_No_Viable_Function: { |
| 9849 | // Try to recover by looking for viable functions which the user might |
| 9850 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9851 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9852 | llvm::MutableArrayRef<Expr *>(Args, NumArgs), |
| 9853 | RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9854 | /*EmptyLookup=*/false, |
| 9855 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9856 | if (!Recovery.isInvalid()) |
| 9857 | return Recovery; |
| 9858 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9859 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9860 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9861 | << ULE->getName() << Fn->getSourceRange(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9862 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, |
| 9863 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9864 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9865 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9866 | |
| 9867 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9868 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9869 | << ULE->getName() << Fn->getSourceRange(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9870 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, |
| 9871 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9872 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9873 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9874 | case OR_Deleted: { |
| 9875 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 9876 | << (*Best)->Function->isDeleted() |
| 9877 | << ULE->getName() |
| 9878 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 9879 | << Fn->getSourceRange(); |
| 9880 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, |
| 9881 | llvm::makeArrayRef(Args, NumArgs)); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 9882 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9883 | // We emitted an error for the unvailable/deleted function call but keep |
| 9884 | // the call in the AST. |
| 9885 | FunctionDecl *FDecl = (*Best)->Function; |
| 9886 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
| 9887 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, |
| 9888 | RParenLoc, ExecConfig); |
| 9889 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9890 | } |
| 9891 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9892 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9893 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9894 | } |
| 9895 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 9896 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 9897 | /// (which eventually refers to the declaration Func) and the call |
| 9898 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 9899 | /// to a specific function. If overload resolution succeeds, returns |
| 9900 | /// the call expression produced by overload resolution. |
| 9901 | /// Otherwise, emits diagnostics and returns ExprError. |
| 9902 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 9903 | UnresolvedLookupExpr *ULE, |
| 9904 | SourceLocation LParenLoc, |
| 9905 | Expr **Args, unsigned NumArgs, |
| 9906 | SourceLocation RParenLoc, |
| 9907 | Expr *ExecConfig, |
| 9908 | bool AllowTypoCorrection) { |
| 9909 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 9910 | ExprResult result; |
| 9911 | |
| 9912 | if (buildOverloadedCallSet(S, Fn, ULE, Args, NumArgs, LParenLoc, |
| 9913 | &CandidateSet, &result)) |
| 9914 | return result; |
| 9915 | |
| 9916 | OverloadCandidateSet::iterator Best; |
| 9917 | OverloadingResult OverloadResult = |
| 9918 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 9919 | |
| 9920 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 9921 | RParenLoc, ExecConfig, &CandidateSet, |
| 9922 | &Best, OverloadResult, |
| 9923 | AllowTypoCorrection); |
| 9924 | } |
| 9925 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9926 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 9927 | return Functions.size() > 1 || |
| 9928 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 9929 | } |
| 9930 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9931 | /// \brief Create a unary operation that may resolve to an overloaded |
| 9932 | /// operator. |
| 9933 | /// |
| 9934 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 9935 | /// |
| 9936 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 9937 | /// operator. |
| 9938 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 9939 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9940 | /// considered by overload resolution. The caller needs to build this |
| 9941 | /// set based on the context using, e.g., |
| 9942 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 9943 | /// set should not contain any member functions; those will be added |
| 9944 | /// by CreateOverloadedUnaryOp(). |
| 9945 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 9946 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9947 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9948 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 9949 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9950 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9951 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9952 | |
| 9953 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 9954 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 9955 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9956 | // TODO: provide better source location info. |
| 9957 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9958 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9959 | if (checkPlaceholderForOverload(*this, Input)) |
| 9960 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9961 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9962 | Expr *Args[2] = { Input, 0 }; |
| 9963 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9964 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9965 | // For post-increment and post-decrement, add the implicit '0' as |
| 9966 | // the second argument, so that we know this is a post-increment or |
| 9967 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9968 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9969 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9970 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 9971 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9972 | NumArgs = 2; |
| 9973 | } |
| 9974 | |
| 9975 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9976 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9977 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9978 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9979 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9980 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9981 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9982 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9983 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9984 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 9985 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9986 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9987 | /*ADL*/ true, IsOverloaded(Fns), |
| 9988 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9989 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9990 | llvm::makeArrayRef(Args, NumArgs), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9991 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9992 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 9993 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9994 | } |
| 9995 | |
| 9996 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9997 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9998 | |
| 9999 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10000 | AddFunctionCandidates(Fns, llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
| 10001 | false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10002 | |
| 10003 | // Add operator candidates that are member functions. |
| 10004 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 10005 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10006 | // Add candidates from ADL. |
| 10007 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10008 | OpLoc, llvm::makeArrayRef(Args, NumArgs), |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10009 | /*ExplicitTemplateArgs*/ 0, |
| 10010 | CandidateSet); |
| 10011 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10012 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 10013 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10014 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10015 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10016 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10017 | // Perform overload resolution. |
| 10018 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10019 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10020 | case OR_Success: { |
| 10021 | // We found a built-in operator or an overloaded operator. |
| 10022 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10023 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10024 | if (FnDecl) { |
| 10025 | // We matched an overloaded operator. Build a call to that |
| 10026 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10027 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10028 | MarkFunctionReferenced(OpLoc, FnDecl); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10029 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10030 | // Convert the arguments. |
| 10031 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10032 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10033 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10034 | ExprResult InputRes = |
| 10035 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10036 | Best->FoundDecl, Method); |
| 10037 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10038 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10039 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10040 | } else { |
| 10041 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10042 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10043 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10044 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10045 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10046 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10047 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10048 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10049 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10050 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10051 | } |
| 10052 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10053 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 10054 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10055 | // Determine the result type. |
| 10056 | QualType ResultTy = FnDecl->getResultType(); |
| 10057 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10058 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10059 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10060 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10061 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10062 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10063 | if (FnExpr.isInvalid()) |
| 10064 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10065 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10066 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10067 | CallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10068 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10069 | llvm::makeArrayRef(Args, NumArgs), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10070 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10071 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10072 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10073 | FnDecl)) |
| 10074 | return ExprError(); |
| 10075 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10076 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10077 | } else { |
| 10078 | // We matched a built-in operator. Convert the arguments, then |
| 10079 | // break out so that we will build the appropriate built-in |
| 10080 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10081 | ExprResult InputRes = |
| 10082 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10083 | Best->Conversions[0], AA_Passing); |
| 10084 | if (InputRes.isInvalid()) |
| 10085 | return ExprError(); |
| 10086 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10087 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10088 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10089 | } |
| 10090 | |
| 10091 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10092 | // This is an erroneous use of an operator which can be overloaded by |
| 10093 | // a non-member function. Check for non-member operators which were |
| 10094 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10095 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, |
| 10096 | llvm::makeArrayRef(Args, NumArgs))) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10097 | // FIXME: Recover by calling the found function. |
| 10098 | return ExprError(); |
| 10099 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10100 | // No viable function; fall through to handling this as a |
| 10101 | // built-in operator, which will produce an error message for us. |
| 10102 | break; |
| 10103 | |
| 10104 | case OR_Ambiguous: |
| 10105 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10106 | << UnaryOperator::getOpcodeStr(Opc) |
| 10107 | << Input->getType() |
| 10108 | << Input->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10109 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 10110 | llvm::makeArrayRef(Args, NumArgs), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10111 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10112 | return ExprError(); |
| 10113 | |
| 10114 | case OR_Deleted: |
| 10115 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10116 | << Best->Function->isDeleted() |
| 10117 | << UnaryOperator::getOpcodeStr(Opc) |
| 10118 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10119 | << Input->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10120 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10121 | llvm::makeArrayRef(Args, NumArgs), |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10122 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10123 | return ExprError(); |
| 10124 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10125 | |
| 10126 | // Either we found no viable overloaded operator or we matched a |
| 10127 | // built-in operator. In either case, fall through to trying to |
| 10128 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10129 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10130 | } |
| 10131 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10132 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10133 | /// operator. |
| 10134 | /// |
| 10135 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10136 | /// |
| 10137 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10138 | /// operator. |
| 10139 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10140 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10141 | /// considered by overload resolution. The caller needs to build this |
| 10142 | /// set based on the context using, e.g., |
| 10143 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10144 | /// set should not contain any member functions; those will be added |
| 10145 | /// by CreateOverloadedBinOp(). |
| 10146 | /// |
| 10147 | /// \param LHS Left-hand argument. |
| 10148 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10149 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10150 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10151 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10152 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10153 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10154 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10155 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10156 | |
| 10157 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10158 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10159 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10160 | |
| 10161 | // If either side is type-dependent, create an appropriate dependent |
| 10162 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10163 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10164 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10165 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10166 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10167 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10168 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10169 | Context.DependentTy, |
| 10170 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10171 | OpLoc, |
| 10172 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10173 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10174 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10175 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10176 | VK_LValue, |
| 10177 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10178 | Context.DependentTy, |
| 10179 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10180 | OpLoc, |
| 10181 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10182 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10183 | |
| 10184 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10185 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10186 | // TODO: provide better source location info in DNLoc component. |
| 10187 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10188 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10189 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10190 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10191 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10192 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10193 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10194 | Context.DependentTy, VK_RValue, |
| 10195 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10196 | } |
| 10197 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10198 | // Always do placeholder-like conversions on the RHS. |
| 10199 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10200 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10201 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10202 | // Do placeholder-like conversion on the LHS; note that we should |
| 10203 | // not get here with a PseudoObject LHS. |
| 10204 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10205 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10206 | return ExprError(); |
| 10207 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10208 | // If this is the assignment operator, we only perform overload resolution |
| 10209 | // if the left-hand side is a class or enumeration type. This is actually |
| 10210 | // a hack. The standard requires that we do overload resolution between the |
| 10211 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10212 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10213 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10214 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10215 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10216 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10217 | // If this is the .* operator, which is not overloadable, just |
| 10218 | // create a built-in binary operator. |
| 10219 | if (Opc == BO_PtrMemD) |
| 10220 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10221 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10222 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10223 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10224 | |
| 10225 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10226 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10227 | |
| 10228 | // Add operator candidates that are member functions. |
| 10229 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 10230 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10231 | // Add candidates from ADL. |
| 10232 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10233 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10234 | /*ExplicitTemplateArgs*/ 0, |
| 10235 | CandidateSet); |
| 10236 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10237 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 10238 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10239 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10240 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10241 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10242 | // Perform overload resolution. |
| 10243 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10244 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10245 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10246 | // We found a built-in operator or an overloaded operator. |
| 10247 | FunctionDecl *FnDecl = Best->Function; |
| 10248 | |
| 10249 | if (FnDecl) { |
| 10250 | // We matched an overloaded operator. Build a call to that |
| 10251 | // operator. |
| 10252 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10253 | MarkFunctionReferenced(OpLoc, FnDecl); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10254 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10255 | // Convert the arguments. |
| 10256 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10257 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10258 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10259 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10260 | ExprResult Arg1 = |
| 10261 | PerformCopyInitialization( |
| 10262 | InitializedEntity::InitializeParameter(Context, |
| 10263 | FnDecl->getParamDecl(0)), |
| 10264 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10265 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10266 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10267 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10268 | ExprResult Arg0 = |
| 10269 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10270 | Best->FoundDecl, Method); |
| 10271 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10272 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10273 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10274 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10275 | } else { |
| 10276 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10277 | ExprResult Arg0 = PerformCopyInitialization( |
| 10278 | InitializedEntity::InitializeParameter(Context, |
| 10279 | FnDecl->getParamDecl(0)), |
| 10280 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10281 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10282 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10283 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10284 | ExprResult Arg1 = |
| 10285 | PerformCopyInitialization( |
| 10286 | InitializedEntity::InitializeParameter(Context, |
| 10287 | FnDecl->getParamDecl(1)), |
| 10288 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10289 | if (Arg1.isInvalid()) |
| 10290 | return ExprError(); |
| 10291 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10292 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10293 | } |
| 10294 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10295 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 10296 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10297 | // Determine the result type. |
| 10298 | QualType ResultTy = FnDecl->getResultType(); |
| 10299 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10300 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10301 | |
| 10302 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10303 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 10304 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10305 | if (FnExpr.isInvalid()) |
| 10306 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10307 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10308 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10309 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10310 | Args, ResultTy, VK, OpLoc, |
| 10311 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10312 | |
| 10313 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10314 | FnDecl)) |
| 10315 | return ExprError(); |
| 10316 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10317 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10318 | } else { |
| 10319 | // We matched a built-in operator. Convert the arguments, then |
| 10320 | // break out so that we will build the appropriate built-in |
| 10321 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10322 | ExprResult ArgsRes0 = |
| 10323 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10324 | Best->Conversions[0], AA_Passing); |
| 10325 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10326 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10327 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10328 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10329 | ExprResult ArgsRes1 = |
| 10330 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10331 | Best->Conversions[1], AA_Passing); |
| 10332 | if (ArgsRes1.isInvalid()) |
| 10333 | return ExprError(); |
| 10334 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10335 | break; |
| 10336 | } |
| 10337 | } |
| 10338 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10339 | case OR_No_Viable_Function: { |
| 10340 | // C++ [over.match.oper]p9: |
| 10341 | // If the operator is the operator , [...] and there are no |
| 10342 | // viable functions, then the operator is assumed to be the |
| 10343 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10344 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10345 | break; |
| 10346 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10347 | // For class as left operand for assignment or compound assigment |
| 10348 | // operator do not fall through to handling in built-in, but report that |
| 10349 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10350 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10351 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10352 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10353 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10354 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10355 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10356 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10357 | // This is an erroneous use of an operator which can be overloaded by |
| 10358 | // a non-member function. Check for non-member operators which were |
| 10359 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10360 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10361 | // FIXME: Recover by calling the found function. |
| 10362 | return ExprError(); |
| 10363 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10364 | // No viable function; try to create a built-in operation, which will |
| 10365 | // produce an error. Then, show the non-viable candidates. |
| 10366 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10367 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10368 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10369 | "C++ binary operator overloading is missing candidates!"); |
| 10370 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10371 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10372 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10373 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10374 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10375 | |
| 10376 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10377 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10378 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10379 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10380 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10381 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10382 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10383 | return ExprError(); |
| 10384 | |
| 10385 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10386 | if (isImplicitlyDeleted(Best->Function)) { |
| 10387 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10388 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10389 | << Context.getRecordType(Method->getParent()) |
| 10390 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10391 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10392 | // The user probably meant to call this special member. Just |
| 10393 | // explain why it's deleted. |
| 10394 | NoteDeletedFunction(Method); |
| 10395 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10396 | } else { |
| 10397 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10398 | << Best->Function->isDeleted() |
| 10399 | << BinaryOperator::getOpcodeStr(Opc) |
| 10400 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10401 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10402 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10403 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10404 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10405 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10406 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10407 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10408 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10409 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10410 | } |
| 10411 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10412 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10413 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10414 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10415 | Expr *Base, Expr *Idx) { |
| 10416 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10417 | DeclarationName OpName = |
| 10418 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10419 | |
| 10420 | // If either side is type-dependent, create an appropriate dependent |
| 10421 | // expression. |
| 10422 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10423 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10424 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10425 | // CHECKME: no 'operator' keyword? |
| 10426 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10427 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10428 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10429 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10430 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10431 | /*ADL*/ true, /*Overloaded*/ false, |
| 10432 | UnresolvedSetIterator(), |
| 10433 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10434 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10435 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10436 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10437 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10438 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10439 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10440 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10441 | } |
| 10442 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10443 | // Handle placeholders on both operands. |
| 10444 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10445 | return ExprError(); |
| 10446 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10447 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10448 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10449 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10450 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10451 | |
| 10452 | // Subscript can only be overloaded as a member function. |
| 10453 | |
| 10454 | // Add operator candidates that are member functions. |
| 10455 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 10456 | |
| 10457 | // Add builtin operator candidates. |
| 10458 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 10459 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10460 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10461 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10462 | // Perform overload resolution. |
| 10463 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10464 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10465 | case OR_Success: { |
| 10466 | // We found a built-in operator or an overloaded operator. |
| 10467 | FunctionDecl *FnDecl = Best->Function; |
| 10468 | |
| 10469 | if (FnDecl) { |
| 10470 | // We matched an overloaded operator. Build a call to that |
| 10471 | // operator. |
| 10472 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10473 | MarkFunctionReferenced(LLoc, FnDecl); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10474 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10475 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10476 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10477 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10478 | // Convert the arguments. |
| 10479 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10480 | ExprResult Arg0 = |
| 10481 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10482 | Best->FoundDecl, Method); |
| 10483 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10484 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10485 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10486 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10487 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10488 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10489 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10490 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10491 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10492 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10493 | Owned(Args[1])); |
| 10494 | if (InputInit.isInvalid()) |
| 10495 | return ExprError(); |
| 10496 | |
| 10497 | Args[1] = InputInit.takeAs<Expr>(); |
| 10498 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10499 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10500 | QualType ResultTy = FnDecl->getResultType(); |
| 10501 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10502 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10503 | |
| 10504 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10505 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10506 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10507 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 10508 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10509 | OpLocInfo.getLoc(), |
| 10510 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10511 | if (FnExpr.isInvalid()) |
| 10512 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10513 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10514 | CXXOperatorCallExpr *TheCall = |
| 10515 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10516 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10517 | ResultTy, VK, RLoc, |
| 10518 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10519 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10520 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10521 | FnDecl)) |
| 10522 | return ExprError(); |
| 10523 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10524 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10525 | } else { |
| 10526 | // We matched a built-in operator. Convert the arguments, then |
| 10527 | // break out so that we will build the appropriate built-in |
| 10528 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10529 | ExprResult ArgsRes0 = |
| 10530 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10531 | Best->Conversions[0], AA_Passing); |
| 10532 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10533 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10534 | Args[0] = ArgsRes0.take(); |
| 10535 | |
| 10536 | ExprResult ArgsRes1 = |
| 10537 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10538 | Best->Conversions[1], AA_Passing); |
| 10539 | if (ArgsRes1.isInvalid()) |
| 10540 | return ExprError(); |
| 10541 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10542 | |
| 10543 | break; |
| 10544 | } |
| 10545 | } |
| 10546 | |
| 10547 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10548 | if (CandidateSet.empty()) |
| 10549 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10550 | << Args[0]->getType() << /*subscript*/ 0 |
| 10551 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10552 | else |
| 10553 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10554 | << Args[0]->getType() |
| 10555 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10556 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10557 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10558 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10559 | } |
| 10560 | |
| 10561 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10562 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10563 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10564 | << Args[0]->getType() << Args[1]->getType() |
| 10565 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10566 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10567 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10568 | return ExprError(); |
| 10569 | |
| 10570 | case OR_Deleted: |
| 10571 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10572 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10573 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10574 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10575 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10576 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10577 | return ExprError(); |
| 10578 | } |
| 10579 | |
| 10580 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10581 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10582 | } |
| 10583 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10584 | /// BuildCallToMemberFunction - Build a call to a member |
| 10585 | /// function. MemExpr is the expression that refers to the member |
| 10586 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10587 | /// arguments to the function call (not including the object |
| 10588 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10589 | /// expression refers to a non-static member function or an overloaded |
| 10590 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10591 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10592 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 10593 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10594 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10595 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10596 | MemExprE->getType() == Context.OverloadTy); |
| 10597 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10598 | // Dig out the member expression. This holds both the object |
| 10599 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10600 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10601 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10602 | // Determine whether this is a call to a pointer-to-member function. |
| 10603 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10604 | assert(op->getType() == Context.BoundMemberTy); |
| 10605 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10606 | |
| 10607 | QualType fnType = |
| 10608 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10609 | |
| 10610 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10611 | QualType resultType = proto->getCallResultType(Context); |
| 10612 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10613 | |
| 10614 | // Check that the object type isn't more qualified than the |
| 10615 | // member function we're calling. |
| 10616 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10617 | |
| 10618 | QualType objectType = op->getLHS()->getType(); |
| 10619 | if (op->getOpcode() == BO_PtrMemI) |
| 10620 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10621 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10622 | |
| 10623 | Qualifiers difference = objectQuals - funcQuals; |
| 10624 | difference.removeObjCGCAttr(); |
| 10625 | difference.removeAddressSpace(); |
| 10626 | if (difference) { |
| 10627 | std::string qualsString = difference.getAsString(); |
| 10628 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 10629 | << fnType.getUnqualifiedType() |
| 10630 | << qualsString |
| 10631 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 10632 | } |
| 10633 | |
| 10634 | CXXMemberCallExpr *call |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10635 | = new (Context) CXXMemberCallExpr(Context, MemExprE, |
| 10636 | llvm::makeArrayRef(Args, NumArgs), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10637 | resultType, valueKind, RParenLoc); |
| 10638 | |
| 10639 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10640 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10641 | call, 0)) |
| 10642 | return ExprError(); |
| 10643 | |
| 10644 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 10645 | return ExprError(); |
| 10646 | |
| 10647 | return MaybeBindToTemporary(call); |
| 10648 | } |
| 10649 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10650 | UnbridgedCastsSet UnbridgedCasts; |
| 10651 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 10652 | return ExprError(); |
| 10653 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10654 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10655 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 10656 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10657 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10658 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 10659 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10660 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10661 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10662 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10663 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10664 | } else { |
| 10665 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10666 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10667 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10668 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10669 | Expr::Classification ObjectClassification |
| 10670 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 10671 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10672 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10673 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10674 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10675 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10676 | // FIXME: avoid copy. |
| 10677 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 10678 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 10679 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10680 | TemplateArgs = &TemplateArgsBuffer; |
| 10681 | } |
| 10682 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10683 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 10684 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 10685 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10686 | NamedDecl *Func = *I; |
| 10687 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 10688 | if (isa<UsingShadowDecl>(Func)) |
| 10689 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 10690 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10691 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10692 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10693 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10694 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
| 10695 | llvm::makeArrayRef(Args, NumArgs), CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10696 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10697 | // If explicit template arguments were provided, we can't call a |
| 10698 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10699 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10700 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10701 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10702 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10703 | ObjectClassification, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10704 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10705 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10706 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10707 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10708 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10709 | ObjectType, ObjectClassification, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10710 | llvm::makeArrayRef(Args, NumArgs), |
| 10711 | CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10712 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10713 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10714 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10715 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10716 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 10717 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10718 | UnbridgedCasts.restore(); |
| 10719 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10720 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10721 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 10722 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10723 | case OR_Success: |
| 10724 | Method = cast<CXXMethodDecl>(Best->Function); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10725 | MarkFunctionReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10726 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10727 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10728 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10729 | break; |
| 10730 | |
| 10731 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10732 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10733 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 10734 | << DeclName << MemExprE->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10735 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10736 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10737 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10738 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10739 | |
| 10740 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10741 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 10742 | << DeclName << MemExprE->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10743 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10744 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10745 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10746 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10747 | |
| 10748 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10749 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10750 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10751 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10752 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10753 | << MemExprE->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10754 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10755 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10756 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10757 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10758 | } |
| 10759 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10760 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10761 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10762 | // If overload resolution picked a static member, build a |
| 10763 | // non-member call based on that function. |
| 10764 | if (Method->isStatic()) { |
| 10765 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 10766 | Args, NumArgs, RParenLoc); |
| 10767 | } |
| 10768 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10769 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10770 | } |
| 10771 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10772 | QualType ResultType = Method->getResultType(); |
| 10773 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 10774 | ResultType = ResultType.getNonLValueExprType(Context); |
| 10775 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10776 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10777 | CXXMemberCallExpr *TheCall = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10778 | new (Context) CXXMemberCallExpr(Context, MemExprE, |
| 10779 | llvm::makeArrayRef(Args, NumArgs), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10780 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10781 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 10782 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10783 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10784 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10785 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10786 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10787 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10788 | // We only need to do this if there was actually an overload; otherwise |
| 10789 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10790 | if (!Method->isStatic()) { |
| 10791 | ExprResult ObjectArg = |
| 10792 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 10793 | FoundDecl, Method); |
| 10794 | if (ObjectArg.isInvalid()) |
| 10795 | return ExprError(); |
| 10796 | MemExpr->setBase(ObjectArg.take()); |
| 10797 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10798 | |
| 10799 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10800 | const FunctionProtoType *Proto = |
| 10801 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10802 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10803 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10804 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10805 | |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10806 | DiagnoseSentinelCalls(Method, LParenLoc, Args, NumArgs); |
| 10807 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10808 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10809 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 10810 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10811 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 10812 | isa<CXXDestructorDecl>(CurContext)) && |
| 10813 | TheCall->getMethodDecl()->isPure()) { |
| 10814 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 10815 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 10816 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10817 | Diag(MemExpr->getLocStart(), |
| 10818 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 10819 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 10820 | << MD->getParent()->getDeclName(); |
| 10821 | |
| 10822 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 10823 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10824 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10825 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10826 | } |
| 10827 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10828 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 10829 | /// type (C++ [over.call.object]), which can end up invoking an |
| 10830 | /// overloaded function call operator (@c operator()) or performing a |
| 10831 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10832 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10833 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 10834 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10835 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10836 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10837 | if (checkPlaceholderForOverload(*this, Obj)) |
| 10838 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10839 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10840 | |
| 10841 | UnbridgedCastsSet UnbridgedCasts; |
| 10842 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 10843 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10844 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10845 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 10846 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10847 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10848 | // C++ [over.call.object]p1: |
| 10849 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 10850 | // 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] | 10851 | // candidate functions includes at least the function call |
| 10852 | // operators of T. The function call operators of T are obtained by |
| 10853 | // ordinary lookup of the name operator() in the context of |
| 10854 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10855 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 10856 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10857 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10858 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 10859 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10860 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10861 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 10862 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 10863 | LookupQualifiedName(R, Record->getDecl()); |
| 10864 | R.suppressDiagnostics(); |
| 10865 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10866 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 10867 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10868 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 10869 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 10870 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 10871 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10872 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10873 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10874 | // In addition, for each (non-explicit in C++0x) conversion function |
| 10875 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10876 | // |
| 10877 | // operator conversion-type-id () cv-qualifier; |
| 10878 | // |
| 10879 | // where cv-qualifier is the same cv-qualification as, or a |
| 10880 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 10881 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 10882 | // R", or the type "reference to pointer to function of |
| 10883 | // (P1,...,Pn) returning R", or the type "reference to function |
| 10884 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10885 | // is also considered as a candidate function. Similarly, |
| 10886 | // surrogate call functions are added to the set of candidate |
| 10887 | // functions for each conversion function declared in an |
| 10888 | // accessible base class provided the function is not hidden |
| 10889 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 10890 | std::pair<CXXRecordDecl::conversion_iterator, |
| 10891 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 10892 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 10893 | for (CXXRecordDecl::conversion_iterator |
| 10894 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10895 | NamedDecl *D = *I; |
| 10896 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 10897 | if (isa<UsingShadowDecl>(D)) |
| 10898 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10899 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 10900 | // Skip over templated conversion functions; they aren't |
| 10901 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10902 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 10903 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 10904 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10905 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10906 | if (!Conv->isExplicit()) { |
| 10907 | // Strip the reference type (if any) and then the pointer type (if |
| 10908 | // any) to get down to what might be a function type. |
| 10909 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 10910 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 10911 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10912 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10913 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 10914 | { |
| 10915 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10916 | Object.get(), llvm::makeArrayRef(Args, NumArgs), |
| 10917 | CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10918 | } |
| 10919 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10920 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10921 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10922 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10923 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10924 | // Perform overload resolution. |
| 10925 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10926 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10927 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10928 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10929 | // Overload resolution succeeded; we'll build the appropriate call |
| 10930 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10931 | break; |
| 10932 | |
| 10933 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10934 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10935 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10936 | << Object.get()->getType() << /*call*/ 1 |
| 10937 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10938 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10939 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10940 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10941 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10942 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10943 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10944 | break; |
| 10945 | |
| 10946 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10947 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10948 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10949 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10950 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 10951 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10952 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10953 | |
| 10954 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10955 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10956 | diag::err_ovl_deleted_object_call) |
| 10957 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10958 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10959 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10960 | << Object.get()->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10961 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10962 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10963 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10964 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10965 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10966 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10967 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10968 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10969 | UnbridgedCasts.restore(); |
| 10970 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10971 | if (Best->Function == 0) { |
| 10972 | // Since there is no function declaration, this is one of the |
| 10973 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10974 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10975 | = cast<CXXConversionDecl>( |
| 10976 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 10977 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10978 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10979 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 10980 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10981 | // We selected one of the surrogate functions that converts the |
| 10982 | // object parameter to a function pointer. Perform the conversion |
| 10983 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10984 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 10985 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 10986 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10987 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 10988 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10989 | if (Call.isInvalid()) |
| 10990 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 10991 | // Record usage of conversion in an implicit cast. |
| 10992 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 10993 | CK_UserDefinedConversion, |
| 10994 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10995 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10996 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10997 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10998 | } |
| 10999 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11000 | MarkFunctionReferenced(LParenLoc, Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11001 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 11002 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11003 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11004 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11005 | // that calls this method, using Object for the implicit object |
| 11006 | // parameter and passing along the remaining arguments. |
| 11007 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11008 | |
| 11009 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11010 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11011 | return ExprError(); |
| 11012 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11013 | const FunctionProtoType *Proto = |
| 11014 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11015 | |
| 11016 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 11017 | unsigned NumArgsToCheck = NumArgs; |
| 11018 | |
| 11019 | // Build the full argument list for the method call (the |
| 11020 | // implicit object parameter is placed at the beginning of the |
| 11021 | // list). |
| 11022 | Expr **MethodArgs; |
| 11023 | if (NumArgs < NumArgsInProto) { |
| 11024 | NumArgsToCheck = NumArgsInProto; |
| 11025 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 11026 | } else { |
| 11027 | MethodArgs = new Expr*[NumArgs + 1]; |
| 11028 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11029 | MethodArgs[0] = Object.get(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11030 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 11031 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11032 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11033 | DeclarationNameInfo OpLocInfo( |
| 11034 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11035 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11036 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11037 | HadMultipleCandidates, |
| 11038 | OpLocInfo.getLoc(), |
| 11039 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11040 | if (NewFn.isInvalid()) |
| 11041 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11042 | |
| 11043 | // Once we've built TheCall, all of the expressions are properly |
| 11044 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11045 | QualType ResultTy = Method->getResultType(); |
| 11046 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11047 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11048 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11049 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11050 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11051 | llvm::makeArrayRef(MethodArgs, NumArgs+1), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11052 | ResultTy, VK, RParenLoc, false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11053 | delete [] MethodArgs; |
| 11054 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11055 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11056 | Method)) |
| 11057 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11058 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11059 | // We may have default arguments. If so, we need to allocate more |
| 11060 | // slots in the call for them. |
| 11061 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11062 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11063 | else if (NumArgs > NumArgsInProto) |
| 11064 | NumArgsToCheck = NumArgsInProto; |
| 11065 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11066 | bool IsError = false; |
| 11067 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11068 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11069 | ExprResult ObjRes = |
| 11070 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11071 | Best->FoundDecl, Method); |
| 11072 | if (ObjRes.isInvalid()) |
| 11073 | IsError = true; |
| 11074 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11075 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11076 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11077 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11078 | // Check the argument types. |
| 11079 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11080 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11081 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11082 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11083 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11084 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11085 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11086 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11087 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11088 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11089 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11090 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11091 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11092 | IsError |= InputInit.isInvalid(); |
| 11093 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11094 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11095 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11096 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11097 | if (DefArg.isInvalid()) { |
| 11098 | IsError = true; |
| 11099 | break; |
| 11100 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11101 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11102 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11103 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11104 | |
| 11105 | TheCall->setArg(i + 1, Arg); |
| 11106 | } |
| 11107 | |
| 11108 | // If this is a variadic call, handle args passed through "...". |
| 11109 | if (Proto->isVariadic()) { |
| 11110 | // Promote the arguments (C99 6.5.2.2p7). |
Aaron Ballman | 4914c28 | 2012-07-20 20:40:35 +0000 | [diff] [blame] | 11111 | for (unsigned i = NumArgsInProto; i < NumArgs; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11112 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11113 | IsError |= Arg.isInvalid(); |
| 11114 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11115 | } |
| 11116 | } |
| 11117 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11118 | if (IsError) return true; |
| 11119 | |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11120 | DiagnoseSentinelCalls(Method, LParenLoc, Args, NumArgs); |
| 11121 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11122 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11123 | return true; |
| 11124 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11125 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11126 | } |
| 11127 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11128 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11129 | /// (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] | 11130 | /// @c Member is the name of the member we're trying to find. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11131 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11132 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11133 | assert(Base->getType()->isRecordType() && |
| 11134 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11135 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11136 | if (checkPlaceholderForOverload(*this, Base)) |
| 11137 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11138 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11139 | SourceLocation Loc = Base->getExprLoc(); |
| 11140 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11141 | // C++ [over.ref]p1: |
| 11142 | // |
| 11143 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11144 | // for a class object x of type T if T::operator->() exists and if |
| 11145 | // the operator is selected as the best match function by the |
| 11146 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11147 | DeclarationName OpName = |
| 11148 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11149 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11150 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11151 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11152 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11153 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11154 | return ExprError(); |
| 11155 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11156 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11157 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11158 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11159 | |
| 11160 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11161 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11162 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 11163 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11164 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11165 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11166 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11167 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11168 | // Perform overload resolution. |
| 11169 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11170 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11171 | case OR_Success: |
| 11172 | // Overload resolution succeeded; we'll build the call below. |
| 11173 | break; |
| 11174 | |
| 11175 | case OR_No_Viable_Function: |
| 11176 | if (CandidateSet.empty()) |
| 11177 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11178 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11179 | else |
| 11180 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11181 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11182 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11183 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11184 | |
| 11185 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11186 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11187 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11188 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11189 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11190 | |
| 11191 | case OR_Deleted: |
| 11192 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11193 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11194 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11195 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11196 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11197 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11198 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11199 | } |
| 11200 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11201 | MarkFunctionReferenced(OpLoc, Best->Function); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11202 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 11203 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11204 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11205 | // Convert the object parameter. |
| 11206 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11207 | ExprResult BaseResult = |
| 11208 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11209 | Best->FoundDecl, Method); |
| 11210 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11211 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11212 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11213 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11214 | // Build the operator call. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11215 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11216 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11217 | if (FnExpr.isInvalid()) |
| 11218 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11219 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11220 | QualType ResultTy = Method->getResultType(); |
| 11221 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11222 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11223 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11224 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11225 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11226 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11227 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11228 | Method)) |
| 11229 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11230 | |
| 11231 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11232 | } |
| 11233 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11234 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11235 | /// a literal operator described by the provided lookup results. |
| 11236 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11237 | DeclarationNameInfo &SuffixInfo, |
| 11238 | ArrayRef<Expr*> Args, |
| 11239 | SourceLocation LitEndLoc, |
| 11240 | TemplateArgumentListInfo *TemplateArgs) { |
| 11241 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11242 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11243 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11244 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11245 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11246 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11247 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11248 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11249 | // Perform overload resolution. This will usually be trivial, but might need |
| 11250 | // to perform substitutions for a literal operator template. |
| 11251 | OverloadCandidateSet::iterator Best; |
| 11252 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11253 | case OR_Success: |
| 11254 | case OR_Deleted: |
| 11255 | break; |
| 11256 | |
| 11257 | case OR_No_Viable_Function: |
| 11258 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11259 | << R.getLookupName(); |
| 11260 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11261 | return ExprError(); |
| 11262 | |
| 11263 | case OR_Ambiguous: |
| 11264 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11265 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11266 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11267 | } |
| 11268 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11269 | FunctionDecl *FD = Best->Function; |
| 11270 | MarkFunctionReferenced(UDSuffixLoc, FD); |
| 11271 | DiagnoseUseOfDecl(Best->FoundDecl, UDSuffixLoc); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11272 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11273 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, HadMultipleCandidates, |
| 11274 | SuffixInfo.getLoc(), |
| 11275 | SuffixInfo.getInfo()); |
| 11276 | if (Fn.isInvalid()) |
| 11277 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11278 | |
| 11279 | // Check the argument types. This should almost always be a no-op, except |
| 11280 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11281 | Expr *ConvArgs[2]; |
| 11282 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { |
| 11283 | ExprResult InputInit = PerformCopyInitialization( |
| 11284 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11285 | SourceLocation(), Args[ArgIdx]); |
| 11286 | if (InputInit.isInvalid()) |
| 11287 | return true; |
| 11288 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11289 | } |
| 11290 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11291 | QualType ResultTy = FD->getResultType(); |
| 11292 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11293 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11294 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11295 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11296 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11297 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11298 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11299 | |
| 11300 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11301 | return ExprError(); |
| 11302 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11303 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11304 | return ExprError(); |
| 11305 | |
| 11306 | return MaybeBindToTemporary(UDL); |
| 11307 | } |
| 11308 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11309 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11310 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11311 | /// will be invoked. Otherwise, the function will be found via argument |
| 11312 | /// dependent lookup. |
| 11313 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11314 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11315 | /// is returned. |
| 11316 | Sema::ForRangeStatus |
| 11317 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11318 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11319 | BeginEndFunction BEF, |
| 11320 | const DeclarationNameInfo &NameInfo, |
| 11321 | LookupResult &MemberLookup, |
| 11322 | OverloadCandidateSet *CandidateSet, |
| 11323 | Expr *Range, ExprResult *CallExpr) { |
| 11324 | CandidateSet->clear(); |
| 11325 | if (!MemberLookup.empty()) { |
| 11326 | ExprResult MemberRef = |
| 11327 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11328 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11329 | /*TemplateKWLoc=*/SourceLocation(), |
| 11330 | /*FirstQualifierInScope=*/0, |
| 11331 | MemberLookup, |
| 11332 | /*TemplateArgs=*/0); |
| 11333 | if (MemberRef.isInvalid()) { |
| 11334 | *CallExpr = ExprError(); |
| 11335 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11336 | << RangeLoc << BEF << Range->getType(); |
| 11337 | return FRS_DiagnosticIssued; |
| 11338 | } |
| 11339 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, MultiExprArg(), Loc, 0); |
| 11340 | if (CallExpr->isInvalid()) { |
| 11341 | *CallExpr = ExprError(); |
| 11342 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11343 | << RangeLoc << BEF << Range->getType(); |
| 11344 | return FRS_DiagnosticIssued; |
| 11345 | } |
| 11346 | } else { |
| 11347 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11348 | UnresolvedLookupExpr *Fn = |
| 11349 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11350 | NestedNameSpecifierLoc(), NameInfo, |
| 11351 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11352 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11353 | |
| 11354 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, &Range, 1, Loc, |
| 11355 | CandidateSet, CallExpr); |
| 11356 | if (CandidateSet->empty() || CandidateSetError) { |
| 11357 | *CallExpr = ExprError(); |
| 11358 | return FRS_NoViableFunction; |
| 11359 | } |
| 11360 | OverloadCandidateSet::iterator Best; |
| 11361 | OverloadingResult OverloadResult = |
| 11362 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11363 | |
| 11364 | if (OverloadResult == OR_No_Viable_Function) { |
| 11365 | *CallExpr = ExprError(); |
| 11366 | return FRS_NoViableFunction; |
| 11367 | } |
| 11368 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, &Range, 1, |
| 11369 | Loc, 0, CandidateSet, &Best, |
| 11370 | OverloadResult, |
| 11371 | /*AllowTypoCorrection=*/false); |
| 11372 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11373 | *CallExpr = ExprError(); |
| 11374 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11375 | << RangeLoc << BEF << Range->getType(); |
| 11376 | return FRS_DiagnosticIssued; |
| 11377 | } |
| 11378 | } |
| 11379 | return FRS_Success; |
| 11380 | } |
| 11381 | |
| 11382 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11383 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11384 | /// a C++ overloaded function (possibly with some parentheses and |
| 11385 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11386 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11387 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11388 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11389 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11390 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11391 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11392 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11393 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11394 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11395 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11396 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11397 | } |
| 11398 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11399 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11400 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11401 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11402 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11403 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11404 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11405 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11406 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11407 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11408 | |
| 11409 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11410 | ICE->getCastKind(), |
| 11411 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11412 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11413 | } |
| 11414 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11415 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11416 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11417 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11418 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11419 | if (Method->isStatic()) { |
| 11420 | // Do nothing: static member functions aren't any different |
| 11421 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11422 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11423 | // Fix the sub expression, which really has to be an |
| 11424 | // UnresolvedLookupExpr holding an overloaded member function |
| 11425 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11426 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11427 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11428 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11429 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11430 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11431 | assert(isa<DeclRefExpr>(SubExpr) |
| 11432 | && "fixed to something other than a decl ref"); |
| 11433 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11434 | && "fixed to a member ref with no nested name qualifier"); |
| 11435 | |
| 11436 | // We have taken the address of a pointer to member |
| 11437 | // function. Perform the computation here so that we get the |
| 11438 | // appropriate pointer to member type. |
| 11439 | QualType ClassType |
| 11440 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11441 | QualType MemPtrType |
| 11442 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11443 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11444 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11445 | VK_RValue, OK_Ordinary, |
| 11446 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11447 | } |
| 11448 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11449 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11450 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11451 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11452 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11453 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11454 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11455 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11456 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11457 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11458 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11459 | |
| 11460 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11461 | // FIXME: avoid copy. |
| 11462 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11463 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11464 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11465 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11466 | } |
| 11467 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11468 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11469 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11470 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11471 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11472 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11473 | ULE->getNameLoc(), |
| 11474 | Fn->getType(), |
| 11475 | VK_LValue, |
| 11476 | Found.getDecl(), |
| 11477 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11478 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11479 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11480 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11481 | } |
| 11482 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11483 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11484 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11485 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11486 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11487 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11488 | TemplateArgs = &TemplateArgsBuffer; |
| 11489 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11490 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11491 | Expr *Base; |
| 11492 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11493 | // If we're filling in a static method where we used to have an |
| 11494 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11495 | if (MemExpr->isImplicitAccess()) { |
| 11496 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11497 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11498 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11499 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11500 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11501 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11502 | MemExpr->getMemberLoc(), |
| 11503 | Fn->getType(), |
| 11504 | VK_LValue, |
| 11505 | Found.getDecl(), |
| 11506 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11507 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11508 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11509 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11510 | } else { |
| 11511 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11512 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11513 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11514 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11515 | Base = new (Context) CXXThisExpr(Loc, |
| 11516 | MemExpr->getBaseType(), |
| 11517 | /*isImplicit=*/true); |
| 11518 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11519 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11520 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11521 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11522 | ExprValueKind valueKind; |
| 11523 | QualType type; |
| 11524 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11525 | valueKind = VK_LValue; |
| 11526 | type = Fn->getType(); |
| 11527 | } else { |
| 11528 | valueKind = VK_RValue; |
| 11529 | type = Context.BoundMemberTy; |
| 11530 | } |
| 11531 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11532 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11533 | MemExpr->isArrow(), |
| 11534 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11535 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11536 | Fn, |
| 11537 | Found, |
| 11538 | MemExpr->getMemberNameInfo(), |
| 11539 | TemplateArgs, |
| 11540 | type, valueKind, OK_Ordinary); |
| 11541 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11542 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11543 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11545 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11546 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11547 | } |
| 11548 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11549 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11550 | DeclAccessPair Found, |
| 11551 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11552 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11553 | } |
| 11554 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11555 | } // end namespace clang |