Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading -------------------------------===// |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 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" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 23 | #include "clang/Basic/DiagnosticOptions.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Initialization.h" |
| 27 | #include "clang/Sema/Lookup.h" |
| 28 | #include "clang/Sema/SemaInternal.h" |
| 29 | #include "clang/Sema/Template.h" |
| 30 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 36 | #include <cstdlib> |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 37 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 38 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 39 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 40 | |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 41 | /// A convenience routine for creating a decayed reference to a function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 42 | static ExprResult |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 43 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| 44 | bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 45 | SourceLocation Loc = SourceLocation(), |
| 46 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 47 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 48 | return ExprError(); |
| 49 | // If FoundDecl is different from Fn (such as if one is a template |
| 50 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 51 | // called on both. |
| 52 | // FIXME: This would be more comprehensively addressed by modifying |
| 53 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 54 | // being used. |
| 55 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 56 | return ExprError(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 57 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 58 | VK_LValue, Loc, LocInfo); |
| 59 | if (HadMultipleCandidates) |
| 60 | DRE->setHadMultipleCandidates(true); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 61 | |
| 62 | S.MarkDeclRefReferenced(DRE); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 63 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 64 | ExprResult E = DRE; |
| 65 | E = S.DefaultFunctionArrayConversion(E.get()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 66 | if (E.isInvalid()) |
| 67 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 68 | return E; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 71 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 72 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 73 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 74 | bool CStyle, |
| 75 | bool AllowObjCWritebackConversion); |
Sam Panzer | d012586 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 76 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 77 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 78 | QualType &ToType, |
| 79 | bool InOverloadResolution, |
| 80 | StandardConversionSequence &SCS, |
| 81 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 82 | static OverloadingResult |
| 83 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 84 | UserDefinedConversionSequence& User, |
| 85 | OverloadCandidateSet& Conversions, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 86 | bool AllowExplicit, |
| 87 | bool AllowObjCConversionOnExplicit); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | static ImplicitConversionSequence::CompareKind |
| 91 | CompareStandardConversionSequences(Sema &S, |
| 92 | const StandardConversionSequence& SCS1, |
| 93 | const StandardConversionSequence& SCS2); |
| 94 | |
| 95 | static ImplicitConversionSequence::CompareKind |
| 96 | CompareQualificationConversions(Sema &S, |
| 97 | const StandardConversionSequence& SCS1, |
| 98 | const StandardConversionSequence& SCS2); |
| 99 | |
| 100 | static ImplicitConversionSequence::CompareKind |
| 101 | CompareDerivedToBaseConversions(Sema &S, |
| 102 | const StandardConversionSequence& SCS1, |
| 103 | const StandardConversionSequence& SCS2); |
| 104 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 106 | /// corresponding to the given implicit conversion kind. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 107 | ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | static const ImplicitConversionRank |
| 109 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 110 | ICR_Exact_Match, |
| 111 | ICR_Exact_Match, |
| 112 | ICR_Exact_Match, |
| 113 | ICR_Exact_Match, |
| 114 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 115 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 116 | ICR_Promotion, |
| 117 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 118 | ICR_Promotion, |
| 119 | ICR_Conversion, |
| 120 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 121 | ICR_Conversion, |
| 122 | ICR_Conversion, |
| 123 | ICR_Conversion, |
| 124 | ICR_Conversion, |
| 125 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 126 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 127 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 128 | ICR_Conversion, |
| 129 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 130 | ICR_Complex_Real_Conversion, |
| 131 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 132 | ICR_Conversion, |
| 133 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 134 | }; |
| 135 | return Rank[(int)Kind]; |
| 136 | } |
| 137 | |
| 138 | /// GetImplicitConversionName - Return the name of this kind of |
| 139 | /// implicit conversion. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 140 | static const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 141 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | "No conversion", |
| 143 | "Lvalue-to-rvalue", |
| 144 | "Array-to-pointer", |
| 145 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 146 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | "Qualification", |
| 148 | "Integral promotion", |
| 149 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 150 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 151 | "Integral conversion", |
| 152 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 153 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 154 | "Floating-integral conversion", |
| 155 | "Pointer conversion", |
| 156 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 157 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 158 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 159 | "Derived-to-base conversion", |
| 160 | "Vector conversion", |
| 161 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 162 | "Complex-real conversion", |
| 163 | "Block Pointer conversion", |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 164 | "Transparent Union Conversion", |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 165 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 166 | }; |
| 167 | return Name[Kind]; |
| 168 | } |
| 169 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 170 | /// StandardConversionSequence - Set the standard conversion |
| 171 | /// sequence to the identity conversion. |
| 172 | void StandardConversionSequence::setAsIdentityConversion() { |
| 173 | First = ICK_Identity; |
| 174 | Second = ICK_Identity; |
| 175 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 176 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 177 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 178 | ReferenceBinding = false; |
| 179 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 180 | IsLvalueReference = true; |
| 181 | BindsToFunctionLvalue = false; |
| 182 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 183 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 184 | ObjCLifetimeConversionBinding = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 185 | CopyConstructor = nullptr; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 188 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 189 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 190 | /// implicit conversions. |
| 191 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 192 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 193 | if (GetConversionRank(First) > Rank) |
| 194 | Rank = GetConversionRank(First); |
| 195 | if (GetConversionRank(Second) > Rank) |
| 196 | Rank = GetConversionRank(Second); |
| 197 | if (GetConversionRank(Third) > Rank) |
| 198 | Rank = GetConversionRank(Third); |
| 199 | return Rank; |
| 200 | } |
| 201 | |
| 202 | /// isPointerConversionToBool - Determines whether this conversion is |
| 203 | /// 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] | 204 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 207 | // Note that FromType has not necessarily been transformed by the |
| 208 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 209 | // check for their presence as well as checking whether FromType is |
| 210 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 211 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 212 | (getFromType()->isPointerType() || |
| 213 | getFromType()->isObjCObjectPointerType() || |
| 214 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 215 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 216 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 217 | return true; |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 222 | /// isPointerConversionToVoidPointer - Determines whether this |
| 223 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 224 | /// used as part of the ranking of standard conversion sequences (C++ |
| 225 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 227 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 229 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 230 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 231 | |
| 232 | // Note that FromType has not necessarily been transformed by the |
| 233 | // array-to-pointer implicit conversion, so check for its presence |
| 234 | // and redo the conversion to get a pointer. |
| 235 | if (First == ICK_Array_To_Pointer) |
| 236 | FromType = Context.getArrayDecayedType(FromType); |
| 237 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 238 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 239 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 240 | return ToPtrType->getPointeeType()->isVoidType(); |
| 241 | |
| 242 | return false; |
| 243 | } |
| 244 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 245 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 246 | /// or after one in an implicit conversion. |
| 247 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 248 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 249 | switch (ICE->getCastKind()) { |
| 250 | case CK_NoOp: |
| 251 | case CK_IntegralCast: |
| 252 | case CK_IntegralToBoolean: |
| 253 | case CK_IntegralToFloating: |
| 254 | case CK_FloatingToIntegral: |
| 255 | case CK_FloatingToBoolean: |
| 256 | case CK_FloatingCast: |
| 257 | Converted = ICE->getSubExpr(); |
| 258 | continue; |
| 259 | |
| 260 | default: |
| 261 | return Converted; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return Converted; |
| 266 | } |
| 267 | |
| 268 | /// Check if this standard conversion sequence represents a narrowing |
| 269 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 270 | /// |
| 271 | /// \param Ctx The AST context. |
| 272 | /// \param Converted The result of applying this standard conversion sequence. |
| 273 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 274 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 275 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 276 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 277 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 278 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 279 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 280 | APValue &ConstantValue, |
| 281 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 282 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 283 | |
| 284 | // C++11 [dcl.init.list]p7: |
| 285 | // A narrowing conversion is an implicit conversion ... |
| 286 | QualType FromType = getToType(0); |
| 287 | QualType ToType = getToType(1); |
| 288 | switch (Second) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 289 | // 'bool' is an integral type; dispatch to the right place to handle it. |
| 290 | case ICK_Boolean_Conversion: |
| 291 | if (FromType->isRealFloatingType()) |
| 292 | goto FloatingIntegralConversion; |
| 293 | if (FromType->isIntegralOrUnscopedEnumerationType()) |
| 294 | goto IntegralConversion; |
| 295 | // Boolean conversions can be from pointers and pointers to members |
| 296 | // [conv.bool], and those aren't considered narrowing conversions. |
| 297 | return NK_Not_Narrowing; |
| 298 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 299 | // -- from a floating-point type to an integer type, or |
| 300 | // |
| 301 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 302 | // type, except where the source is a constant expression and the actual |
| 303 | // value after conversion will fit into the target type and will produce |
| 304 | // the original value when converted back to the original type, or |
| 305 | case ICK_Floating_Integral: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 306 | FloatingIntegralConversion: |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 307 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 308 | return NK_Type_Narrowing; |
| 309 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 310 | llvm::APSInt IntConstantValue; |
| 311 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 312 | if (Initializer && |
| 313 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 314 | // Convert the integer to the floating type. |
| 315 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 316 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 317 | llvm::APFloat::rmNearestTiesToEven); |
| 318 | // And back. |
| 319 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 320 | bool ignored; |
| 321 | Result.convertToInteger(ConvertedValue, |
| 322 | llvm::APFloat::rmTowardZero, &ignored); |
| 323 | // If the resulting value is different, this was a narrowing conversion. |
| 324 | if (IntConstantValue != ConvertedValue) { |
| 325 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 326 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 327 | return NK_Constant_Narrowing; |
| 328 | } |
| 329 | } else { |
| 330 | // Variables are always narrowings. |
| 331 | return NK_Variable_Narrowing; |
| 332 | } |
| 333 | } |
| 334 | return NK_Not_Narrowing; |
| 335 | |
| 336 | // -- from long double to double or float, or from double to float, except |
| 337 | // where the source is a constant expression and the actual value after |
| 338 | // conversion is within the range of values that can be represented (even |
| 339 | // if it cannot be represented exactly), or |
| 340 | case ICK_Floating_Conversion: |
| 341 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 342 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 343 | // FromType is larger than ToType. |
| 344 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 345 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 346 | // Constant! |
| 347 | assert(ConstantValue.isFloat()); |
| 348 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 349 | // Convert the source value into the target type. |
| 350 | bool ignored; |
| 351 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 352 | Ctx.getFloatTypeSemantics(ToType), |
| 353 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 354 | // If there was no overflow, the source value is within the range of |
| 355 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 356 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 357 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 358 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 359 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 360 | } else { |
| 361 | return NK_Variable_Narrowing; |
| 362 | } |
| 363 | } |
| 364 | return NK_Not_Narrowing; |
| 365 | |
| 366 | // -- from an integer type or unscoped enumeration type to an integer type |
| 367 | // that cannot represent all the values of the original type, except where |
| 368 | // the source is a constant expression and the actual value after |
| 369 | // conversion will fit into the target type and will produce the original |
| 370 | // value when converted back to the original type. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 371 | case ICK_Integral_Conversion: |
| 372 | IntegralConversion: { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 373 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 374 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 375 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 376 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 377 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 378 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 379 | |
| 380 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 381 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 382 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 383 | // Not all values of FromType can be represented in ToType. |
| 384 | llvm::APSInt InitializerValue; |
| 385 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 386 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 387 | // Such conversions on variables are always narrowing. |
| 388 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 389 | } |
| 390 | bool Narrowing = false; |
| 391 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 392 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 393 | // narrowing. |
| 394 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 395 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 396 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 397 | // Add a bit to the InitializerValue so we don't have to worry about |
| 398 | // signed vs. unsigned comparisons. |
| 399 | InitializerValue = InitializerValue.extend( |
| 400 | InitializerValue.getBitWidth() + 1); |
| 401 | // Convert the initializer to and from the target width and signed-ness. |
| 402 | llvm::APSInt ConvertedValue = InitializerValue; |
| 403 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 404 | ConvertedValue.setIsSigned(ToSigned); |
| 405 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 406 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 407 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 408 | if (ConvertedValue != InitializerValue) |
| 409 | Narrowing = true; |
| 410 | } |
| 411 | if (Narrowing) { |
| 412 | ConstantType = Initializer->getType(); |
| 413 | ConstantValue = APValue(InitializerValue); |
| 414 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | return NK_Not_Narrowing; |
| 418 | } |
| 419 | |
| 420 | default: |
| 421 | // Other kinds of conversions are not narrowings. |
| 422 | return NK_Not_Narrowing; |
| 423 | } |
| 424 | } |
| 425 | |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 426 | /// dump - Print this standard conversion sequence to standard |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 427 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 428 | void StandardConversionSequence::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 429 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 430 | bool PrintedSomething = false; |
| 431 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 432 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 433 | PrintedSomething = true; |
| 434 | } |
| 435 | |
| 436 | if (Second != ICK_Identity) { |
| 437 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 438 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 439 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 440 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 441 | |
| 442 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 443 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 444 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 445 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 446 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 447 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 448 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 449 | PrintedSomething = true; |
| 450 | } |
| 451 | |
| 452 | if (Third != ICK_Identity) { |
| 453 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 454 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 455 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 456 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | PrintedSomething = true; |
| 458 | } |
| 459 | |
| 460 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 461 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 465 | /// dump - Print this user-defined conversion sequence to standard |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 466 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 467 | void UserDefinedConversionSequence::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 468 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 469 | if (Before.First || Before.Second || Before.Third) { |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 470 | Before.dump(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 471 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 472 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 473 | if (ConversionFunction) |
| 474 | OS << '\'' << *ConversionFunction << '\''; |
| 475 | else |
| 476 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 477 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 478 | OS << " -> "; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 479 | After.dump(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 483 | /// dump - Print this implicit conversion sequence to standard |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 484 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 485 | void ImplicitConversionSequence::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 486 | raw_ostream &OS = llvm::errs(); |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 487 | if (isStdInitializerListElement()) |
| 488 | OS << "Worst std::initializer_list element conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 489 | switch (ConversionKind) { |
| 490 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 491 | OS << "Standard conversion: "; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 492 | Standard.dump(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 493 | break; |
| 494 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 495 | OS << "User-defined conversion: "; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 496 | UserDefined.dump(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 497 | break; |
| 498 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 499 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 500 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 501 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 502 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 503 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 505 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 509 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | } |
| 511 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 512 | void AmbiguousConversionSequence::construct() { |
| 513 | new (&conversions()) ConversionSet(); |
| 514 | } |
| 515 | |
| 516 | void AmbiguousConversionSequence::destruct() { |
| 517 | conversions().~ConversionSet(); |
| 518 | } |
| 519 | |
| 520 | void |
| 521 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 522 | FromTypePtr = O.FromTypePtr; |
| 523 | ToTypePtr = O.ToTypePtr; |
| 524 | new (&conversions()) ConversionSet(O.conversions()); |
| 525 | } |
| 526 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 527 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 528 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 529 | // template argument information. |
| 530 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 531 | TemplateArgument FirstArg; |
| 532 | TemplateArgument SecondArg; |
| 533 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 534 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 535 | // template parameter and template argument information. |
| 536 | struct DFIParamWithArguments : DFIArguments { |
| 537 | TemplateParameter Param; |
| 538 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 539 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 540 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 541 | /// \brief Convert from Sema's representation of template deduction information |
| 542 | /// to the form used in overload-candidate information. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 543 | DeductionFailureInfo |
| 544 | clang::MakeDeductionFailureInfo(ASTContext &Context, |
| 545 | Sema::TemplateDeductionResult TDK, |
| 546 | TemplateDeductionInfo &Info) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 547 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 548 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 549 | Result.HasDiagnostic = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 550 | Result.Data = nullptr; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 551 | switch (TDK) { |
| 552 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 553 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 554 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 555 | case Sema::TDK_TooManyArguments: |
| 556 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 557 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 559 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 560 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 561 | Result.Data = Info.Param.getOpaqueValue(); |
| 562 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 563 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 564 | case Sema::TDK_NonDeducedMismatch: { |
| 565 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 566 | DFIArguments *Saved = new (Context) DFIArguments; |
| 567 | Saved->FirstArg = Info.FirstArg; |
| 568 | Saved->SecondArg = Info.SecondArg; |
| 569 | Result.Data = Saved; |
| 570 | break; |
| 571 | } |
| 572 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 573 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 574 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 575 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 576 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 577 | Saved->Param = Info.Param; |
| 578 | Saved->FirstArg = Info.FirstArg; |
| 579 | Saved->SecondArg = Info.SecondArg; |
| 580 | Result.Data = Saved; |
| 581 | break; |
| 582 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 584 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 585 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 586 | if (Info.hasSFINAEDiagnostic()) { |
| 587 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 588 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 589 | Info.takeSFINAEDiagnostic(*Diag); |
| 590 | Result.HasDiagnostic = true; |
| 591 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 592 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 594 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 595 | Result.Data = Info.Expression; |
| 596 | break; |
| 597 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 598 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 600 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 602 | return Result; |
| 603 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 604 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 605 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 606 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 607 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 608 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 609 | case Sema::TDK_InstantiationDepth: |
| 610 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 611 | case Sema::TDK_TooManyArguments: |
| 612 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 613 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 614 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 615 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 617 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 618 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 619 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 620 | // FIXME: Destroy the data? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 621 | Data = nullptr; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 622 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 623 | |
| 624 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 625 | // FIXME: Destroy the template argument list? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 626 | Data = nullptr; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 627 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 628 | Diag->~PartialDiagnosticAt(); |
| 629 | HasDiagnostic = false; |
| 630 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 631 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 633 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 634 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 635 | break; |
| 636 | } |
| 637 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 638 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 639 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 640 | if (HasDiagnostic) |
| 641 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 642 | return nullptr; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 645 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 646 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 647 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 648 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 649 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 650 | case Sema::TDK_TooManyArguments: |
| 651 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 652 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 653 | case Sema::TDK_NonDeducedMismatch: |
| 654 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 655 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 657 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 658 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 659 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 660 | |
| 661 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 662 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 663 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 665 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 666 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 670 | return TemplateParameter(); |
| 671 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 672 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 673 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 674 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 675 | case Sema::TDK_Success: |
| 676 | case Sema::TDK_Invalid: |
| 677 | case Sema::TDK_InstantiationDepth: |
| 678 | case Sema::TDK_TooManyArguments: |
| 679 | case Sema::TDK_TooFewArguments: |
| 680 | case Sema::TDK_Incomplete: |
| 681 | case Sema::TDK_InvalidExplicitArguments: |
| 682 | case Sema::TDK_Inconsistent: |
| 683 | case Sema::TDK_Underqualified: |
| 684 | case Sema::TDK_NonDeducedMismatch: |
| 685 | case Sema::TDK_FailedOverloadResolution: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 686 | return nullptr; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 687 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 688 | case Sema::TDK_SubstitutionFailure: |
| 689 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 690 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 691 | // Unhandled |
| 692 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 693 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 696 | return nullptr; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 699 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 700 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 701 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 702 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 703 | case Sema::TDK_InstantiationDepth: |
| 704 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 705 | case Sema::TDK_TooManyArguments: |
| 706 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 707 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 708 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 709 | case Sema::TDK_FailedOverloadResolution: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 710 | return nullptr; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 711 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 712 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 713 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 714 | case Sema::TDK_NonDeducedMismatch: |
| 715 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 716 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 717 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 718 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 719 | break; |
| 720 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 721 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 722 | return nullptr; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 724 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 725 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 726 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 727 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 728 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 729 | case Sema::TDK_InstantiationDepth: |
| 730 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 731 | case Sema::TDK_TooManyArguments: |
| 732 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 733 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 734 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 735 | case Sema::TDK_FailedOverloadResolution: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 736 | return nullptr; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 737 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 738 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 739 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 740 | case Sema::TDK_NonDeducedMismatch: |
| 741 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 743 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 744 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 747 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 748 | return nullptr; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 751 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 752 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 753 | Sema::TDK_FailedOverloadResolution) |
| 754 | return static_cast<Expr*>(Data); |
| 755 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 756 | return nullptr; |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 759 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 760 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 761 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 762 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 763 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 764 | i->DeductionFailure.Destroy(); |
| 765 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | void OverloadCandidateSet::clear() { |
| 769 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 770 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 771 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 772 | Functions.clear(); |
| 773 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 774 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 775 | namespace { |
| 776 | class UnbridgedCastsSet { |
| 777 | struct Entry { |
| 778 | Expr **Addr; |
| 779 | Expr *Saved; |
| 780 | }; |
| 781 | SmallVector<Entry, 2> Entries; |
| 782 | |
| 783 | public: |
| 784 | void save(Sema &S, Expr *&E) { |
| 785 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 786 | Entry entry = { &E, E }; |
| 787 | Entries.push_back(entry); |
| 788 | E = S.stripARCUnbridgedCast(E); |
| 789 | } |
| 790 | |
| 791 | void restore() { |
| 792 | for (SmallVectorImpl<Entry>::iterator |
| 793 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 794 | *i->Addr = i->Saved; |
| 795 | } |
| 796 | }; |
| 797 | } |
| 798 | |
| 799 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 800 | /// preprocessing on the given expression. |
| 801 | /// |
| 802 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 803 | /// without this, they will be immediately diagnosed as errors |
| 804 | /// |
| 805 | /// Return true on unrecoverable error. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 806 | static bool |
| 807 | checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 808 | UnbridgedCastsSet *unbridgedCasts = nullptr) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 809 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 810 | // We can't handle overloaded expressions here because overload |
| 811 | // resolution might reasonably tweak them. |
| 812 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 813 | |
| 814 | // If the context potentially accepts unbridged ARC casts, strip |
| 815 | // the unbridged cast and add it to the collection for later restoration. |
| 816 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 817 | unbridgedCasts) { |
| 818 | unbridgedCasts->save(S, E); |
| 819 | return false; |
| 820 | } |
| 821 | |
| 822 | // Go ahead and check everything else. |
| 823 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 824 | if (result.isInvalid()) |
| 825 | return true; |
| 826 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 827 | E = result.get(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
| 831 | // Nothing to do. |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 836 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 837 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 838 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 839 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 840 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 841 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 842 | return true; |
| 843 | |
| 844 | return false; |
| 845 | } |
| 846 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 847 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 848 | // overload of the declarations in Old. This routine returns false if |
| 849 | // New and Old cannot be overloaded, e.g., if New has the same |
| 850 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 851 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 852 | // it does return false, MatchedDecl will point to the decl that New |
| 853 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 854 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 855 | // |
| 856 | // Example: Given the following input: |
| 857 | // |
| 858 | // void f(int, float); // #1 |
| 859 | // void f(int, int); // #2 |
| 860 | // int f(int, int); // #3 |
| 861 | // |
| 862 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 864 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 865 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 866 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 867 | // (since they have different signatures), so this routine returns |
| 868 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 869 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 870 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 871 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 872 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 873 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 874 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 875 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 876 | // |
| 877 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 878 | // into a class by a using declaration. The rules for whether to hide |
| 879 | // shadow declarations ignore some properties which otherwise figure |
| 880 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 881 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 882 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 883 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 884 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 885 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 886 | NamedDecl *OldD = *I; |
| 887 | |
| 888 | bool OldIsUsingDecl = false; |
| 889 | if (isa<UsingShadowDecl>(OldD)) { |
| 890 | OldIsUsingDecl = true; |
| 891 | |
| 892 | // We can always introduce two using declarations into the same |
| 893 | // context, even if they have identical signatures. |
| 894 | if (NewIsUsingDecl) continue; |
| 895 | |
| 896 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 897 | } |
| 898 | |
| 899 | // If either declaration was introduced by a using declaration, |
| 900 | // we'll need to use slightly different rules for matching. |
| 901 | // Essentially, these rules are the normal rules, except that |
| 902 | // function templates hide function templates with different |
| 903 | // return types or template parameter lists. |
| 904 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 905 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 906 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 907 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 908 | if (FunctionDecl *OldF = OldD->getAsFunction()) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 909 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 910 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 911 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 912 | continue; |
| 913 | } |
| 914 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 915 | if (!isa<FunctionTemplateDecl>(OldD) && |
| 916 | !shouldLinkPossiblyHiddenDecl(*I, New)) |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 917 | continue; |
| 918 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 919 | Match = *I; |
| 920 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 921 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 922 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 923 | // We can overload with these, which can show up when doing |
| 924 | // redeclaration checks for UsingDecls. |
| 925 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 926 | } else if (isa<TagDecl>(OldD)) { |
| 927 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 928 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 929 | // Optimistically assume that an unresolved using decl will |
| 930 | // overload; if it doesn't, we'll have to diagnose during |
| 931 | // template instantiation. |
| 932 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 933 | // (C++ 13p1): |
| 934 | // Only function declarations can be overloaded; object and type |
| 935 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 936 | Match = *I; |
| 937 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 938 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 939 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 940 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 941 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 944 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 945 | bool UseUsingDeclRules) { |
| 946 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 947 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 948 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 949 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 950 | // MSVCRT user defined entry points cannot be overloaded. |
| 951 | if (New->isMSVCRTEntryPoint()) |
| 952 | return false; |
| 953 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 954 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 955 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 956 | |
| 957 | // C++ [temp.fct]p2: |
| 958 | // A function template can be overloaded with other function templates |
| 959 | // and with normal (non-template) functions. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 960 | if ((OldTemplate == nullptr) != (NewTemplate == nullptr)) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 961 | return true; |
| 962 | |
| 963 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 964 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 965 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 966 | |
| 967 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 968 | // determine whether they are overloads. If we find any mismatch |
| 969 | // in the signature, they are overloads. |
| 970 | |
| 971 | // If either of these functions is a K&R-style function (no |
| 972 | // prototype), then we consider them to have matching signatures. |
| 973 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 974 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 975 | return false; |
| 976 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 977 | const FunctionProtoType *OldType = cast<FunctionProtoType>(OldQType); |
| 978 | const FunctionProtoType *NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 979 | |
| 980 | // The signature of a function includes the types of its |
| 981 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 982 | // of the ellipsis; see C++ DR 357). |
| 983 | if (OldQType != NewQType && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 984 | (OldType->getNumParams() != NewType->getNumParams() || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 985 | OldType->isVariadic() != NewType->isVariadic() || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 986 | !FunctionParamTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 987 | return true; |
| 988 | |
| 989 | // C++ [temp.over.link]p4: |
| 990 | // The signature of a function template consists of its function |
| 991 | // signature, its return type and its template parameter list. The names |
| 992 | // of the template parameters are significant only for establishing the |
| 993 | // relationship between the template parameters and the rest of the |
| 994 | // signature. |
| 995 | // |
| 996 | // We check the return type and template parameter lists for function |
| 997 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 998 | // |
| 999 | // However, we don't consider either of these when deciding whether |
| 1000 | // a member introduced by a shadow declaration is hidden. |
| 1001 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1002 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1003 | OldTemplate->getTemplateParameters(), |
| 1004 | false, TPL_TemplateMatch) || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1005 | OldType->getReturnType() != NewType->getReturnType())) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1006 | return true; |
| 1007 | |
| 1008 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1009 | // 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] | 1010 | // |
| 1011 | // As part of this, also check whether one of the member functions |
| 1012 | // is static, in which case they are not overloads (C++ |
| 1013 | // 13.1p2). While not part of the definition of the signature, |
| 1014 | // this check is important to determine whether these functions |
| 1015 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1016 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1017 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1018 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1019 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1020 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1021 | if (!UseUsingDeclRules && |
| 1022 | (OldMethod->getRefQualifier() == RQ_None || |
| 1023 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1024 | // C++0x [over.load]p2: |
| 1025 | // - Member function declarations with the same name and the same |
| 1026 | // parameter-type-list as well as member function template |
| 1027 | // declarations with the same name, the same parameter-type-list, and |
| 1028 | // the same template parameter lists cannot be overloaded if any of |
| 1029 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1030 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1031 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1032 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1033 | } |
| 1034 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1035 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1037 | // We may not have applied the implicit const for a constexpr member |
| 1038 | // function yet (because we haven't yet resolved whether this is a static |
| 1039 | // or non-static member function). Add it now, on the assumption that this |
| 1040 | // is a redeclaration of OldMethod. |
David Majnemer | 62e9370 | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1041 | unsigned OldQuals = OldMethod->getTypeQualifiers(); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1042 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1043 | if (!getLangOpts().CPlusPlus14 && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1044 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1045 | NewQuals |= Qualifiers::Const; |
David Majnemer | 62e9370 | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1046 | |
| 1047 | // We do not allow overloading based off of '__restrict'. |
| 1048 | OldQuals &= ~Qualifiers::Restrict; |
| 1049 | NewQuals &= ~Qualifiers::Restrict; |
| 1050 | if (OldQuals != NewQuals) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1051 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1052 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1053 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1054 | // enable_if attributes are an order-sensitive part of the signature. |
| 1055 | for (specific_attr_iterator<EnableIfAttr> |
| 1056 | NewI = New->specific_attr_begin<EnableIfAttr>(), |
| 1057 | NewE = New->specific_attr_end<EnableIfAttr>(), |
| 1058 | OldI = Old->specific_attr_begin<EnableIfAttr>(), |
| 1059 | OldE = Old->specific_attr_end<EnableIfAttr>(); |
| 1060 | NewI != NewE || OldI != OldE; ++NewI, ++OldI) { |
| 1061 | if (NewI == NewE || OldI == OldE) |
| 1062 | return true; |
| 1063 | llvm::FoldingSetNodeID NewID, OldID; |
| 1064 | NewI->getCond()->Profile(NewID, Context, true); |
| 1065 | OldI->getCond()->Profile(OldID, Context, true); |
| 1066 | if (NewID != OldID) |
| 1067 | return true; |
| 1068 | } |
| 1069 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1070 | // The signatures match; this is not an overload. |
| 1071 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1074 | /// \brief Checks availability of the function depending on the current |
| 1075 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1076 | /// |
| 1077 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1078 | /// an available function, false otherwise. |
| 1079 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1080 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1081 | } |
| 1082 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1083 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1084 | /// |
| 1085 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1086 | /// is not an option. See TryImplicitConversion for more information. |
| 1087 | static ImplicitConversionSequence |
| 1088 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1089 | bool SuppressUserConversions, |
| 1090 | bool AllowExplicit, |
| 1091 | bool InOverloadResolution, |
| 1092 | bool CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1093 | bool AllowObjCWritebackConversion, |
| 1094 | bool AllowObjCConversionOnExplicit) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1095 | ImplicitConversionSequence ICS; |
| 1096 | |
| 1097 | if (SuppressUserConversions) { |
| 1098 | // We're not in the case above, so there is no conversion that |
| 1099 | // we can perform. |
| 1100 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1101 | return ICS; |
| 1102 | } |
| 1103 | |
| 1104 | // Attempt user-defined conversion. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1105 | OverloadCandidateSet Conversions(From->getExprLoc(), |
| 1106 | OverloadCandidateSet::CSK_Normal); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1107 | switch (IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, |
| 1108 | Conversions, AllowExplicit, |
| 1109 | AllowObjCConversionOnExplicit)) { |
| 1110 | case OR_Success: |
| 1111 | case OR_Deleted: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1112 | ICS.setUserDefined(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1113 | ICS.UserDefined.Before.setAsIdentityConversion(); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1114 | // C++ [over.ics.user]p4: |
| 1115 | // A conversion of an expression of class type to the same class |
| 1116 | // type is given Exact Match rank, and a conversion of an |
| 1117 | // expression of class type to a base class of that type is |
| 1118 | // given Conversion rank, in spite of the fact that a copy |
| 1119 | // constructor (i.e., a user-defined conversion function) is |
| 1120 | // called for those cases. |
| 1121 | if (CXXConstructorDecl *Constructor |
| 1122 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1123 | QualType FromCanon |
| 1124 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1125 | QualType ToCanon |
| 1126 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1127 | if (Constructor->isCopyConstructor() && |
| 1128 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1129 | // Turn this into a "standard" conversion sequence, so that it |
| 1130 | // gets ranked with standard conversion sequences. |
| 1131 | ICS.setStandard(); |
| 1132 | ICS.Standard.setAsIdentityConversion(); |
| 1133 | ICS.Standard.setFromType(From->getType()); |
| 1134 | ICS.Standard.setAllToTypes(ToType); |
| 1135 | ICS.Standard.CopyConstructor = Constructor; |
| 1136 | if (ToCanon != FromCanon) |
| 1137 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1138 | } |
| 1139 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | |
| 1142 | case OR_Ambiguous: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1143 | ICS.setAmbiguous(); |
| 1144 | ICS.Ambiguous.setFromType(From->getType()); |
| 1145 | ICS.Ambiguous.setToType(ToType); |
| 1146 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1147 | Cand != Conversions.end(); ++Cand) |
| 1148 | if (Cand->Viable) |
| 1149 | ICS.Ambiguous.addConversion(Cand->Function); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1150 | break; |
| 1151 | |
| 1152 | // Fall through. |
| 1153 | case OR_No_Viable_Function: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1154 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1155 | break; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | return ICS; |
| 1159 | } |
| 1160 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1161 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1162 | /// from the given expression (Expr) to the given type (ToType). This |
| 1163 | /// function returns an implicit conversion sequence that can be used |
| 1164 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1165 | /// |
| 1166 | /// void f(float f); |
| 1167 | /// void g(int i) { f(i); } |
| 1168 | /// |
| 1169 | /// this routine would produce an implicit conversion sequence to |
| 1170 | /// describe the initialization of f from i, which will be a standard |
| 1171 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1172 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1173 | // |
| 1174 | /// Note that this routine only determines how the conversion can be |
| 1175 | /// performed; it does not actually perform the conversion. As such, |
| 1176 | /// it will not produce any diagnostics if no conversion is available, |
| 1177 | /// but will instead return an implicit conversion sequence of kind |
| 1178 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1179 | /// |
| 1180 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1181 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1182 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1183 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1184 | /// |
| 1185 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1186 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1187 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1188 | static ImplicitConversionSequence |
| 1189 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1190 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1191 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1192 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1193 | bool CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1194 | bool AllowObjCWritebackConversion, |
| 1195 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1196 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1197 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1198 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1199 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1200 | return ICS; |
| 1201 | } |
| 1202 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1203 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1204 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1205 | return ICS; |
| 1206 | } |
| 1207 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1208 | // C++ [over.ics.user]p4: |
| 1209 | // A conversion of an expression of class type to the same class |
| 1210 | // type is given Exact Match rank, and a conversion of an |
| 1211 | // expression of class type to a base class of that type is |
| 1212 | // given Conversion rank, in spite of the fact that a copy/move |
| 1213 | // constructor (i.e., a user-defined conversion function) is |
| 1214 | // called for those cases. |
| 1215 | QualType FromType = From->getType(); |
| 1216 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1217 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1218 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1219 | ICS.setStandard(); |
| 1220 | ICS.Standard.setAsIdentityConversion(); |
| 1221 | ICS.Standard.setFromType(FromType); |
| 1222 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1224 | // We don't actually check at this point whether there is a valid |
| 1225 | // copy/move constructor, since overloading just assumes that it |
| 1226 | // exists. When we actually perform initialization, we'll find the |
| 1227 | // appropriate constructor to copy the returned object, if needed. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1228 | ICS.Standard.CopyConstructor = nullptr; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1230 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1231 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1232 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1234 | return ICS; |
| 1235 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1236 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1237 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1238 | AllowExplicit, InOverloadResolution, CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1239 | AllowObjCWritebackConversion, |
| 1240 | AllowObjCConversionOnExplicit); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1243 | ImplicitConversionSequence |
| 1244 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1245 | bool SuppressUserConversions, |
| 1246 | bool AllowExplicit, |
| 1247 | bool InOverloadResolution, |
| 1248 | bool CStyle, |
| 1249 | bool AllowObjCWritebackConversion) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1250 | return ::TryImplicitConversion(*this, From, ToType, |
| 1251 | SuppressUserConversions, AllowExplicit, |
| 1252 | InOverloadResolution, CStyle, |
| 1253 | AllowObjCWritebackConversion, |
| 1254 | /*AllowObjCConversionOnExplicit=*/false); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1257 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1258 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1259 | /// converted expression. Flavor is the kind of conversion we're |
| 1260 | /// performing, used in the error message. If @p AllowExplicit, |
| 1261 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1262 | ExprResult |
| 1263 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1264 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1265 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1266 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1269 | ExprResult |
| 1270 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1271 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1272 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1273 | if (checkPlaceholderForOverload(*this, From)) |
| 1274 | return ExprError(); |
| 1275 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1276 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1277 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1278 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1279 | (Action == AA_Passing || Action == AA_Sending); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1280 | if (getLangOpts().ObjC1) |
| 1281 | CheckObjCBridgeRelatedConversions(From->getLocStart(), |
| 1282 | ToType, From->getType(), From); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1283 | ICS = ::TryImplicitConversion(*this, From, ToType, |
| 1284 | /*SuppressUserConversions=*/false, |
| 1285 | AllowExplicit, |
| 1286 | /*InOverloadResolution=*/false, |
| 1287 | /*CStyle=*/false, |
| 1288 | AllowObjCWritebackConversion, |
| 1289 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1290 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1291 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1292 | |
| 1293 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1294 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1295 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1296 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1297 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1298 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1299 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1300 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1301 | // where F adds one of the following at most once: |
| 1302 | // - a pointer |
| 1303 | // - a member pointer |
| 1304 | // - a block pointer |
| 1305 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1306 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1307 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1308 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1309 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1310 | if (TyClass == Type::Pointer) { |
| 1311 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1312 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1313 | } else if (TyClass == Type::BlockPointer) { |
| 1314 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1315 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1316 | } else if (TyClass == Type::MemberPointer) { |
| 1317 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1318 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1319 | } else { |
| 1320 | return false; |
| 1321 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1322 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1323 | TyClass = CanTo->getTypeClass(); |
| 1324 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1325 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1330 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1331 | if (!EInfo.getNoReturn()) return false; |
| 1332 | |
| 1333 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1334 | assert(QualType(FromFn, 0).isCanonical()); |
| 1335 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1336 | |
| 1337 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1338 | return true; |
| 1339 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1341 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1342 | /// vector conversion. |
| 1343 | /// |
| 1344 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1345 | /// conversion. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1346 | static bool IsVectorConversion(Sema &S, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1347 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1348 | // We need at least one of these types to be a vector type to have a vector |
| 1349 | // conversion. |
| 1350 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1351 | return false; |
| 1352 | |
| 1353 | // Identical types require no conversions. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1354 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1355 | return false; |
| 1356 | |
| 1357 | // There are no conversions between extended vector types, only identity. |
| 1358 | if (ToType->isExtVectorType()) { |
| 1359 | // There are no conversions between extended vector types other than the |
| 1360 | // identity conversion. |
| 1361 | if (FromType->isExtVectorType()) |
| 1362 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1364 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1365 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1366 | ICK = ICK_Vector_Splat; |
| 1367 | return true; |
| 1368 | } |
| 1369 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1370 | |
| 1371 | // We can perform the conversion between vector types in the following cases: |
| 1372 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1373 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1374 | // same size |
| 1375 | if (ToType->isVectorType() && FromType->isVectorType()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1376 | if (S.Context.areCompatibleVectorTypes(FromType, ToType) || |
| 1377 | S.isLaxVectorConversion(FromType, ToType)) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1378 | ICK = ICK_Vector_Conversion; |
| 1379 | return true; |
| 1380 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1381 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1382 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1385 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1386 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1387 | bool InOverloadResolution, |
| 1388 | StandardConversionSequence &SCS, |
| 1389 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1390 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1391 | /// IsStandardConversion - Determines whether there is a standard |
| 1392 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1393 | /// expression From to the type ToType. Standard conversion sequences |
| 1394 | /// only consider non-class types; for conversions that involve class |
| 1395 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1396 | /// contain the standard conversion sequence required to perform this |
| 1397 | /// conversion and this routine will return true. Otherwise, this |
| 1398 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1399 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1400 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1401 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1402 | bool CStyle, |
| 1403 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1404 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1406 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1407 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1408 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1409 | SCS.setFromType(FromType); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1410 | SCS.CopyConstructor = nullptr; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1412 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1414 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1415 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1416 | return false; |
| 1417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1421 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1422 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1423 | // (C++ 4p1). |
| 1424 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1425 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1426 | DeclAccessPair AccessPair; |
| 1427 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1428 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1429 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1430 | // We were able to resolve the address of the overloaded function, |
| 1431 | // so we can convert to the type of that function. |
| 1432 | FromType = Fn->getType(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1433 | SCS.setFromType(FromType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1434 | |
| 1435 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1436 | // if the type matches (identity) or we are converting to bool |
| 1437 | if (!S.Context.hasSameUnqualifiedType( |
| 1438 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1439 | QualType resultTy; |
| 1440 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1441 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1442 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1443 | // otherwise, only a boolean conversion is standard |
| 1444 | if (!ToType->isBooleanType()) |
| 1445 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1446 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1447 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1448 | // Check if the "from" expression is taking the address of an overloaded |
| 1449 | // function and recompute the FromType accordingly. Take advantage of the |
| 1450 | // fact that non-static member functions *must* have such an address-of |
| 1451 | // expression. |
| 1452 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1453 | if (Method && !Method->isStatic()) { |
| 1454 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1455 | "Non-unary operator on non-static member address"); |
| 1456 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1457 | == UO_AddrOf && |
| 1458 | "Non-address-of operator on non-static member address"); |
| 1459 | const Type *ClassType |
| 1460 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1461 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1462 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1463 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1464 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1465 | "Non-address-of operator for overloaded function expression"); |
| 1466 | FromType = S.Context.getPointerType(FromType); |
| 1467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1469 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1470 | assert(S.Context.hasSameType( |
| 1471 | FromType, |
| 1472 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1473 | } else { |
| 1474 | return false; |
| 1475 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1476 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1477 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1478 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1479 | // be converted to a prvalue. |
| 1480 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1481 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1482 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1483 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1484 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1486 | // C11 6.3.2.1p2: |
| 1487 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1488 | // of the type of the lvalue ... |
| 1489 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1490 | FromType = Atomic->getValueType(); |
| 1491 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1492 | // If T is a non-class type, the type of the rvalue is the |
| 1493 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1494 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1495 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1496 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1497 | } else if (FromType->isArrayType()) { |
| 1498 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1499 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1500 | |
| 1501 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1502 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1503 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1504 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1505 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1506 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1507 | // This conversion is deprecated in C++03 (D.4) |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1508 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1509 | |
| 1510 | // For the purpose of ranking in overload resolution |
| 1511 | // (13.3.3.1.1), this conversion is considered an |
| 1512 | // array-to-pointer conversion followed by a qualification |
| 1513 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1514 | SCS.Second = ICK_Identity; |
| 1515 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1516 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1517 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1518 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1519 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1520 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1521 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1522 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1523 | |
| 1524 | // An lvalue of function type T can be converted to an rvalue of |
| 1525 | // type "pointer to T." The result is a pointer to the |
| 1526 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1527 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1528 | } else { |
| 1529 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1530 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1531 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1532 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1533 | |
| 1534 | // The second conversion can be an integral promotion, floating |
| 1535 | // point promotion, integral conversion, floating point conversion, |
| 1536 | // floating-integral conversion, pointer conversion, |
| 1537 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1538 | // For overloading in C, this can also be a "compatible-type" |
| 1539 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1540 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1541 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1542 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1543 | // The unqualified versions of the types are the same: there's no |
| 1544 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1545 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1546 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1548 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1549 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1550 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1551 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1552 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1553 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1554 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1555 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1556 | SCS.Second = ICK_Complex_Promotion; |
| 1557 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1558 | } else if (ToType->isBooleanType() && |
| 1559 | (FromType->isArithmeticType() || |
| 1560 | FromType->isAnyPointerType() || |
| 1561 | FromType->isBlockPointerType() || |
| 1562 | FromType->isMemberPointerType() || |
| 1563 | FromType->isNullPtrType())) { |
| 1564 | // Boolean conversions (C++ 4.12). |
| 1565 | SCS.Second = ICK_Boolean_Conversion; |
| 1566 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1567 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1568 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1569 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1570 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1571 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1572 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1573 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1574 | SCS.Second = ICK_Complex_Conversion; |
| 1575 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1576 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1577 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1578 | // Complex-real conversions (C99 6.3.1.7) |
| 1579 | SCS.Second = ICK_Complex_Real; |
| 1580 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1581 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1582 | // Floating point conversions (C++ 4.8). |
| 1583 | SCS.Second = ICK_Floating_Conversion; |
| 1584 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1585 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1586 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1587 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1588 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1589 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1590 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1591 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1592 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1593 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1594 | } else if (AllowObjCWritebackConversion && |
| 1595 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1596 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1597 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1598 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1599 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1600 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1601 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1602 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1603 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1604 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1605 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1606 | SCS.Second = ICK_Pointer_Member; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1607 | } else if (IsVectorConversion(S, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1608 | SCS.Second = SecondICK; |
| 1609 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1610 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1611 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1612 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1613 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1614 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1615 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1616 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1617 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1618 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1619 | InOverloadResolution, |
| 1620 | SCS, CStyle)) { |
| 1621 | SCS.Second = ICK_TransparentUnionConversion; |
| 1622 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1623 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1624 | CStyle)) { |
| 1625 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1626 | // appropriately. |
| 1627 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1628 | } else if (ToType->isEventT() && |
| 1629 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1630 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1631 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1632 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1633 | } else { |
| 1634 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1635 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1636 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1637 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1638 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1639 | QualType CanonFrom; |
| 1640 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1641 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1642 | bool ObjCLifetimeConversion; |
| 1643 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1644 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1645 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1646 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1647 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1648 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1649 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1650 | } else { |
| 1651 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1652 | SCS.Third = ICK_Identity; |
| 1653 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1655 | // [...] Any difference in top-level cv-qualification is |
| 1656 | // subsumed by the initialization itself and does not constitute |
| 1657 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1658 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1659 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1660 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1661 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1662 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1663 | FromType = ToType; |
| 1664 | CanonFrom = CanonTo; |
| 1665 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1666 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1667 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1668 | |
| 1669 | // If we have not converted the argument type to the parameter type, |
| 1670 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1671 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1672 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1673 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1674 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1675 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1676 | |
| 1677 | static bool |
| 1678 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1679 | QualType &ToType, |
| 1680 | bool InOverloadResolution, |
| 1681 | StandardConversionSequence &SCS, |
| 1682 | bool CStyle) { |
| 1683 | |
| 1684 | const RecordType *UT = ToType->getAsUnionType(); |
| 1685 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1686 | return false; |
| 1687 | // The field to initialize within the transparent union. |
| 1688 | RecordDecl *UD = UT->getDecl(); |
| 1689 | // It's compatible if the expression matches any of the fields. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1690 | for (const auto *it : UD->fields()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1691 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1692 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1693 | ToType = it->getType(); |
| 1694 | return true; |
| 1695 | } |
| 1696 | } |
| 1697 | return false; |
| 1698 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1699 | |
| 1700 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1701 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1702 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1703 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1705 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1706 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1707 | if (!To) { |
| 1708 | return false; |
| 1709 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1710 | |
| 1711 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1712 | // unsigned short int can be converted to an rvalue of type int if |
| 1713 | // int can represent all the values of the source type; otherwise, |
| 1714 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1715 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1716 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1717 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1718 | if (// We can promote any signed, promotable integer type to an int |
| 1719 | (FromType->isSignedIntegerType() || |
| 1720 | // We can promote any unsigned integer type whose size is |
| 1721 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1723 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1724 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1727 | return To->getKind() == BuiltinType::UInt; |
| 1728 | } |
| 1729 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1730 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1731 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1732 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1733 | // following types that can represent all the values of the enumeration |
| 1734 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1735 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1736 | // 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] | 1737 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1738 | // 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] | 1739 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1740 | // 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] | 1741 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1742 | // C++11 [conv.prom]p4: |
| 1743 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1744 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1745 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1746 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1747 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1748 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1749 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1750 | // provided for a scoped enumeration. |
| 1751 | if (FromEnumType->getDecl()->isScoped()) |
| 1752 | return false; |
| 1753 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1754 | // We can perform an integral promotion to the underlying type of the enum, |
| 1755 | // even if that's not the promoted type. |
| 1756 | if (FromEnumType->getDecl()->isFixed()) { |
| 1757 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1758 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1759 | IsIntegralPromotion(From, Underlying, ToType); |
| 1760 | } |
| 1761 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1762 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1763 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1764 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1765 | return Context.hasSameUnqualifiedType( |
| 1766 | ToType, FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1767 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1768 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1769 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1770 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1771 | // to an rvalue a prvalue of the first of the following types that can |
| 1772 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1773 | // 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] | 1774 | // 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] | 1775 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1776 | // 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] | 1777 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1778 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1779 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1780 | // Determine whether the type we're converting from is signed or |
| 1781 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1782 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1783 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1785 | // The types we'll try to promote to, in the appropriate |
| 1786 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | QualType PromoteTypes[6] = { |
| 1788 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1789 | Context.LongTy, Context.UnsignedLongTy , |
| 1790 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1791 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1792 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1793 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1794 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1796 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1797 | // We found the type that we can promote to. If this is the |
| 1798 | // type we wanted, we have a promotion. Otherwise, no |
| 1799 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1800 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1806 | // rvalue of type int if int can represent all the values of the |
| 1807 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1808 | // unsigned int can represent all the values of the bit-field. If |
| 1809 | // the bit-field is larger yet, no integral promotion applies to |
| 1810 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1811 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1812 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1813 | // conversion. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1814 | if (From) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1815 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1816 | llvm::APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1817 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1818 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1819 | llvm::APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1820 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1822 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1823 | if (BitWidth < ToSize || |
| 1824 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1825 | return To->getKind() == BuiltinType::Int; |
| 1826 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1828 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1829 | // that fits into an unsigned int? |
| 1830 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1831 | return To->getKind() == BuiltinType::UInt; |
| 1832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1834 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1835 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1836 | } |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1839 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1840 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1841 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1842 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1843 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1844 | |
| 1845 | return false; |
| 1846 | } |
| 1847 | |
| 1848 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1849 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1850 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1852 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1853 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1854 | /// An rvalue of type float can be converted to an rvalue of type |
| 1855 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1856 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1857 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1858 | return true; |
| 1859 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1860 | // C99 6.3.1.5p1: |
| 1861 | // When a float is promoted to double or long double, or a |
| 1862 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1863 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1864 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1865 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1866 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1867 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1868 | |
| 1869 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1870 | if (!getLangOpts().NativeHalfType && |
| 1871 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1872 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1873 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1876 | return false; |
| 1877 | } |
| 1878 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1879 | /// \brief Determine if a conversion is a complex promotion. |
| 1880 | /// |
| 1881 | /// A complex promotion is defined as a complex -> complex conversion |
| 1882 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1883 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1884 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1885 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1886 | if (!FromComplex) |
| 1887 | return false; |
| 1888 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1889 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1890 | if (!ToComplex) |
| 1891 | return false; |
| 1892 | |
| 1893 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1894 | ToComplex->getElementType()) || |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1895 | IsIntegralPromotion(nullptr, FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1896 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1899 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1900 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1901 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1902 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1903 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1904 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1906 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1907 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1908 | ASTContext &Context, |
| 1909 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1910 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1911 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1912 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1913 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1914 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1915 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1916 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1917 | |
| 1918 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1919 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1920 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1921 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1922 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1923 | if (StripObjCLifetime) |
| 1924 | Quals.removeObjCLifetime(); |
| 1925 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1927 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1928 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1929 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1930 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1931 | |
| 1932 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1933 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1934 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1935 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1936 | return Context.getPointerType(ToPointee); |
| 1937 | } |
| 1938 | |
| 1939 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1940 | QualType QualifiedCanonToPointee |
| 1941 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1943 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1944 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1945 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1946 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1947 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1949 | bool InOverloadResolution, |
| 1950 | ASTContext &Context) { |
| 1951 | // Handle value-dependent integral null pointer constants correctly. |
| 1952 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1953 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1954 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1955 | return !InOverloadResolution; |
| 1956 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1957 | return Expr->isNullPointerConstant(Context, |
| 1958 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1959 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1962 | /// IsPointerConversion - Determines whether the conversion of the |
| 1963 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1964 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1965 | /// 4.10). If so, returns true and places the converted type (that |
| 1966 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1967 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1968 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1969 | /// This routine also supports conversions to and from block pointers |
| 1970 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1971 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1972 | /// appropriate overloading rules for Objective-C, we may want to |
| 1973 | /// split the Objective-C checks into a different routine; however, |
| 1974 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1975 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1976 | /// set if the conversion is an allowed Objective-C conversion that |
| 1977 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1978 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1979 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1980 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1982 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1983 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1984 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1985 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1986 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1988 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1989 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1990 | ConvertedType = ToType; |
| 1991 | return true; |
| 1992 | } |
| 1993 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1994 | // Blocks: Block pointers can be converted to void*. |
| 1995 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1996 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1997 | ConvertedType = ToType; |
| 1998 | return true; |
| 1999 | } |
| 2000 | // Blocks: A null pointer constant can be converted to a block |
| 2001 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2003 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2004 | ConvertedType = ToType; |
| 2005 | return true; |
| 2006 | } |
| 2007 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2008 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2009 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2011 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2012 | ConvertedType = ToType; |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2016 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2017 | if (!ToTypePtr) |
| 2018 | return false; |
| 2019 | |
| 2020 | // 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] | 2021 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2022 | ConvertedType = ToType; |
| 2023 | return true; |
| 2024 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2025 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2026 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2027 | // , including objective-c pointers. |
| 2028 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2029 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2030 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2031 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2032 | FromType->getAs<ObjCObjectPointerType>(), |
| 2033 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2034 | ToType, Context); |
| 2035 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2036 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2037 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2038 | if (!FromTypePtr) |
| 2039 | return false; |
| 2040 | |
| 2041 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2042 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2043 | // 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] | 2044 | // pointer conversion, so don't do all of the work below. |
| 2045 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2046 | return false; |
| 2047 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2048 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2049 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2050 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2051 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2052 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2054 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2055 | ToType, Context, |
| 2056 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2057 | return true; |
| 2058 | } |
| 2059 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2060 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2061 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2062 | ToPointeeType->isVoidType()) { |
| 2063 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2064 | ToPointeeType, |
| 2065 | ToType, Context); |
| 2066 | return true; |
| 2067 | } |
| 2068 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2069 | // When we're overloading in C, we allow a special kind of pointer |
| 2070 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2071 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2072 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2074 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2076 | return true; |
| 2077 | } |
| 2078 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2079 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2081 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2082 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2083 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2084 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2085 | // necessitates this conversion is ill-formed. The result of the |
| 2086 | // conversion is a pointer to the base class sub-object of the |
| 2087 | // derived class object. The null pointer value is converted to |
| 2088 | // the null pointer value of the destination type. |
| 2089 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2090 | // Note that we do not check for ambiguity or inaccessibility |
| 2091 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2092 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2093 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2094 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2095 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2096 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2098 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2099 | ToType, Context); |
| 2100 | return true; |
| 2101 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2102 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2103 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2104 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2105 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2106 | ToPointeeType, |
| 2107 | ToType, Context); |
| 2108 | return true; |
| 2109 | } |
| 2110 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2111 | return false; |
| 2112 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2113 | |
| 2114 | /// \brief Adopt the given qualifiers for the given type. |
| 2115 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2116 | Qualifiers TQs = T.getQualifiers(); |
| 2117 | |
| 2118 | // Check whether qualifiers already match. |
| 2119 | if (TQs == Qs) |
| 2120 | return T; |
| 2121 | |
| 2122 | if (Qs.compatiblyIncludes(TQs)) |
| 2123 | return Context.getQualifiedType(T, Qs); |
| 2124 | |
| 2125 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2126 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2127 | |
| 2128 | /// isObjCPointerConversion - Determines whether this is an |
| 2129 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2130 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2132 | QualType& ConvertedType, |
| 2133 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2134 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2135 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2137 | // The set of qualifiers on the type we're converting from. |
| 2138 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2139 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2140 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2141 | const ObjCObjectPointerType* ToObjCPtr = |
| 2142 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2144 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2145 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2146 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2147 | // If the pointee types are the same (ignoring qualifications), |
| 2148 | // then this is not a pointer conversion. |
| 2149 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2150 | FromObjCPtr->getPointeeType())) |
| 2151 | return false; |
| 2152 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2153 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2154 | // 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] | 2155 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2156 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2157 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2158 | return true; |
| 2159 | } |
| 2160 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2162 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2164 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2165 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2166 | return true; |
| 2167 | } |
| 2168 | // Objective C++: We're able to convert from a pointer to an |
| 2169 | // interface to a pointer to a different interface. |
| 2170 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2171 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2172 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2173 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2174 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2175 | FromObjCPtr->getPointeeType())) |
| 2176 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2177 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2178 | ToObjCPtr->getPointeeType(), |
| 2179 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2180 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2181 | return true; |
| 2182 | } |
| 2183 | |
| 2184 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2185 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2186 | // interfaces, which is permitted. However, we're going to |
| 2187 | // complain about it. |
| 2188 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2189 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2190 | ToObjCPtr->getPointeeType(), |
| 2191 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2192 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2193 | return true; |
| 2194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2196 | // 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] | 2197 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2198 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2199 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2200 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2201 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2202 | // 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] | 2203 | // to a block pointer type. |
| 2204 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2205 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2206 | return true; |
| 2207 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2208 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2209 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2210 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2211 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2212 | // 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] | 2213 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2214 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2215 | return true; |
| 2216 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2217 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2218 | return false; |
| 2219 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2220 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2221 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2222 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2223 | else if (const BlockPointerType *FromBlockPtr = |
| 2224 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2225 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2226 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2227 | return false; |
| 2228 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2229 | // If we have pointers to pointers, recursively check whether this |
| 2230 | // is an Objective-C conversion. |
| 2231 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2232 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2233 | IncompatibleObjC)) { |
| 2234 | // We always complain about this conversion. |
| 2235 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2236 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2237 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2238 | return true; |
| 2239 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2240 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2241 | // as in I* to id. |
| 2242 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2243 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2244 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2245 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2246 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2247 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2248 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2249 | return true; |
| 2250 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2251 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2252 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2253 | // differences in the argument and result types are in Objective-C |
| 2254 | // pointer conversions. If so, we permit the conversion (but |
| 2255 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2257 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2258 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2259 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2260 | if (FromFunctionType && ToFunctionType) { |
| 2261 | // If the function types are exactly the same, this isn't an |
| 2262 | // Objective-C pointer conversion. |
| 2263 | if (Context.getCanonicalType(FromPointeeType) |
| 2264 | == Context.getCanonicalType(ToPointeeType)) |
| 2265 | return false; |
| 2266 | |
| 2267 | // Perform the quick checks that will tell us whether these |
| 2268 | // function types are obviously different. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2269 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2270 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2271 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2272 | return false; |
| 2273 | |
| 2274 | bool HasObjCConversion = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2275 | if (Context.getCanonicalType(FromFunctionType->getReturnType()) == |
| 2276 | Context.getCanonicalType(ToFunctionType->getReturnType())) { |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2277 | // Okay, the types match exactly. Nothing to do. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2278 | } else if (isObjCPointerConversion(FromFunctionType->getReturnType(), |
| 2279 | ToFunctionType->getReturnType(), |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2280 | ConvertedType, IncompatibleObjC)) { |
| 2281 | // Okay, we have an Objective-C pointer conversion. |
| 2282 | HasObjCConversion = true; |
| 2283 | } else { |
| 2284 | // Function types are too different. Abort. |
| 2285 | return false; |
| 2286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2288 | // Check argument types. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2289 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2290 | ArgIdx != NumArgs; ++ArgIdx) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2291 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
| 2292 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2293 | if (Context.getCanonicalType(FromArgType) |
| 2294 | == Context.getCanonicalType(ToArgType)) { |
| 2295 | // Okay, the types match exactly. Nothing to do. |
| 2296 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2297 | ConvertedType, IncompatibleObjC)) { |
| 2298 | // Okay, we have an Objective-C pointer conversion. |
| 2299 | HasObjCConversion = true; |
| 2300 | } else { |
| 2301 | // Argument types are too different. Abort. |
| 2302 | return false; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | if (HasObjCConversion) { |
| 2307 | // We had an Objective-C conversion. Allow this pointer |
| 2308 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2309 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2310 | IncompatibleObjC = true; |
| 2311 | return true; |
| 2312 | } |
| 2313 | } |
| 2314 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2315 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2316 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2317 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2318 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2319 | /// used for parameter passing when performing automatic reference counting. |
| 2320 | /// |
| 2321 | /// \param FromType The type we're converting form. |
| 2322 | /// |
| 2323 | /// \param ToType The type we're converting to. |
| 2324 | /// |
| 2325 | /// \param ConvertedType The type that will be produced after applying |
| 2326 | /// this conversion. |
| 2327 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2328 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2329 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2330 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2331 | return false; |
| 2332 | |
| 2333 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2334 | QualType ToPointee; |
| 2335 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2336 | ToPointee = ToPointer->getPointeeType(); |
| 2337 | else |
| 2338 | return false; |
| 2339 | |
| 2340 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2341 | if (!ToPointee->isObjCLifetimeType() || |
| 2342 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2343 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2344 | return false; |
| 2345 | |
| 2346 | // Argument must be a pointer to __strong to __weak. |
| 2347 | QualType FromPointee; |
| 2348 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2349 | FromPointee = FromPointer->getPointeeType(); |
| 2350 | else |
| 2351 | return false; |
| 2352 | |
| 2353 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2354 | if (!FromPointee->isObjCLifetimeType() || |
| 2355 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2356 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2357 | return false; |
| 2358 | |
| 2359 | // Make sure that we have compatible qualifiers. |
| 2360 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2361 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2362 | return false; |
| 2363 | |
| 2364 | // Remove qualifiers from the pointee type we're converting from; they |
| 2365 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2366 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2367 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2368 | |
| 2369 | // The unqualified form of the pointee types must be compatible. |
| 2370 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2371 | bool IncompatibleObjC; |
| 2372 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2373 | FromPointee = ToPointee; |
| 2374 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2375 | IncompatibleObjC)) |
| 2376 | return false; |
| 2377 | |
| 2378 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2379 | /// __autoreleasing pointee. |
| 2380 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2381 | ConvertedType = Context.getPointerType(FromPointee); |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2385 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2386 | QualType& ConvertedType) { |
| 2387 | QualType ToPointeeType; |
| 2388 | if (const BlockPointerType *ToBlockPtr = |
| 2389 | ToType->getAs<BlockPointerType>()) |
| 2390 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2391 | else |
| 2392 | return false; |
| 2393 | |
| 2394 | QualType FromPointeeType; |
| 2395 | if (const BlockPointerType *FromBlockPtr = |
| 2396 | FromType->getAs<BlockPointerType>()) |
| 2397 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2398 | else |
| 2399 | return false; |
| 2400 | // We have pointer to blocks, check whether the only |
| 2401 | // differences in the argument and result types are in Objective-C |
| 2402 | // pointer conversions. If so, we permit the conversion. |
| 2403 | |
| 2404 | const FunctionProtoType *FromFunctionType |
| 2405 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2406 | const FunctionProtoType *ToFunctionType |
| 2407 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2408 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2409 | if (!FromFunctionType || !ToFunctionType) |
| 2410 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2411 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2412 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2413 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2414 | |
| 2415 | // Perform the quick checks that will tell us whether these |
| 2416 | // function types are obviously different. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2417 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2418 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2419 | return false; |
| 2420 | |
| 2421 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2422 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2423 | if (FromEInfo != ToEInfo) |
| 2424 | return false; |
| 2425 | |
| 2426 | bool IncompatibleObjC = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2427 | if (Context.hasSameType(FromFunctionType->getReturnType(), |
| 2428 | ToFunctionType->getReturnType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2429 | // Okay, the types match exactly. Nothing to do. |
| 2430 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2431 | QualType RHS = FromFunctionType->getReturnType(); |
| 2432 | QualType LHS = ToFunctionType->getReturnType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2433 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2434 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2435 | LHS = LHS.getUnqualifiedType(); |
| 2436 | |
| 2437 | if (Context.hasSameType(RHS,LHS)) { |
| 2438 | // OK exact match. |
| 2439 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2440 | ConvertedType, IncompatibleObjC)) { |
| 2441 | if (IncompatibleObjC) |
| 2442 | return false; |
| 2443 | // Okay, we have an Objective-C pointer conversion. |
| 2444 | } |
| 2445 | else |
| 2446 | return false; |
| 2447 | } |
| 2448 | |
| 2449 | // Check argument types. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2450 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2451 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2452 | IncompatibleObjC = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2453 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
| 2454 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2455 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2456 | // Okay, the types match exactly. Nothing to do. |
| 2457 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2458 | ConvertedType, IncompatibleObjC)) { |
| 2459 | if (IncompatibleObjC) |
| 2460 | return false; |
| 2461 | // Okay, we have an Objective-C pointer conversion. |
| 2462 | } else |
| 2463 | // Argument types are too different. Abort. |
| 2464 | return false; |
| 2465 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2466 | if (LangOpts.ObjCAutoRefCount && |
| 2467 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2468 | ToFunctionType)) |
| 2469 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2470 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2471 | ConvertedType = ToType; |
| 2472 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2475 | enum { |
| 2476 | ft_default, |
| 2477 | ft_different_class, |
| 2478 | ft_parameter_arity, |
| 2479 | ft_parameter_mismatch, |
| 2480 | ft_return_type, |
| 2481 | ft_qualifer_mismatch |
| 2482 | }; |
| 2483 | |
| 2484 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2485 | /// function types. Catches different number of parameter, mismatch in |
| 2486 | /// parameter types, and different return types. |
| 2487 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2488 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2489 | // If either type is not valid, include no extra info. |
| 2490 | if (FromType.isNull() || ToType.isNull()) { |
| 2491 | PDiag << ft_default; |
| 2492 | return; |
| 2493 | } |
| 2494 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2495 | // Get the function type from the pointers. |
| 2496 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2497 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2498 | *ToMember = ToType->getAs<MemberPointerType>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2499 | if (!Context.hasSameType(FromMember->getClass(), ToMember->getClass())) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2500 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2501 | << QualType(FromMember->getClass(), 0); |
| 2502 | return; |
| 2503 | } |
| 2504 | FromType = FromMember->getPointeeType(); |
| 2505 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2508 | if (FromType->isPointerType()) |
| 2509 | FromType = FromType->getPointeeType(); |
| 2510 | if (ToType->isPointerType()) |
| 2511 | ToType = ToType->getPointeeType(); |
| 2512 | |
| 2513 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2514 | FromType = FromType.getNonReferenceType(); |
| 2515 | ToType = ToType.getNonReferenceType(); |
| 2516 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2517 | // Don't print extra info for non-specialized template functions. |
| 2518 | if (FromType->isInstantiationDependentType() && |
| 2519 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2520 | PDiag << ft_default; |
| 2521 | return; |
| 2522 | } |
| 2523 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2524 | // No extra info for same types. |
| 2525 | if (Context.hasSameType(FromType, ToType)) { |
| 2526 | PDiag << ft_default; |
| 2527 | return; |
| 2528 | } |
| 2529 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2530 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2531 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2532 | |
| 2533 | // Both types need to be function types. |
| 2534 | if (!FromFunction || !ToFunction) { |
| 2535 | PDiag << ft_default; |
| 2536 | return; |
| 2537 | } |
| 2538 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2539 | if (FromFunction->getNumParams() != ToFunction->getNumParams()) { |
| 2540 | PDiag << ft_parameter_arity << ToFunction->getNumParams() |
| 2541 | << FromFunction->getNumParams(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
| 2544 | |
| 2545 | // Handle different parameter types. |
| 2546 | unsigned ArgPos; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2547 | if (!FunctionParamTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2548 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2549 | << ToFunction->getParamType(ArgPos) |
| 2550 | << FromFunction->getParamType(ArgPos); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2551 | return; |
| 2552 | } |
| 2553 | |
| 2554 | // Handle different return type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2555 | if (!Context.hasSameType(FromFunction->getReturnType(), |
| 2556 | ToFunction->getReturnType())) { |
| 2557 | PDiag << ft_return_type << ToFunction->getReturnType() |
| 2558 | << FromFunction->getReturnType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2559 | return; |
| 2560 | } |
| 2561 | |
| 2562 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2563 | ToQuals = ToFunction->getTypeQuals(); |
| 2564 | if (FromQuals != ToQuals) { |
| 2565 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2566 | return; |
| 2567 | } |
| 2568 | |
| 2569 | // Unable to find a difference, so add no extra info. |
| 2570 | PDiag << ft_default; |
| 2571 | } |
| 2572 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2573 | /// FunctionParamTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2574 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2575 | /// they have same number of arguments. If the parameters are different, |
| 2576 | /// ArgPos will have the parameter index of the first different parameter. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2577 | bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, |
| 2578 | const FunctionProtoType *NewType, |
| 2579 | unsigned *ArgPos) { |
| 2580 | for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), |
| 2581 | N = NewType->param_type_begin(), |
| 2582 | E = OldType->param_type_end(); |
| 2583 | O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2584 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2585 | N->getUnqualifiedType())) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2586 | if (ArgPos) |
| 2587 | *ArgPos = O - OldType->param_type_begin(); |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2588 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | return true; |
| 2592 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2594 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2595 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2596 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2597 | /// conversions for which IsPointerConversion has already returned |
| 2598 | /// true. It returns true and produces a diagnostic if there was an |
| 2599 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2600 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2601 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2602 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2603 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2604 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2605 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2606 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2607 | Kind = CK_BitCast; |
| 2608 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2609 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2610 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2611 | Expr::NPCK_ZeroExpression) { |
| 2612 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2613 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2614 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2615 | << ToType << From->getSourceRange()); |
| 2616 | else if (!isUnevaluatedContext()) |
| 2617 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2618 | << ToType << From->getSourceRange(); |
| 2619 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2620 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2621 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2622 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2623 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2625 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2626 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2627 | // We must have a derived-to-base conversion. Check an |
| 2628 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2629 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2630 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2631 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2632 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2633 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2634 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2635 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2636 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2637 | } |
| 2638 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2639 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2640 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2641 | if (const ObjCObjectPointerType *FromPtrType = |
| 2642 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2643 | // Objective-C++ conversions are always okay. |
| 2644 | // FIXME: We should have a different class of conversions for the |
| 2645 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2646 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2647 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2648 | } else if (FromType->isBlockPointerType()) { |
| 2649 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2650 | } else { |
| 2651 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2652 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2653 | } else if (ToType->isBlockPointerType()) { |
| 2654 | if (!FromType->isBlockPointerType()) |
| 2655 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2656 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2657 | |
| 2658 | // We shouldn't fall into this case unless it's valid for other |
| 2659 | // reasons. |
| 2660 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2661 | Kind = CK_NullToPointer; |
| 2662 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2663 | return false; |
| 2664 | } |
| 2665 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2666 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2667 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2668 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2669 | /// If so, returns true and places the converted type (that might differ from |
| 2670 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2671 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2672 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2673 | bool InOverloadResolution, |
| 2674 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2675 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2676 | if (!ToTypePtr) |
| 2677 | return false; |
| 2678 | |
| 2679 | // 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] | 2680 | if (From->isNullPointerConstant(Context, |
| 2681 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2682 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2683 | ConvertedType = ToType; |
| 2684 | return true; |
| 2685 | } |
| 2686 | |
| 2687 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2688 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2689 | if (!FromTypePtr) |
| 2690 | return false; |
| 2691 | |
| 2692 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2693 | // where D is derived from B (C++ 4.11p2). |
| 2694 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2695 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2697 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2698 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2699 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2700 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2701 | ToClass.getTypePtr()); |
| 2702 | return true; |
| 2703 | } |
| 2704 | |
| 2705 | return false; |
| 2706 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2707 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2708 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2709 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2710 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2711 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2712 | /// true and produces a diagnostic if there was an error, or returns false |
| 2713 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2715 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2716 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2717 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2718 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2719 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2720 | if (!FromPtrType) { |
| 2721 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2723 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2724 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2725 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2726 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2727 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2728 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2729 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2730 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2731 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2732 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2733 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2734 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2735 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2736 | // FIXME: What about dependent types? |
| 2737 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2738 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2739 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2740 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2741 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2742 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2743 | assert(DerivationOkay && |
| 2744 | "Should not have been called if derivation isn't OK."); |
| 2745 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2746 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2747 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2748 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2749 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2750 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2751 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2752 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2753 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2754 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2755 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2756 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2757 | << FromClass << ToClass << QualType(VBase, 0) |
| 2758 | << From->getSourceRange(); |
| 2759 | return true; |
| 2760 | } |
| 2761 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2762 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2763 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2764 | Paths.front(), |
| 2765 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2766 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2767 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2768 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2769 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2770 | return false; |
| 2771 | } |
| 2772 | |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2773 | /// Determine whether the lifetime conversion between the two given |
| 2774 | /// qualifiers sets is nontrivial. |
| 2775 | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
| 2776 | Qualifiers ToQuals) { |
| 2777 | // Converting anything to const __unsafe_unretained is trivial. |
| 2778 | if (ToQuals.hasConst() && |
| 2779 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 2780 | return false; |
| 2781 | |
| 2782 | return true; |
| 2783 | } |
| 2784 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2785 | /// IsQualificationConversion - Determines whether the conversion from |
| 2786 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2787 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2788 | /// |
| 2789 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2790 | /// when the qualification conversion involves a change in the Objective-C |
| 2791 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2793 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2794 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2795 | FromType = Context.getCanonicalType(FromType); |
| 2796 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2797 | ObjCLifetimeConversion = false; |
| 2798 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2799 | // If FromType and ToType are the same type, this is not a |
| 2800 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2801 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2802 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2804 | // (C++ 4.4p4): |
| 2805 | // A conversion can add cv-qualifiers at levels other than the first |
| 2806 | // in multi-level pointers, subject to the following rules: [...] |
| 2807 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2808 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2809 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2810 | // Within each iteration of the loop, we check the qualifiers to |
| 2811 | // determine if this still looks like a qualification |
| 2812 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2813 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2814 | // until there are no more pointers or pointers-to-members left to |
| 2815 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2816 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2817 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2818 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2819 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2820 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2821 | // Objective-C ARC: |
| 2822 | // Check Objective-C lifetime conversions. |
| 2823 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2824 | UnwrappedAnyPointer) { |
| 2825 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2826 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
| 2827 | ObjCLifetimeConversion = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2828 | FromQuals.removeObjCLifetime(); |
| 2829 | ToQuals.removeObjCLifetime(); |
| 2830 | } else { |
| 2831 | // Qualification conversions cannot cast between different |
| 2832 | // Objective-C lifetime qualifiers. |
| 2833 | return false; |
| 2834 | } |
| 2835 | } |
| 2836 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2837 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2838 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2839 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2840 | FromQuals.removeObjCGCAttr(); |
| 2841 | ToQuals.removeObjCGCAttr(); |
| 2842 | } |
| 2843 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2844 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2845 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2846 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2847 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2849 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2850 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2851 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2852 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2853 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2855 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2856 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2857 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2858 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2859 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2860 | |
| 2861 | // We are left with FromType and ToType being the pointee types |
| 2862 | // after unwrapping the original FromType and ToType the same number |
| 2863 | // of types. If we unwrapped any pointers, and if FromType and |
| 2864 | // ToType have the same unqualified type (since we checked |
| 2865 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2866 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2869 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2870 | /// atomic type. |
| 2871 | /// |
| 2872 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2873 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2874 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2875 | bool InOverloadResolution, |
| 2876 | StandardConversionSequence &SCS, |
| 2877 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2878 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2879 | if (!ToAtomic) |
| 2880 | return false; |
| 2881 | |
| 2882 | StandardConversionSequence InnerSCS; |
| 2883 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2884 | InOverloadResolution, InnerSCS, |
| 2885 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2886 | return false; |
| 2887 | |
| 2888 | SCS.Second = InnerSCS.Second; |
| 2889 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2890 | SCS.Third = InnerSCS.Third; |
| 2891 | SCS.QualificationIncludesObjCLifetime |
| 2892 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2893 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2894 | return true; |
| 2895 | } |
| 2896 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2897 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2898 | CXXConstructorDecl *Constructor, |
| 2899 | QualType Type) { |
| 2900 | const FunctionProtoType *CtorType = |
| 2901 | Constructor->getType()->getAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2902 | if (CtorType->getNumParams() > 0) { |
| 2903 | QualType FirstArg = CtorType->getParamType(0); |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2904 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2905 | return true; |
| 2906 | } |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2910 | static OverloadingResult |
| 2911 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2912 | CXXRecordDecl *To, |
| 2913 | UserDefinedConversionSequence &User, |
| 2914 | OverloadCandidateSet &CandidateSet, |
| 2915 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2916 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2917 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2918 | Con != ConEnd; ++Con) { |
| 2919 | NamedDecl *D = *Con; |
| 2920 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2921 | |
| 2922 | // Find the constructor (which may be a template). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2923 | CXXConstructorDecl *Constructor = nullptr; |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2924 | FunctionTemplateDecl *ConstructorTmpl |
| 2925 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2926 | if (ConstructorTmpl) |
| 2927 | Constructor |
| 2928 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2929 | else |
| 2930 | Constructor = cast<CXXConstructorDecl>(D); |
| 2931 | |
| 2932 | bool Usable = !Constructor->isInvalidDecl() && |
| 2933 | S.isInitListConstructor(Constructor) && |
| 2934 | (AllowExplicit || !Constructor->isExplicit()); |
| 2935 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2936 | // If the first argument is (a reference to) the target type, |
| 2937 | // suppress conversions. |
| 2938 | bool SuppressUserConversions = |
| 2939 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2940 | if (ConstructorTmpl) |
| 2941 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2942 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2943 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2944 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2945 | else |
| 2946 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2947 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2948 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2953 | |
| 2954 | OverloadCandidateSet::iterator Best; |
| 2955 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2956 | case OR_Success: { |
| 2957 | // Record the standard conversion we used and the conversion function. |
| 2958 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2959 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2960 | // Initializer lists don't have conversions as such. |
| 2961 | User.Before.setAsIdentityConversion(); |
| 2962 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2963 | User.ConversionFunction = Constructor; |
| 2964 | User.FoundConversionFunction = Best->FoundDecl; |
| 2965 | User.After.setAsIdentityConversion(); |
| 2966 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2967 | User.After.setAllToTypes(ToType); |
| 2968 | return OR_Success; |
| 2969 | } |
| 2970 | |
| 2971 | case OR_No_Viable_Function: |
| 2972 | return OR_No_Viable_Function; |
| 2973 | case OR_Deleted: |
| 2974 | return OR_Deleted; |
| 2975 | case OR_Ambiguous: |
| 2976 | return OR_Ambiguous; |
| 2977 | } |
| 2978 | |
| 2979 | llvm_unreachable("Invalid OverloadResult!"); |
| 2980 | } |
| 2981 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2982 | /// Determines whether there is a user-defined conversion sequence |
| 2983 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2984 | /// ToType. If such a conversion exists, User will contain the |
| 2985 | /// user-defined conversion sequence that performs such a conversion |
| 2986 | /// and this routine will return true. Otherwise, this routine returns |
| 2987 | /// false and User is unspecified. |
| 2988 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2989 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2990 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2991 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 2992 | /// |
| 2993 | /// \param AllowObjCConversionOnExplicit true if the conversion should |
| 2994 | /// allow an extra Objective-C pointer conversion on uses of explicit |
| 2995 | /// constructors. Requires \c AllowExplicit to also be set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2996 | static OverloadingResult |
| 2997 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2998 | UserDefinedConversionSequence &User, |
| 2999 | OverloadCandidateSet &CandidateSet, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3000 | bool AllowExplicit, |
| 3001 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | a1977bf | 2013-11-08 01:20:25 +0000 | [diff] [blame] | 3002 | assert(AllowExplicit || !AllowObjCConversionOnExplicit); |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3004 | // Whether we will only visit constructors. |
| 3005 | bool ConstructorsOnly = false; |
| 3006 | |
| 3007 | // If the type we are conversion to is a class type, enumerate its |
| 3008 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3009 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3010 | // C++ [over.match.ctor]p1: |
| 3011 | // When objects of class type are direct-initialized (8.5), or |
| 3012 | // copy-initialized from an expression of the same or a |
| 3013 | // derived class type (8.5), overload resolution selects the |
| 3014 | // constructor. [...] For copy-initialization, the candidate |
| 3015 | // functions are all the converting constructors (12.3.1) of |
| 3016 | // that class. The argument list is the expression-list within |
| 3017 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3018 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3019 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3020 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3021 | ConstructorsOnly = true; |
| 3022 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3023 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3024 | // RequireCompleteType may have returned true due to some invalid decl |
| 3025 | // during template instantiation, but ToType may be complete enough now |
| 3026 | // to try to recover. |
| 3027 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3028 | // We're not going to find any constructors. |
| 3029 | } else if (CXXRecordDecl *ToRecordDecl |
| 3030 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3031 | |
| 3032 | Expr **Args = &From; |
| 3033 | unsigned NumArgs = 1; |
| 3034 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3035 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3036 | // But first, see if there is an init-list-constructor that will work. |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3037 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3038 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3039 | if (Result != OR_No_Viable_Function) |
| 3040 | return Result; |
| 3041 | // Never mind. |
| 3042 | CandidateSet.clear(); |
| 3043 | |
| 3044 | // If we're list-initializing, we pass the individual elements as |
| 3045 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3046 | Args = InitList->getInits(); |
| 3047 | NumArgs = InitList->getNumInits(); |
| 3048 | ListInitializing = true; |
| 3049 | } |
| 3050 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3051 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3052 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3053 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3054 | NamedDecl *D = *Con; |
| 3055 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3056 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3057 | // Find the constructor (which may be a template). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3058 | CXXConstructorDecl *Constructor = nullptr; |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3059 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3060 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3061 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3063 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3064 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3065 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3066 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3067 | bool Usable = !Constructor->isInvalidDecl(); |
| 3068 | if (ListInitializing) |
| 3069 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3070 | else |
| 3071 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3072 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3073 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3074 | if (SuppressUserConversions && ListInitializing) { |
| 3075 | SuppressUserConversions = false; |
| 3076 | if (NumArgs == 1) { |
| 3077 | // If the first argument is (a reference to) the target type, |
| 3078 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3079 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3080 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3081 | } |
| 3082 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3083 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3084 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3085 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3086 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3087 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3088 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3089 | // Allow one user-defined conversion when user specifies a |
| 3090 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3091 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3092 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3093 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3094 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3095 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3099 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3100 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3101 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3102 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3104 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3106 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3107 | // Add all of the conversion functions as candidates. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3108 | const auto &Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3109 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3110 | DeclAccessPair FoundDecl = I.getPair(); |
| 3111 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3112 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3113 | if (isa<UsingShadowDecl>(D)) |
| 3114 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3115 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3116 | CXXConversionDecl *Conv; |
| 3117 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3118 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3119 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3120 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3121 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3122 | |
| 3123 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3124 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3125 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3126 | ActingContext, From, ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3127 | CandidateSet, |
| 3128 | AllowObjCConversionOnExplicit); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3129 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3130 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3131 | From, ToType, CandidateSet, |
| 3132 | AllowObjCConversionOnExplicit); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3136 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3137 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3138 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3139 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3140 | OverloadCandidateSet::iterator Best; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3141 | switch (auto Result = CandidateSet.BestViableFunction(S, From->getLocStart(), |
| 3142 | Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3143 | case OR_Success: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3144 | case OR_Deleted: |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3145 | // Record the standard conversion we used and the conversion function. |
| 3146 | if (CXXConstructorDecl *Constructor |
| 3147 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3148 | // C++ [over.ics.user]p1: |
| 3149 | // If the user-defined conversion is specified by a |
| 3150 | // constructor (12.3.1), the initial standard conversion |
| 3151 | // sequence converts the source type to the type required by |
| 3152 | // the argument of the constructor. |
| 3153 | // |
| 3154 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3155 | if (isa<InitListExpr>(From)) { |
| 3156 | // Initializer lists don't have conversions as such. |
| 3157 | User.Before.setAsIdentityConversion(); |
| 3158 | } else { |
| 3159 | if (Best->Conversions[0].isEllipsis()) |
| 3160 | User.EllipsisConversion = true; |
| 3161 | else { |
| 3162 | User.Before = Best->Conversions[0].Standard; |
| 3163 | User.EllipsisConversion = false; |
| 3164 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3165 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3166 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3167 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3168 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3169 | User.After.setAsIdentityConversion(); |
| 3170 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3171 | User.After.setAllToTypes(ToType); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3172 | return Result; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3173 | } |
| 3174 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3175 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3176 | // C++ [over.ics.user]p1: |
| 3177 | // |
| 3178 | // [...] If the user-defined conversion is specified by a |
| 3179 | // conversion function (12.3.2), the initial standard |
| 3180 | // conversion sequence converts the source type to the |
| 3181 | // implicit object parameter of the conversion function. |
| 3182 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3183 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3184 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3185 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3186 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3188 | // C++ [over.ics.user]p2: |
| 3189 | // The second standard conversion sequence converts the |
| 3190 | // result of the user-defined conversion to the target type |
| 3191 | // for the sequence. Since an implicit conversion sequence |
| 3192 | // is an initialization, the special rules for |
| 3193 | // initialization by user-defined conversion apply when |
| 3194 | // selecting the best user-defined conversion for a |
| 3195 | // user-defined conversion sequence (see 13.3.3 and |
| 3196 | // 13.3.3.1). |
| 3197 | User.After = Best->FinalConversion; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3198 | return Result; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3199 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3200 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3201 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3202 | case OR_No_Viable_Function: |
| 3203 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3204 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3205 | case OR_Ambiguous: |
| 3206 | return OR_Ambiguous; |
| 3207 | } |
| 3208 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3209 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3210 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3211 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3212 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3213 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3214 | ImplicitConversionSequence ICS; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3215 | OverloadCandidateSet CandidateSet(From->getExprLoc(), |
| 3216 | OverloadCandidateSet::CSK_Normal); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3217 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3218 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3219 | CandidateSet, false, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3220 | if (OvResult == OR_Ambiguous) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3221 | Diag(From->getLocStart(), diag::err_typecheck_ambiguous_condition) |
| 3222 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3223 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3224 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3225 | diag::err_typecheck_nonviable_condition_incomplete, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3226 | From->getType(), From->getSourceRange())) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3227 | Diag(From->getLocStart(), diag::err_typecheck_nonviable_condition) |
| 3228 | << From->getType() << From->getSourceRange() << ToType; |
| 3229 | } else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3230 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3231 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3232 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3233 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3234 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3235 | /// \brief Compare the user-defined conversion functions or constructors |
| 3236 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3237 | /// is possible. |
| 3238 | static ImplicitConversionSequence::CompareKind |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3239 | compareConversionFunctions(Sema &S, FunctionDecl *Function1, |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3240 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3241 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3242 | return ImplicitConversionSequence::Indistinguishable; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3243 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3244 | // Objective-C++: |
| 3245 | // If both conversion functions are implicitly-declared conversions from |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3246 | // a lambda closure type to a function pointer and a block pointer, |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3247 | // respectively, always prefer the conversion to a function pointer, |
| 3248 | // because the function pointer is more lightweight and is more likely |
| 3249 | // to keep code working. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3250 | CXXConversionDecl *Conv1 = dyn_cast_or_null<CXXConversionDecl>(Function1); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3251 | if (!Conv1) |
| 3252 | return ImplicitConversionSequence::Indistinguishable; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3253 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3254 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3255 | if (!Conv2) |
| 3256 | return ImplicitConversionSequence::Indistinguishable; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3257 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3258 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3259 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3260 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3261 | if (Block1 != Block2) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3262 | return Block1 ? ImplicitConversionSequence::Worse |
| 3263 | : ImplicitConversionSequence::Better; |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | return ImplicitConversionSequence::Indistinguishable; |
| 3267 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3268 | |
| 3269 | static bool hasDeprecatedStringLiteralToCharPtrConversion( |
| 3270 | const ImplicitConversionSequence &ICS) { |
| 3271 | return (ICS.isStandard() && ICS.Standard.DeprecatedStringLiteralToCharPtr) || |
| 3272 | (ICS.isUserDefined() && |
| 3273 | ICS.UserDefined.Before.DeprecatedStringLiteralToCharPtr); |
| 3274 | } |
| 3275 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3276 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3277 | /// conversion sequences to determine whether one is better than the |
| 3278 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3279 | static ImplicitConversionSequence::CompareKind |
| 3280 | CompareImplicitConversionSequences(Sema &S, |
| 3281 | const ImplicitConversionSequence& ICS1, |
| 3282 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3283 | { |
| 3284 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3285 | // conversion sequences (as defined in 13.3.3.1) |
| 3286 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3287 | // conversion sequence than a user-defined conversion sequence or |
| 3288 | // an ellipsis conversion sequence, and |
| 3289 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3290 | // conversion sequence than an ellipsis conversion sequence |
| 3291 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3293 | // C++0x [over.best.ics]p10: |
| 3294 | // For the purpose of ranking implicit conversion sequences as |
| 3295 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3296 | // treated as a user-defined sequence that is indistinguishable |
| 3297 | // from any other user-defined conversion sequence. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3298 | |
| 3299 | // String literal to 'char *' conversion has been deprecated in C++03. It has |
| 3300 | // been removed from C++11. We still accept this conversion, if it happens at |
| 3301 | // the best viable function. Otherwise, this conversion is considered worse |
| 3302 | // than ellipsis conversion. Consider this as an extension; this is not in the |
| 3303 | // standard. For example: |
| 3304 | // |
| 3305 | // int &f(...); // #1 |
| 3306 | // void f(char*); // #2 |
| 3307 | // void g() { int &r = f("foo"); } |
| 3308 | // |
| 3309 | // In C++03, we pick #2 as the best viable function. |
| 3310 | // In C++11, we pick #1 as the best viable function, because ellipsis |
| 3311 | // conversion is better than string-literal to char* conversion (since there |
| 3312 | // is no such conversion in C++11). If there was no #1 at all or #1 couldn't |
| 3313 | // convert arguments, #2 would be the best viable function in C++11. |
| 3314 | // If the best viable function has this conversion, a warning will be issued |
| 3315 | // in C++03, or an ExtWarn (+SFINAE failure) will be issued in C++11. |
| 3316 | |
| 3317 | if (S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
| 3318 | hasDeprecatedStringLiteralToCharPtrConversion(ICS1) != |
| 3319 | hasDeprecatedStringLiteralToCharPtrConversion(ICS2)) |
| 3320 | return hasDeprecatedStringLiteralToCharPtrConversion(ICS1) |
| 3321 | ? ImplicitConversionSequence::Worse |
| 3322 | : ImplicitConversionSequence::Better; |
| 3323 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3324 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3325 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3326 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3327 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3328 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3329 | // The following checks require both conversion sequences to be of |
| 3330 | // the same kind. |
| 3331 | if (ICS1.getKind() != ICS2.getKind()) |
| 3332 | return ImplicitConversionSequence::Indistinguishable; |
| 3333 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3334 | ImplicitConversionSequence::CompareKind Result = |
| 3335 | ImplicitConversionSequence::Indistinguishable; |
| 3336 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3337 | // Two implicit conversion sequences of the same form are |
| 3338 | // indistinguishable conversion sequences unless one of the |
| 3339 | // following rules apply: (C++ 13.3.3.2p3): |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3340 | |
| 3341 | // List-initialization sequence L1 is a better conversion sequence than |
| 3342 | // list-initialization sequence L2 if: |
| 3343 | // - L1 converts to std::initializer_list<X> for some X and L2 does not, or, |
| 3344 | // if not that, |
| 3345 | // - L1 converts to type "array of N1 T", L2 converts to type "array of N2 T", |
| 3346 | // and N1 is smaller than N2., |
| 3347 | // even if one of the other rules in this paragraph would otherwise apply. |
| 3348 | if (!ICS1.isBad()) { |
| 3349 | if (ICS1.isStdInitializerListElement() && |
| 3350 | !ICS2.isStdInitializerListElement()) |
| 3351 | return ImplicitConversionSequence::Better; |
| 3352 | if (!ICS1.isStdInitializerListElement() && |
| 3353 | ICS2.isStdInitializerListElement()) |
| 3354 | return ImplicitConversionSequence::Worse; |
| 3355 | } |
| 3356 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3357 | if (ICS1.isStandard()) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3358 | // Standard conversion sequence S1 is a better conversion sequence than |
| 3359 | // standard conversion sequence S2 if [...] |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3360 | Result = CompareStandardConversionSequences(S, |
| 3361 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3362 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3363 | // User-defined conversion sequence U1 is a better conversion |
| 3364 | // sequence than another user-defined conversion sequence U2 if |
| 3365 | // they contain the same user-defined conversion function or |
| 3366 | // constructor and if the second standard conversion sequence of |
| 3367 | // U1 is better than the second standard conversion sequence of |
| 3368 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3370 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3371 | Result = CompareStandardConversionSequences(S, |
| 3372 | ICS1.UserDefined.After, |
| 3373 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3374 | else |
| 3375 | Result = compareConversionFunctions(S, |
| 3376 | ICS1.UserDefined.ConversionFunction, |
| 3377 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3380 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3383 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3384 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3385 | Qualifiers Quals; |
| 3386 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3387 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3390 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3393 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3394 | // determine if one is a proper subset of the other. |
| 3395 | static ImplicitConversionSequence::CompareKind |
| 3396 | compareStandardConversionSubsets(ASTContext &Context, |
| 3397 | const StandardConversionSequence& SCS1, |
| 3398 | const StandardConversionSequence& SCS2) { |
| 3399 | ImplicitConversionSequence::CompareKind Result |
| 3400 | = ImplicitConversionSequence::Indistinguishable; |
| 3401 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3402 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3403 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3404 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3405 | return ImplicitConversionSequence::Better; |
| 3406 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3407 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3409 | if (SCS1.Second != SCS2.Second) { |
| 3410 | if (SCS1.Second == ICK_Identity) |
| 3411 | Result = ImplicitConversionSequence::Better; |
| 3412 | else if (SCS2.Second == ICK_Identity) |
| 3413 | Result = ImplicitConversionSequence::Worse; |
| 3414 | else |
| 3415 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3416 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3417 | return ImplicitConversionSequence::Indistinguishable; |
| 3418 | |
| 3419 | if (SCS1.Third == SCS2.Third) { |
| 3420 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3421 | : ImplicitConversionSequence::Indistinguishable; |
| 3422 | } |
| 3423 | |
| 3424 | if (SCS1.Third == ICK_Identity) |
| 3425 | return Result == ImplicitConversionSequence::Worse |
| 3426 | ? ImplicitConversionSequence::Indistinguishable |
| 3427 | : ImplicitConversionSequence::Better; |
| 3428 | |
| 3429 | if (SCS2.Third == ICK_Identity) |
| 3430 | return Result == ImplicitConversionSequence::Better |
| 3431 | ? ImplicitConversionSequence::Indistinguishable |
| 3432 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3434 | return ImplicitConversionSequence::Indistinguishable; |
| 3435 | } |
| 3436 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3437 | /// \brief Determine whether one of the given reference bindings is better |
| 3438 | /// than the other based on what kind of bindings they are. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3439 | static bool |
| 3440 | isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3441 | const StandardConversionSequence &SCS2) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3442 | // C++0x [over.ics.rank]p3b4: |
| 3443 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3444 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3445 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3446 | // 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] | 3447 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3448 | // reference*. |
| 3449 | // |
| 3450 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3451 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3452 | // time of this writing) break the standard definition of std::forward |
| 3453 | // and std::reference_wrapper when dealing with references to functions. |
| 3454 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3455 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3456 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3457 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3459 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3460 | SCS2.IsLvalueReference) || |
| 3461 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3462 | !SCS2.IsLvalueReference && SCS2.BindsToFunctionLvalue); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3463 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3465 | /// CompareStandardConversionSequences - Compare two standard |
| 3466 | /// conversion sequences to determine whether one is better than the |
| 3467 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3468 | static ImplicitConversionSequence::CompareKind |
| 3469 | CompareStandardConversionSequences(Sema &S, |
| 3470 | const StandardConversionSequence& SCS1, |
| 3471 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3472 | { |
| 3473 | // Standard conversion sequence S1 is a better conversion sequence |
| 3474 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3475 | |
| 3476 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3477 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3478 | // excluding any Lvalue Transformation; the identity conversion |
| 3479 | // sequence is considered to be a subsequence of any |
| 3480 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3481 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3482 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3483 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3484 | |
| 3485 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3486 | // defined below), or, if not that, |
| 3487 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3488 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3489 | if (Rank1 < Rank2) |
| 3490 | return ImplicitConversionSequence::Better; |
| 3491 | else if (Rank2 < Rank1) |
| 3492 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3494 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3495 | // are indistinguishable unless one of the following rules |
| 3496 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3498 | // A conversion that is not a conversion of a pointer, or |
| 3499 | // pointer to member, to bool is better than another conversion |
| 3500 | // that is such a conversion. |
| 3501 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3502 | return SCS2.isPointerConversionToBool() |
| 3503 | ? ImplicitConversionSequence::Better |
| 3504 | : ImplicitConversionSequence::Worse; |
| 3505 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3506 | // C++ [over.ics.rank]p4b2: |
| 3507 | // |
| 3508 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3509 | // conversion of B* to A* is better than conversion of B* to |
| 3510 | // void*, and conversion of A* to void* is better than conversion |
| 3511 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3513 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3515 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3516 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3517 | // Exactly one of the conversion sequences is a conversion to |
| 3518 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3519 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3520 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3521 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3522 | // Neither conversion sequence converts to a void pointer; compare |
| 3523 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3524 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3525 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3526 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3527 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3528 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3529 | // Both conversion sequences are conversions to void |
| 3530 | // pointers. Compare the source types to determine if there's an |
| 3531 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3532 | QualType FromType1 = SCS1.getFromType(); |
| 3533 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Adjust the types we're converting from via the array-to-pointer |
| 3536 | // conversion, if we need to. |
| 3537 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3538 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3539 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3540 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3541 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3542 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3543 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3544 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3545 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3546 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3547 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3548 | return ImplicitConversionSequence::Worse; |
| 3549 | |
| 3550 | // Objective-C++: If one interface is more specific than the |
| 3551 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3552 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3553 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3554 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3555 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3556 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3557 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3558 | FromObjCPtr2); |
| 3559 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3560 | FromObjCPtr1); |
| 3561 | if (AssignLeft != AssignRight) { |
| 3562 | return AssignLeft? ImplicitConversionSequence::Better |
| 3563 | : ImplicitConversionSequence::Worse; |
| 3564 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3565 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3566 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3567 | |
| 3568 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3569 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3570 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3571 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3572 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3573 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3574 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3575 | // Check for a better reference binding based on the kind of bindings. |
| 3576 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3577 | return ImplicitConversionSequence::Better; |
| 3578 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3579 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3580 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3581 | // C++ [over.ics.rank]p3b4: |
| 3582 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3583 | // which the references refer are the same type except for |
| 3584 | // top-level cv-qualifiers, and the type to which the reference |
| 3585 | // initialized by S2 refers is more cv-qualified than the type |
| 3586 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3587 | QualType T1 = SCS1.getToType(2); |
| 3588 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3589 | T1 = S.Context.getCanonicalType(T1); |
| 3590 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3591 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3592 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3593 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3594 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3595 | // Objective-C++ ARC: If the references refer to objects with different |
| 3596 | // lifetimes, prefer bindings that don't change lifetime. |
| 3597 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3598 | SCS2.ObjCLifetimeConversionBinding) { |
| 3599 | return SCS1.ObjCLifetimeConversionBinding |
| 3600 | ? ImplicitConversionSequence::Worse |
| 3601 | : ImplicitConversionSequence::Better; |
| 3602 | } |
| 3603 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3604 | // If the type is an array type, promote the element qualifiers to the |
| 3605 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3606 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3607 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3608 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3609 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3610 | if (T2.isMoreQualifiedThan(T1)) |
| 3611 | return ImplicitConversionSequence::Better; |
| 3612 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3613 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3616 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3617 | // In Microsoft mode, prefer an integral conversion to a |
| 3618 | // floating-to-integral conversion if the integral conversion |
| 3619 | // is between types of the same size. |
| 3620 | // For example: |
| 3621 | // void f(float); |
| 3622 | // void f(int); |
| 3623 | // int main { |
| 3624 | // long a; |
| 3625 | // f(a); |
| 3626 | // } |
| 3627 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3628 | // as clang will do in standard mode. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3629 | if (S.getLangOpts().MSVCCompat && SCS1.Second == ICK_Integral_Conversion && |
| 3630 | SCS2.Second == ICK_Floating_Integral && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3631 | S.Context.getTypeSize(SCS1.getFromType()) == |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3632 | S.Context.getTypeSize(SCS1.getToType(2))) |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3633 | return ImplicitConversionSequence::Better; |
| 3634 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3635 | return ImplicitConversionSequence::Indistinguishable; |
| 3636 | } |
| 3637 | |
| 3638 | /// CompareQualificationConversions - Compares two standard conversion |
| 3639 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3640 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3641 | static ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3642 | CompareQualificationConversions(Sema &S, |
| 3643 | const StandardConversionSequence& SCS1, |
| 3644 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3645 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3646 | // -- S1 and S2 differ only in their qualification conversion and |
| 3647 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3648 | // cv-qualification signature of type T1 is a proper subset of |
| 3649 | // the cv-qualification signature of type T2, and S1 is not the |
| 3650 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3651 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3652 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3653 | return ImplicitConversionSequence::Indistinguishable; |
| 3654 | |
| 3655 | // FIXME: the example in the standard doesn't use a qualification |
| 3656 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3657 | QualType T1 = SCS1.getToType(2); |
| 3658 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3659 | T1 = S.Context.getCanonicalType(T1); |
| 3660 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3661 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3662 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3663 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3664 | |
| 3665 | // If the types are the same, we won't learn anything by unwrapped |
| 3666 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3667 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3668 | return ImplicitConversionSequence::Indistinguishable; |
| 3669 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3670 | // If the type is an array type, promote the element qualifiers to the type |
| 3671 | // for comparison. |
| 3672 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3673 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3674 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3675 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3676 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3678 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3679 | |
| 3680 | // Objective-C++ ARC: |
| 3681 | // Prefer qualification conversions not involving a change in lifetime |
| 3682 | // to qualification conversions that do not change lifetime. |
| 3683 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3684 | SCS2.QualificationIncludesObjCLifetime) { |
| 3685 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3686 | ? ImplicitConversionSequence::Worse |
| 3687 | : ImplicitConversionSequence::Better; |
| 3688 | } |
| 3689 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3690 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3691 | // Within each iteration of the loop, we check the qualifiers to |
| 3692 | // determine if this still looks like a qualification |
| 3693 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3694 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3695 | // until there are no more pointers or pointers-to-members left |
| 3696 | // to unwrap. This essentially mimics what |
| 3697 | // IsQualificationConversion does, but here we're checking for a |
| 3698 | // strict subset of qualifiers. |
| 3699 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3700 | // The qualifiers are the same, so this doesn't tell us anything |
| 3701 | // about how the sequences rank. |
| 3702 | ; |
| 3703 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3704 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3705 | if (Result == ImplicitConversionSequence::Worse) |
| 3706 | // Neither has qualifiers that are a subset of the other's |
| 3707 | // qualifiers. |
| 3708 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3710 | Result = ImplicitConversionSequence::Better; |
| 3711 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3712 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3713 | if (Result == ImplicitConversionSequence::Better) |
| 3714 | // Neither has qualifiers that are a subset of the other's |
| 3715 | // qualifiers. |
| 3716 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3718 | Result = ImplicitConversionSequence::Worse; |
| 3719 | } else { |
| 3720 | // Qualifiers are disjoint. |
| 3721 | return ImplicitConversionSequence::Indistinguishable; |
| 3722 | } |
| 3723 | |
| 3724 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3725 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3726 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3729 | // Check that the winning standard conversion sequence isn't using |
| 3730 | // the deprecated string literal array to pointer conversion. |
| 3731 | switch (Result) { |
| 3732 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3733 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3734 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3735 | break; |
| 3736 | |
| 3737 | case ImplicitConversionSequence::Indistinguishable: |
| 3738 | break; |
| 3739 | |
| 3740 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3741 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3742 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3743 | break; |
| 3744 | } |
| 3745 | |
| 3746 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3749 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3750 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3751 | /// various kinds of derived-to-base conversions (C++ |
| 3752 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3753 | /// conversions between Objective-C interface types. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3754 | static ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3755 | CompareDerivedToBaseConversions(Sema &S, |
| 3756 | const StandardConversionSequence& SCS1, |
| 3757 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3758 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3759 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3760 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3761 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3762 | |
| 3763 | // Adjust the types we're converting from via the array-to-pointer |
| 3764 | // conversion, if we need to. |
| 3765 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3766 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3767 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3768 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3769 | |
| 3770 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3771 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3772 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3773 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3774 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3775 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3776 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3777 | // |
| 3778 | // If class B is derived directly or indirectly from class A and |
| 3779 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3780 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3781 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3783 | SCS2.Second == ICK_Pointer_Conversion && |
| 3784 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3785 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3786 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3787 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3788 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3790 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3791 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3792 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3793 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3794 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3796 | // -- 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] | 3797 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3798 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3799 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3800 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3801 | return ImplicitConversionSequence::Worse; |
| 3802 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3803 | |
| 3804 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3805 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3806 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3807 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3808 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3809 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3812 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3813 | const ObjCObjectPointerType *FromPtr1 |
| 3814 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3815 | const ObjCObjectPointerType *FromPtr2 |
| 3816 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3817 | const ObjCObjectPointerType *ToPtr1 |
| 3818 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3819 | const ObjCObjectPointerType *ToPtr2 |
| 3820 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3821 | |
| 3822 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3823 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3824 | // that we do for C++ pointers to class types. However, we employ the |
| 3825 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3826 | // Objective-C pointer types. |
| 3827 | bool FromAssignLeft |
| 3828 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3829 | bool FromAssignRight |
| 3830 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3831 | bool ToAssignLeft |
| 3832 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3833 | bool ToAssignRight |
| 3834 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3835 | |
| 3836 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3837 | // type is better than a conversion to 'id'. |
| 3838 | if (ToPtr1->isObjCIdType() && |
| 3839 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3840 | return ImplicitConversionSequence::Worse; |
| 3841 | if (ToPtr2->isObjCIdType() && |
| 3842 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3843 | return ImplicitConversionSequence::Better; |
| 3844 | |
| 3845 | // A conversion to a non-id object pointer type is better than a |
| 3846 | // conversion to a qualified 'id' type |
| 3847 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3848 | return ImplicitConversionSequence::Worse; |
| 3849 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3850 | return ImplicitConversionSequence::Better; |
| 3851 | |
| 3852 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3853 | // type is better than a conversion to 'Class'. |
| 3854 | if (ToPtr1->isObjCClassType() && |
| 3855 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3856 | return ImplicitConversionSequence::Worse; |
| 3857 | if (ToPtr2->isObjCClassType() && |
| 3858 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3859 | return ImplicitConversionSequence::Better; |
| 3860 | |
| 3861 | // A conversion to a non-Class object pointer type is better than a |
| 3862 | // conversion to a qualified 'Class' type. |
| 3863 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3864 | return ImplicitConversionSequence::Worse; |
| 3865 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3866 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3867 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3868 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3869 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3870 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3871 | (ToAssignLeft != ToAssignRight)) |
| 3872 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3873 | : ImplicitConversionSequence::Better; |
| 3874 | |
| 3875 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3876 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3877 | (FromAssignLeft != FromAssignRight)) |
| 3878 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3879 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3880 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3881 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3882 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3883 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3884 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3885 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3886 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3888 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3889 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3890 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3891 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3892 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3893 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3894 | ToType2->getAs<MemberPointerType>(); |
| 3895 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3896 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3897 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3898 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3899 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3900 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3901 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3902 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3903 | // 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] | 3904 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3905 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3906 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3907 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3908 | return ImplicitConversionSequence::Better; |
| 3909 | } |
| 3910 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3911 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3912 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3913 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3914 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3915 | return ImplicitConversionSequence::Worse; |
| 3916 | } |
| 3917 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3918 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3919 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3920 | // -- 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] | 3921 | // -- binding of an expression of type C to a reference of type |
| 3922 | // B& is better than binding an expression of type C to a |
| 3923 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3924 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3925 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3926 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3927 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3928 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3929 | return ImplicitConversionSequence::Worse; |
| 3930 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3931 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3932 | // -- 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] | 3933 | // -- binding of an expression of type B to a reference of type |
| 3934 | // A& is better than binding an expression of type C to a |
| 3935 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3936 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3937 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3938 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3939 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3940 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3941 | return ImplicitConversionSequence::Worse; |
| 3942 | } |
| 3943 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3945 | return ImplicitConversionSequence::Indistinguishable; |
| 3946 | } |
| 3947 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3948 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3949 | /// C++ class. |
| 3950 | static bool isTypeValid(QualType T) { |
| 3951 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3952 | return !Record->isInvalidDecl(); |
| 3953 | |
| 3954 | return true; |
| 3955 | } |
| 3956 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3957 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3958 | /// determine whether they are reference-related, |
| 3959 | /// reference-compatible, reference-compatible with added |
| 3960 | /// qualification, or incompatible, for use in C++ initialization by |
| 3961 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3962 | /// type, and the first type (T1) is the pointee type of the reference |
| 3963 | /// type being initialized. |
| 3964 | Sema::ReferenceCompareResult |
| 3965 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3966 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3967 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3968 | bool &ObjCConversion, |
| 3969 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3970 | assert(!OrigT1->isReferenceType() && |
| 3971 | "T1 must be the pointee type of the reference type"); |
| 3972 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3973 | |
| 3974 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3975 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3976 | Qualifiers T1Quals, T2Quals; |
| 3977 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3978 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3979 | |
| 3980 | // C++ [dcl.init.ref]p4: |
| 3981 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3982 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3983 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3984 | DerivedToBase = false; |
| 3985 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3986 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3987 | if (UnqualT1 == UnqualT2) { |
| 3988 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3989 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3990 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3991 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3992 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3993 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3994 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3995 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3996 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3997 | else |
| 3998 | return Ref_Incompatible; |
| 3999 | |
| 4000 | // At this point, we know that T1 and T2 are reference-related (at |
| 4001 | // least). |
| 4002 | |
| 4003 | // If the type is an array type, promote the element qualifiers to the type |
| 4004 | // for comparison. |
| 4005 | if (isa<ArrayType>(T1) && T1Quals) |
| 4006 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 4007 | if (isa<ArrayType>(T2) && T2Quals) |
| 4008 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 4009 | |
| 4010 | // C++ [dcl.init.ref]p4: |
| 4011 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 4012 | // reference-related to T2 and cv1 is the same cv-qualification |
| 4013 | // as, or greater cv-qualification than, cv2. For purposes of |
| 4014 | // overload resolution, cases for which cv1 is greater |
| 4015 | // cv-qualification than cv2 are identified as |
| 4016 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4017 | // |
| 4018 | // Note that we also require equivalence of Objective-C GC and address-space |
| 4019 | // qualifiers when performing these computations, so that e.g., an int in |
| 4020 | // address space 1 is not reference-compatible with an int in address |
| 4021 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4022 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 4023 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 4024 | if (isNonTrivialObjCLifetimeConversion(T2Quals, T1Quals)) |
| 4025 | ObjCLifetimeConversion = true; |
| 4026 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4027 | T1Quals.removeObjCLifetime(); |
| 4028 | T2Quals.removeObjCLifetime(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4031 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4032 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4033 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4034 | return Ref_Compatible_With_Added_Qualification; |
| 4035 | else |
| 4036 | return Ref_Related; |
| 4037 | } |
| 4038 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4039 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4040 | /// with DeclType. Return true if something definite is found. |
| 4041 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4042 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 4043 | QualType DeclType, SourceLocation DeclLoc, |
| 4044 | Expr *Init, QualType T2, bool AllowRvalues, |
| 4045 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4046 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4047 | CXXRecordDecl *T2RecordDecl |
| 4048 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4049 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4050 | OverloadCandidateSet CandidateSet(DeclLoc, OverloadCandidateSet::CSK_Normal); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4051 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4052 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4053 | NamedDecl *D = *I; |
| 4054 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4055 | if (isa<UsingShadowDecl>(D)) |
| 4056 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4057 | |
| 4058 | FunctionTemplateDecl *ConvTemplate |
| 4059 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4060 | CXXConversionDecl *Conv; |
| 4061 | if (ConvTemplate) |
| 4062 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4063 | else |
| 4064 | Conv = cast<CXXConversionDecl>(D); |
| 4065 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4066 | // 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] | 4067 | // explicit conversions, skip it. |
| 4068 | if (!AllowExplicit && Conv->isExplicit()) |
| 4069 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4071 | if (AllowRvalues) { |
| 4072 | bool DerivedToBase = false; |
| 4073 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4074 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4075 | |
| 4076 | // If we are initializing an rvalue reference, don't permit conversion |
| 4077 | // functions that return lvalues. |
| 4078 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4079 | const ReferenceType *RefType |
| 4080 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4081 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4082 | continue; |
| 4083 | } |
| 4084 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4085 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4086 | S.CompareReferenceRelationship( |
| 4087 | DeclLoc, |
| 4088 | Conv->getConversionType().getNonReferenceType() |
| 4089 | .getUnqualifiedType(), |
| 4090 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4091 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4092 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4093 | continue; |
| 4094 | } else { |
| 4095 | // If the conversion function doesn't return a reference type, |
| 4096 | // it can't be considered for this conversion. An rvalue reference |
| 4097 | // is only acceptable if its referencee is a function type. |
| 4098 | |
| 4099 | const ReferenceType *RefType = |
| 4100 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4101 | if (!RefType || |
| 4102 | (!RefType->isLValueReferenceType() && |
| 4103 | !RefType->getPointeeType()->isFunctionType())) |
| 4104 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4105 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4107 | if (ConvTemplate) |
| 4108 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4109 | Init, DeclType, CandidateSet, |
| 4110 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4111 | else |
| 4112 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4113 | DeclType, CandidateSet, |
| 4114 | /*AllowObjCConversionOnExplicit=*/false); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4117 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4118 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4119 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4120 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4121 | case OR_Success: |
| 4122 | // C++ [over.ics.ref]p1: |
| 4123 | // |
| 4124 | // [...] If the parameter binds directly to the result of |
| 4125 | // applying a conversion function to the argument |
| 4126 | // expression, the implicit conversion sequence is a |
| 4127 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4128 | // second standard conversion sequence either an identity |
| 4129 | // conversion or, if the conversion function returns an |
| 4130 | // entity of a type that is a derived class of the parameter |
| 4131 | // type, a derived-to-base Conversion. |
| 4132 | if (!Best->FinalConversion.DirectBinding) |
| 4133 | return false; |
| 4134 | |
| 4135 | ICS.setUserDefined(); |
| 4136 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4137 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4138 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4139 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4140 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4141 | ICS.UserDefined.EllipsisConversion = false; |
| 4142 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4143 | ICS.UserDefined.After.DirectBinding && |
| 4144 | "Expected a direct reference binding!"); |
| 4145 | return true; |
| 4146 | |
| 4147 | case OR_Ambiguous: |
| 4148 | ICS.setAmbiguous(); |
| 4149 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4150 | Cand != CandidateSet.end(); ++Cand) |
| 4151 | if (Cand->Viable) |
| 4152 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4153 | return true; |
| 4154 | |
| 4155 | case OR_No_Viable_Function: |
| 4156 | case OR_Deleted: |
| 4157 | // There was no suitable conversion, or we found a deleted |
| 4158 | // conversion; continue with other checks. |
| 4159 | return false; |
| 4160 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4161 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4162 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4165 | /// \brief Compute an implicit conversion sequence for reference |
| 4166 | /// initialization. |
| 4167 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4168 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4169 | SourceLocation DeclLoc, |
| 4170 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4171 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4172 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4173 | |
| 4174 | // Most paths end in a failed conversion. |
| 4175 | ImplicitConversionSequence ICS; |
| 4176 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4177 | |
| 4178 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4179 | QualType T2 = Init->getType(); |
| 4180 | |
| 4181 | // If the initializer is the address of an overloaded function, try |
| 4182 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4183 | // type of the resulting function. |
| 4184 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4185 | DeclAccessPair Found; |
| 4186 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4187 | false, Found)) |
| 4188 | T2 = Fn->getType(); |
| 4189 | } |
| 4190 | |
| 4191 | // Compute some basic properties of the types and the initializer. |
| 4192 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4193 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4194 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4195 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4196 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4197 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4198 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4199 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4201 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4202 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4203 | // A reference to type "cv1 T1" is initialized by an expression |
| 4204 | // of type "cv2 T2" as follows: |
| 4205 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4206 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4207 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4208 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4209 | // reference-compatible with "cv2 T2," or |
| 4210 | // |
| 4211 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4212 | if (InitCategory.isLValue() && |
| 4213 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4214 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4215 | // When a parameter of reference type binds directly (8.5.3) |
| 4216 | // to an argument expression, the implicit conversion sequence |
| 4217 | // is the identity conversion, unless the argument expression |
| 4218 | // has a type that is a derived class of the parameter type, |
| 4219 | // in which case the implicit conversion sequence is a |
| 4220 | // derived-to-base Conversion (13.3.3.1). |
| 4221 | ICS.setStandard(); |
| 4222 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4223 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4224 | : ObjCConversion? ICK_Compatible_Conversion |
| 4225 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4226 | ICS.Standard.Third = ICK_Identity; |
| 4227 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4228 | ICS.Standard.setToType(0, T2); |
| 4229 | ICS.Standard.setToType(1, T1); |
| 4230 | ICS.Standard.setToType(2, T1); |
| 4231 | ICS.Standard.ReferenceBinding = true; |
| 4232 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4233 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4234 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4235 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4236 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4237 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4238 | ICS.Standard.CopyConstructor = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4239 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4240 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4241 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4242 | // derived-to-base conversions is suppressed when we're |
| 4243 | // computing the implicit conversion sequence (C++ |
| 4244 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4245 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4246 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4247 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4248 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4249 | // not reference-related to T2, and can be implicitly |
| 4250 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4251 | // is reference-compatible with "cv3 T3" 92) (this |
| 4252 | // conversion is selected by enumerating the applicable |
| 4253 | // conversion functions (13.3.1.6) and choosing the best |
| 4254 | // one through overload resolution (13.3)), |
| 4255 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4256 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4257 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4258 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4259 | Init, T2, /*AllowRvalues=*/false, |
| 4260 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4261 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4262 | } |
| 4263 | } |
| 4264 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4265 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4266 | // 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] | 4267 | // shall be an rvalue reference. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4268 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4269 | return ICS; |
| 4270 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4271 | // -- If the initializer expression |
| 4272 | // |
| 4273 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4274 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4275 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4276 | (InitCategory.isXValue() || |
| 4277 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4278 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4279 | ICS.setStandard(); |
| 4280 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4282 | : ObjCConversion? ICK_Compatible_Conversion |
| 4283 | : ICK_Identity; |
| 4284 | ICS.Standard.Third = ICK_Identity; |
| 4285 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4286 | ICS.Standard.setToType(0, T2); |
| 4287 | ICS.Standard.setToType(1, T1); |
| 4288 | ICS.Standard.setToType(2, T1); |
| 4289 | ICS.Standard.ReferenceBinding = true; |
| 4290 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4291 | // binding unless we're binding to a class prvalue. |
| 4292 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4293 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4294 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4296 | S.getLangOpts().CPlusPlus11 || |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4297 | !(InitCategory.isPRValue() || T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4298 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4299 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4301 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4302 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4303 | ICS.Standard.CopyConstructor = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4304 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4305 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4306 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4307 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4308 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4309 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4310 | // an xvalue, class prvalue, or function lvalue of type |
| 4311 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4312 | // "cv3 T3", |
| 4313 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4314 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4315 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4317 | // class subobject). |
| 4318 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4320 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4321 | Init, T2, /*AllowRvalues=*/true, |
| 4322 | AllowExplicit)) { |
| 4323 | // In the second case, if the reference is an rvalue reference |
| 4324 | // and the second standard conversion sequence of the |
| 4325 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4326 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4327 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4328 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4329 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4330 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4331 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4332 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4333 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4334 | // A temporary of function type cannot be created; don't even try. |
| 4335 | if (T1->isFunctionType()) |
| 4336 | return ICS; |
| 4337 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4338 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4339 | // initialized from the initializer expression using the |
| 4340 | // rules for a non-reference copy initialization (8.5). The |
| 4341 | // reference is then bound to the temporary. If T1 is |
| 4342 | // reference-related to T2, cv1 must be the same |
| 4343 | // cv-qualification as, or greater cv-qualification than, |
| 4344 | // cv2; otherwise, the program is ill-formed. |
| 4345 | if (RefRelationship == Sema::Ref_Related) { |
| 4346 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4347 | // we would be reference-compatible or reference-compatible with |
| 4348 | // added qualification. But that wasn't the case, so the reference |
| 4349 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4350 | // |
| 4351 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4352 | // ObjC GC and lifetime qualifiers aren't important. |
| 4353 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4354 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4355 | T1Quals.removeObjCGCAttr(); |
| 4356 | T1Quals.removeObjCLifetime(); |
| 4357 | T2Quals.removeObjCGCAttr(); |
| 4358 | T2Quals.removeObjCLifetime(); |
| 4359 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4360 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | // If at least one of the types is a class type, the types are not |
| 4364 | // related, and we aren't allowed any user conversions, the |
| 4365 | // reference binding fails. This case is important for breaking |
| 4366 | // recursion, since TryImplicitConversion below will attempt to |
| 4367 | // create a temporary through the use of a copy constructor. |
| 4368 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4369 | (T1->isRecordType() || T2->isRecordType())) |
| 4370 | return ICS; |
| 4371 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4372 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4373 | // reference, the initializer expression shall not be an lvalue. |
| 4374 | if (RefRelationship >= Sema::Ref_Related && |
| 4375 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4376 | return ICS; |
| 4377 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4378 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4379 | // When a parameter of reference type is not bound directly to |
| 4380 | // an argument expression, the conversion sequence is the one |
| 4381 | // required to convert the argument expression to the |
| 4382 | // underlying type of the reference according to |
| 4383 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4384 | // to copy-initializing a temporary of the underlying type with |
| 4385 | // the argument expression. Any difference in top-level |
| 4386 | // cv-qualification is subsumed by the initialization itself |
| 4387 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4388 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4389 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4390 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4391 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4392 | /*AllowObjCWritebackConversion=*/false, |
| 4393 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4394 | |
| 4395 | // Of course, that's still a reference binding. |
| 4396 | if (ICS.isStandard()) { |
| 4397 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4398 | ICS.Standard.IsLvalueReference = !isRValRef; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4399 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4400 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4401 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4402 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4403 | } else if (ICS.isUserDefined()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4404 | const ReferenceType *LValRefType = |
| 4405 | ICS.UserDefined.ConversionFunction->getReturnType() |
| 4406 | ->getAs<LValueReferenceType>(); |
| 4407 | |
| 4408 | // C++ [over.ics.ref]p3: |
| 4409 | // Except for an implicit object parameter, for which see 13.3.1, a |
| 4410 | // standard conversion sequence cannot be formed if it requires [...] |
| 4411 | // binding an rvalue reference to an lvalue other than a function |
| 4412 | // lvalue. |
| 4413 | // Note that the function case is not possible here. |
| 4414 | if (DeclType->isRValueReferenceType() && LValRefType) { |
| 4415 | // FIXME: This is the wrong BadConversionSequence. The problem is binding |
| 4416 | // an rvalue reference to a (non-function) lvalue, not binding an lvalue |
| 4417 | // reference to an rvalue! |
| 4418 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, DeclType); |
| 4419 | return ICS; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4420 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4421 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4422 | ICS.UserDefined.Before.setAsIdentityConversion(); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4423 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4424 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4425 | ICS.UserDefined.After.BindsToFunctionLvalue = false; |
| 4426 | ICS.UserDefined.After.BindsToRvalue = !LValRefType; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4427 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4428 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4429 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4431 | return ICS; |
| 4432 | } |
| 4433 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4434 | static ImplicitConversionSequence |
| 4435 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4436 | bool SuppressUserConversions, |
| 4437 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4438 | bool AllowObjCWritebackConversion, |
| 4439 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4440 | |
| 4441 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4442 | /// initializer list From. |
| 4443 | static ImplicitConversionSequence |
| 4444 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4445 | bool SuppressUserConversions, |
| 4446 | bool InOverloadResolution, |
| 4447 | bool AllowObjCWritebackConversion) { |
| 4448 | // C++11 [over.ics.list]p1: |
| 4449 | // When an argument is an initializer list, it is not an expression and |
| 4450 | // special rules apply for converting it to a parameter type. |
| 4451 | |
| 4452 | ImplicitConversionSequence Result; |
| 4453 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4454 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4455 | // 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] | 4456 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4457 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4458 | return Result; |
| 4459 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4460 | // Per DR1467: |
| 4461 | // If the parameter type is a class X and the initializer list has a single |
| 4462 | // element of type cv U, where U is X or a class derived from X, the |
| 4463 | // implicit conversion sequence is the one required to convert the element |
| 4464 | // to the parameter type. |
| 4465 | // |
| 4466 | // Otherwise, if the parameter type is a character array [... ] |
| 4467 | // and the initializer list has a single element that is an |
| 4468 | // appropriately-typed string literal (8.5.2 [dcl.init.string]), the |
| 4469 | // implicit conversion sequence is the identity conversion. |
| 4470 | if (From->getNumInits() == 1) { |
| 4471 | if (ToType->isRecordType()) { |
| 4472 | QualType InitType = From->getInit(0)->getType(); |
| 4473 | if (S.Context.hasSameUnqualifiedType(InitType, ToType) || |
| 4474 | S.IsDerivedFrom(InitType, ToType)) |
| 4475 | return TryCopyInitialization(S, From->getInit(0), ToType, |
| 4476 | SuppressUserConversions, |
| 4477 | InOverloadResolution, |
| 4478 | AllowObjCWritebackConversion); |
| 4479 | } |
| 4480 | // FIXME: Check the other conditions here: array of character type, |
| 4481 | // initializer is a string literal. |
| 4482 | if (ToType->isArrayType()) { |
| 4483 | InitializedEntity Entity = |
| 4484 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4485 | /*Consumed=*/false); |
| 4486 | if (S.CanPerformCopyInitialization(Entity, From)) { |
| 4487 | Result.setStandard(); |
| 4488 | Result.Standard.setAsIdentityConversion(); |
| 4489 | Result.Standard.setFromType(ToType); |
| 4490 | Result.Standard.setAllToTypes(ToType); |
| 4491 | return Result; |
| 4492 | } |
| 4493 | } |
| 4494 | } |
| 4495 | |
| 4496 | // C++14 [over.ics.list]p2: Otherwise, if the parameter type [...] (below). |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4497 | // C++11 [over.ics.list]p2: |
| 4498 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4499 | // all the elements can be implicitly converted to X, the implicit |
| 4500 | // conversion sequence is the worst conversion necessary to convert an |
| 4501 | // element of the list to X. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4502 | // |
| 4503 | // C++14 [over.ics.list]p3: |
| 4504 | // Otherwise, if the parameter type is "array of N X", if the initializer |
| 4505 | // list has exactly N elements or if it has fewer than N elements and X is |
| 4506 | // default-constructible, and if all the elements of the initializer list |
| 4507 | // can be implicitly converted to X, the implicit conversion sequence is |
| 4508 | // the worst conversion necessary to convert an element of the list to X. |
| 4509 | // |
| 4510 | // FIXME: We're missing a lot of these checks. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4511 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4512 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4513 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4514 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4515 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4516 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4517 | if (!X.isNull()) { |
| 4518 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4519 | Expr *Init = From->getInit(i); |
| 4520 | ImplicitConversionSequence ICS = |
| 4521 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4522 | InOverloadResolution, |
| 4523 | AllowObjCWritebackConversion); |
| 4524 | // If a single element isn't convertible, fail. |
| 4525 | if (ICS.isBad()) { |
| 4526 | Result = ICS; |
| 4527 | break; |
| 4528 | } |
| 4529 | // Otherwise, look for the worst conversion. |
| 4530 | if (Result.isBad() || |
| 4531 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4532 | ImplicitConversionSequence::Worse) |
| 4533 | Result = ICS; |
| 4534 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4535 | |
| 4536 | // For an empty list, we won't have computed any conversion sequence. |
| 4537 | // Introduce the identity conversion sequence. |
| 4538 | if (From->getNumInits() == 0) { |
| 4539 | Result.setStandard(); |
| 4540 | Result.Standard.setAsIdentityConversion(); |
| 4541 | Result.Standard.setFromType(ToType); |
| 4542 | Result.Standard.setAllToTypes(ToType); |
| 4543 | } |
| 4544 | |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4545 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4546 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4547 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4548 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4549 | // C++14 [over.ics.list]p4: |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4550 | // C++11 [over.ics.list]p3: |
| 4551 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4552 | // resolution chooses a single best constructor [...] the implicit |
| 4553 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4554 | // constructors are viable but none is better than the others, the |
| 4555 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4556 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4557 | // This function can deal with initializer lists. |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4558 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4559 | /*AllowExplicit=*/false, |
| 4560 | InOverloadResolution, /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4561 | AllowObjCWritebackConversion, |
| 4562 | /*AllowObjCConversionOnExplicit=*/false); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4563 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4564 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4565 | // C++14 [over.ics.list]p5: |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4566 | // C++11 [over.ics.list]p4: |
| 4567 | // Otherwise, if the parameter has an aggregate type which can be |
| 4568 | // initialized from the initializer list [...] the implicit conversion |
| 4569 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4570 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4571 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4572 | // down to checking whether the initialization works. |
| 4573 | // FIXME: Find out whether this parameter is consumed or not. |
| 4574 | InitializedEntity Entity = |
| 4575 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4576 | /*Consumed=*/false); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4577 | if (S.CanPerformCopyInitialization(Entity, From)) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4578 | Result.setUserDefined(); |
| 4579 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4580 | // Initializer lists don't have a type. |
| 4581 | Result.UserDefined.Before.setFromType(QualType()); |
| 4582 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4583 | |
| 4584 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4585 | Result.UserDefined.After.setFromType(ToType); |
| 4586 | Result.UserDefined.After.setAllToTypes(ToType); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4587 | Result.UserDefined.ConversionFunction = nullptr; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4588 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4589 | return Result; |
| 4590 | } |
| 4591 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4592 | // C++14 [over.ics.list]p6: |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4593 | // C++11 [over.ics.list]p5: |
| 4594 | // 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] | 4595 | if (ToType->isReferenceType()) { |
| 4596 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4597 | // mention initializer lists in any way. So we go by what list- |
| 4598 | // initialization would do and try to extrapolate from that. |
| 4599 | |
| 4600 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4601 | |
| 4602 | // If the initializer list has a single element that is reference-related |
| 4603 | // to the parameter type, we initialize the reference from that. |
| 4604 | if (From->getNumInits() == 1) { |
| 4605 | Expr *Init = From->getInit(0); |
| 4606 | |
| 4607 | QualType T2 = Init->getType(); |
| 4608 | |
| 4609 | // If the initializer is the address of an overloaded function, try |
| 4610 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4611 | // type of the resulting function. |
| 4612 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4613 | DeclAccessPair Found; |
| 4614 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4615 | Init, ToType, false, Found)) |
| 4616 | T2 = Fn->getType(); |
| 4617 | } |
| 4618 | |
| 4619 | // Compute some basic properties of the types and the initializer. |
| 4620 | bool dummy1 = false; |
| 4621 | bool dummy2 = false; |
| 4622 | bool dummy3 = false; |
| 4623 | Sema::ReferenceCompareResult RefRelationship |
| 4624 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4625 | dummy2, dummy3); |
| 4626 | |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4627 | if (RefRelationship >= Sema::Ref_Related) { |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4628 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4629 | SuppressUserConversions, |
| 4630 | /*AllowExplicit=*/false); |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4631 | } |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | // Otherwise, we bind the reference to a temporary created from the |
| 4635 | // initializer list. |
| 4636 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4637 | InOverloadResolution, |
| 4638 | AllowObjCWritebackConversion); |
| 4639 | if (Result.isFailure()) |
| 4640 | return Result; |
| 4641 | assert(!Result.isEllipsis() && |
| 4642 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4643 | |
| 4644 | // Can we even bind to a temporary? |
| 4645 | if (ToType->isRValueReferenceType() || |
| 4646 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4647 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4648 | Result.UserDefined.After; |
| 4649 | SCS.ReferenceBinding = true; |
| 4650 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4651 | SCS.BindsToRvalue = true; |
| 4652 | SCS.BindsToFunctionLvalue = false; |
| 4653 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4654 | SCS.ObjCLifetimeConversionBinding = false; |
| 4655 | } else |
| 4656 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4657 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4658 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4659 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4660 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4661 | // C++14 [over.ics.list]p7: |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4662 | // C++11 [over.ics.list]p6: |
| 4663 | // Otherwise, if the parameter type is not a class: |
| 4664 | if (!ToType->isRecordType()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4665 | // - if the initializer list has one element that is not itself an |
| 4666 | // initializer list, the implicit conversion sequence is the one |
| 4667 | // required to convert the element to the parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4668 | unsigned NumInits = From->getNumInits(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4669 | if (NumInits == 1 && !isa<InitListExpr>(From->getInit(0))) |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4670 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4671 | SuppressUserConversions, |
| 4672 | InOverloadResolution, |
| 4673 | AllowObjCWritebackConversion); |
| 4674 | // - if the initializer list has no elements, the implicit conversion |
| 4675 | // sequence is the identity conversion. |
| 4676 | else if (NumInits == 0) { |
| 4677 | Result.setStandard(); |
| 4678 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4679 | Result.Standard.setFromType(ToType); |
| 4680 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4681 | } |
| 4682 | return Result; |
| 4683 | } |
| 4684 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4685 | // C++14 [over.ics.list]p8: |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4686 | // C++11 [over.ics.list]p7: |
| 4687 | // In all cases other than those enumerated above, no conversion is possible |
| 4688 | return Result; |
| 4689 | } |
| 4690 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4691 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4692 | /// ToType from the expression From. Return the implicit conversion |
| 4693 | /// sequence required to pass this argument, which may be a bad |
| 4694 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4695 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4696 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4697 | static ImplicitConversionSequence |
| 4698 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4699 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4700 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4701 | bool AllowObjCWritebackConversion, |
| 4702 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4703 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4704 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4705 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4706 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4707 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4708 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4709 | /*FIXME:*/From->getLocStart(), |
| 4710 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4711 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4712 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4713 | return TryImplicitConversion(S, From, ToType, |
| 4714 | SuppressUserConversions, |
| 4715 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4716 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4717 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4718 | AllowObjCWritebackConversion, |
| 4719 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4722 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4723 | const CanQualType ToQTy, |
| 4724 | Sema &S, |
| 4725 | SourceLocation Loc, |
| 4726 | ExprValueKind FromVK) { |
| 4727 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4728 | ImplicitConversionSequence ICS = |
| 4729 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4730 | |
| 4731 | return !ICS.isBad(); |
| 4732 | } |
| 4733 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4734 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4735 | /// parameter of the given member function (@c Method) from the |
| 4736 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4737 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4738 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4739 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4740 | CXXMethodDecl *Method, |
| 4741 | CXXRecordDecl *ActingContext) { |
| 4742 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4743 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4744 | // const volatile object. |
| 4745 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4746 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4747 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4748 | |
| 4749 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4750 | // to exit early. |
| 4751 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4752 | |
| 4753 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4754 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4755 | FromType = PT->getPointeeType(); |
| 4756 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4757 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4758 | // better have an lvalue. |
| 4759 | assert(FromClassification.isLValue()); |
| 4760 | } |
| 4761 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4762 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4764 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4765 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4766 | // parameter is |
| 4767 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4768 | // - "lvalue reference to cv X" for functions declared without a |
| 4769 | // ref-qualifier or with the & ref-qualifier |
| 4770 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4771 | // ref-qualifier |
| 4772 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4773 | // 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] | 4774 | // cv-qualification on the member function declaration. |
| 4775 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4776 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4777 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4778 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4779 | // reference binding here, that allows class rvalues to bind to |
| 4780 | // non-constant references. |
| 4781 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4782 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4783 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4784 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4785 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4786 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4787 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4788 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4789 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4790 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4791 | |
| 4792 | // Check that we have either the same type or a derived type. It |
| 4793 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4794 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4795 | ImplicitConversionKind SecondKind; |
| 4796 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4797 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4798 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4799 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4800 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4801 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4802 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4803 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4804 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4805 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4806 | // Check the ref-qualifier. |
| 4807 | switch (Method->getRefQualifier()) { |
| 4808 | case RQ_None: |
| 4809 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4810 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4812 | case RQ_LValue: |
| 4813 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4814 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4816 | ImplicitParamType); |
| 4817 | return ICS; |
| 4818 | } |
| 4819 | break; |
| 4820 | |
| 4821 | case RQ_RValue: |
| 4822 | if (!FromClassification.isRValue()) { |
| 4823 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4824 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4825 | ImplicitParamType); |
| 4826 | return ICS; |
| 4827 | } |
| 4828 | break; |
| 4829 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4830 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4831 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4832 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4833 | ICS.Standard.setAsIdentityConversion(); |
| 4834 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4835 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4836 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4837 | ICS.Standard.ReferenceBinding = true; |
| 4838 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4840 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4841 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4842 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4843 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4844 | return ICS; |
| 4845 | } |
| 4846 | |
| 4847 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4848 | /// the implicit object parameter for the given Method with the given |
| 4849 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4850 | ExprResult |
| 4851 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4852 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4853 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4854 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4855 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4856 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4857 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4859 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4860 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4861 | FromRecordType = PT->getPointeeType(); |
| 4862 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4863 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4864 | } else { |
| 4865 | FromRecordType = From->getType(); |
| 4866 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4867 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4870 | // Note that we always use the true parent context when performing |
| 4871 | // the actual argument initialization. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4872 | ImplicitConversionSequence ICS = TryObjectArgumentInitialization( |
| 4873 | *this, From->getType(), FromClassification, Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4874 | if (ICS.isBad()) { |
| 4875 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4876 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4877 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4878 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4879 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4880 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4881 | diag::err_member_function_call_bad_cvr) |
| 4882 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4883 | << From->getSourceRange(); |
| 4884 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4885 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4886 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4890 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4891 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4892 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4894 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4895 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4896 | ExprResult FromRes = |
| 4897 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4898 | if (FromRes.isInvalid()) |
| 4899 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4900 | From = FromRes.get(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4901 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4903 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4904 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4905 | From->getValueKind()).get(); |
| 4906 | return From; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4909 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4910 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4911 | static ImplicitConversionSequence |
| 4912 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4913 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4914 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4916 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4917 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4918 | /*AllowObjCWritebackConversion=*/false, |
| 4919 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4923 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4924 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4925 | if (checkPlaceholderForOverload(*this, From)) |
| 4926 | return ExprError(); |
| 4927 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4928 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4929 | if (!ICS.isBad()) |
| 4930 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4931 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4932 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4933 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4934 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4935 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4936 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4937 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4938 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4939 | /// Check that the specified conversion is permitted in a converted constant |
| 4940 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4941 | /// is acceptable. |
| 4942 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4943 | StandardConversionSequence &SCS) { |
| 4944 | // Since we know that the target type is an integral or unscoped enumeration |
| 4945 | // type, most conversion kinds are impossible. All possible First and Third |
| 4946 | // conversions are fine. |
| 4947 | switch (SCS.Second) { |
| 4948 | case ICK_Identity: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4949 | case ICK_NoReturn_Adjustment: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4950 | case ICK_Integral_Promotion: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4951 | case ICK_Integral_Conversion: // Narrowing conversions are checked elsewhere. |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4952 | return true; |
| 4953 | |
| 4954 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4955 | // Conversion from an integral or unscoped enumeration type to bool is |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4956 | // classified as ICK_Boolean_Conversion, but it's also arguably an integral |
| 4957 | // conversion, so we allow it in a converted constant expression. |
| 4958 | // |
| 4959 | // FIXME: Per core issue 1407, we should not allow this, but that breaks |
| 4960 | // a lot of popular code. We should at least add a warning for this |
| 4961 | // (non-conforming) extension. |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4962 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4963 | SCS.getToType(2)->isBooleanType(); |
| 4964 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4965 | case ICK_Pointer_Conversion: |
| 4966 | case ICK_Pointer_Member: |
| 4967 | // C++1z: null pointer conversions and null member pointer conversions are |
| 4968 | // only permitted if the source type is std::nullptr_t. |
| 4969 | return SCS.getFromType()->isNullPtrType(); |
| 4970 | |
| 4971 | case ICK_Floating_Promotion: |
| 4972 | case ICK_Complex_Promotion: |
| 4973 | case ICK_Floating_Conversion: |
| 4974 | case ICK_Complex_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4975 | case ICK_Floating_Integral: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4976 | case ICK_Compatible_Conversion: |
| 4977 | case ICK_Derived_To_Base: |
| 4978 | case ICK_Vector_Conversion: |
| 4979 | case ICK_Vector_Splat: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4980 | case ICK_Complex_Real: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4981 | case ICK_Block_Pointer_Conversion: |
| 4982 | case ICK_TransparentUnionConversion: |
| 4983 | case ICK_Writeback_Conversion: |
| 4984 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4985 | return false; |
| 4986 | |
| 4987 | case ICK_Lvalue_To_Rvalue: |
| 4988 | case ICK_Array_To_Pointer: |
| 4989 | case ICK_Function_To_Pointer: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4990 | llvm_unreachable("found a first conversion kind in Second"); |
| 4991 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4992 | case ICK_Qualification: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4993 | llvm_unreachable("found a third conversion kind in Second"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4994 | |
| 4995 | case ICK_Num_Conversion_Kinds: |
| 4996 | break; |
| 4997 | } |
| 4998 | |
| 4999 | llvm_unreachable("unknown conversion kind"); |
| 5000 | } |
| 5001 | |
| 5002 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 5003 | /// converted constant expression of type T, perform the conversion and produce |
| 5004 | /// the converted expression, per C++11 [expr.const]p3. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5005 | static ExprResult CheckConvertedConstantExpression(Sema &S, Expr *From, |
| 5006 | QualType T, APValue &Value, |
| 5007 | Sema::CCEKind CCE, |
| 5008 | bool RequireInt) { |
| 5009 | assert(S.getLangOpts().CPlusPlus11 && |
| 5010 | "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5011 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5012 | if (checkPlaceholderForOverload(S, From)) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5013 | return ExprError(); |
| 5014 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5015 | // C++1z [expr.const]p3: |
| 5016 | // A converted constant expression of type T is an expression, |
| 5017 | // implicitly converted to type T, where the converted |
| 5018 | // expression is a constant expression and the implicit conversion |
| 5019 | // sequence contains only [... list of conversions ...]. |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5020 | ImplicitConversionSequence ICS = |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5021 | TryCopyInitialization(S, From, T, |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5022 | /*SuppressUserConversions=*/false, |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5023 | /*InOverloadResolution=*/false, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5024 | /*AllowObjcWritebackConversion=*/false, |
| 5025 | /*AllowExplicit=*/false); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5026 | StandardConversionSequence *SCS = nullptr; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5027 | switch (ICS.getKind()) { |
| 5028 | case ImplicitConversionSequence::StandardConversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5029 | SCS = &ICS.Standard; |
| 5030 | break; |
| 5031 | case ImplicitConversionSequence::UserDefinedConversion: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5032 | // We are converting to a non-class type, so the Before sequence |
| 5033 | // must be trivial. |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5034 | SCS = &ICS.UserDefined.After; |
| 5035 | break; |
| 5036 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5037 | case ImplicitConversionSequence::BadConversion: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5038 | if (!S.DiagnoseMultipleUserDefinedConversion(From, T)) |
| 5039 | return S.Diag(From->getLocStart(), |
| 5040 | diag::err_typecheck_converted_constant_expression) |
| 5041 | << From->getType() << From->getSourceRange() << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5042 | return ExprError(); |
| 5043 | |
| 5044 | case ImplicitConversionSequence::EllipsisConversion: |
| 5045 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 5046 | } |
| 5047 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5048 | // Check that we would only use permitted conversions. |
| 5049 | if (!CheckConvertedConstantConversions(S, *SCS)) { |
| 5050 | return S.Diag(From->getLocStart(), |
| 5051 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 5052 | << From->getType() << From->getSourceRange() << T; |
| 5053 | } |
| 5054 | // [...] and where the reference binding (if any) binds directly. |
| 5055 | if (SCS->ReferenceBinding && !SCS->DirectBinding) { |
| 5056 | return S.Diag(From->getLocStart(), |
| 5057 | diag::err_typecheck_converted_constant_expression_indirect) |
| 5058 | << From->getType() << From->getSourceRange() << T; |
| 5059 | } |
| 5060 | |
| 5061 | ExprResult Result = |
| 5062 | S.PerformImplicitConversion(From, T, ICS, Sema::AA_Converting); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5063 | if (Result.isInvalid()) |
| 5064 | return Result; |
| 5065 | |
| 5066 | // Check for a narrowing implicit conversion. |
| 5067 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5068 | QualType PreNarrowingType; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5069 | switch (SCS->getNarrowingKind(S.Context, Result.get(), PreNarrowingValue, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5070 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5071 | case NK_Variable_Narrowing: |
| 5072 | // Implicit conversion to a narrower type, and the value is not a constant |
| 5073 | // expression. We'll diagnose this in a moment. |
| 5074 | case NK_Not_Narrowing: |
| 5075 | break; |
| 5076 | |
| 5077 | case NK_Constant_Narrowing: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5078 | S.Diag(From->getLocStart(), diag::ext_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5079 | << CCE << /*Constant*/1 |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5080 | << PreNarrowingValue.getAsString(S.Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5081 | break; |
| 5082 | |
| 5083 | case NK_Type_Narrowing: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5084 | S.Diag(From->getLocStart(), diag::ext_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5085 | << CCE << /*Constant*/0 << From->getType() << T; |
| 5086 | break; |
| 5087 | } |
| 5088 | |
| 5089 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5090 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5091 | Expr::EvalResult Eval; |
| 5092 | Eval.Diag = &Notes; |
| 5093 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5094 | if ((T->isReferenceType() |
| 5095 | ? !Result.get()->EvaluateAsLValue(Eval, S.Context) |
| 5096 | : !Result.get()->EvaluateAsRValue(Eval, S.Context)) || |
| 5097 | (RequireInt && !Eval.Val.isInt())) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5098 | // The expression can't be folded, so we can't keep it at this position in |
| 5099 | // the AST. |
| 5100 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5101 | } else { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5102 | Value = Eval.Val; |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5103 | |
| 5104 | if (Notes.empty()) { |
| 5105 | // It's a constant expression. |
| 5106 | return Result; |
| 5107 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5111 | if (Notes.size() == 1 && |
| 5112 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5113 | S.Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5114 | else { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5115 | S.Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5116 | << CCE << From->getSourceRange(); |
| 5117 | for (unsigned I = 0; I < Notes.size(); ++I) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5118 | S.Diag(Notes[I].first, Notes[I].second); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5119 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5120 | return ExprError(); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5123 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 5124 | APValue &Value, CCEKind CCE) { |
| 5125 | return ::CheckConvertedConstantExpression(*this, From, T, Value, CCE, false); |
| 5126 | } |
| 5127 | |
| 5128 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 5129 | llvm::APSInt &Value, |
| 5130 | CCEKind CCE) { |
| 5131 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 5132 | |
| 5133 | APValue V; |
| 5134 | auto R = ::CheckConvertedConstantExpression(*this, From, T, V, CCE, true); |
| 5135 | if (!R.isInvalid()) |
| 5136 | Value = V.getInt(); |
| 5137 | return R; |
| 5138 | } |
| 5139 | |
| 5140 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5141 | /// dropPointerConversions - If the given standard conversion sequence |
| 5142 | /// involves any pointer conversions, remove them. This may change |
| 5143 | /// the result type of the conversion sequence. |
| 5144 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5145 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5146 | SCS.Second = ICK_Identity; |
| 5147 | SCS.Third = ICK_Identity; |
| 5148 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5149 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5150 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5151 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5152 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5153 | /// convert the expression From to an Objective-C pointer type. |
| 5154 | static ImplicitConversionSequence |
| 5155 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5156 | // Do an implicit conversion to 'id'. |
| 5157 | QualType Ty = S.Context.getObjCIdType(); |
| 5158 | ImplicitConversionSequence ICS |
| 5159 | = TryImplicitConversion(S, From, Ty, |
| 5160 | // FIXME: Are these flags correct? |
| 5161 | /*SuppressUserConversions=*/false, |
| 5162 | /*AllowExplicit=*/true, |
| 5163 | /*InOverloadResolution=*/false, |
| 5164 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5165 | /*AllowObjCWritebackConversion=*/false, |
| 5166 | /*AllowObjCConversionOnExplicit=*/true); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5167 | |
| 5168 | // Strip off any final conversions to 'id'. |
| 5169 | switch (ICS.getKind()) { |
| 5170 | case ImplicitConversionSequence::BadConversion: |
| 5171 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5172 | case ImplicitConversionSequence::EllipsisConversion: |
| 5173 | break; |
| 5174 | |
| 5175 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5176 | dropPointerConversion(ICS.UserDefined.After); |
| 5177 | break; |
| 5178 | |
| 5179 | case ImplicitConversionSequence::StandardConversion: |
| 5180 | dropPointerConversion(ICS.Standard); |
| 5181 | break; |
| 5182 | } |
| 5183 | |
| 5184 | return ICS; |
| 5185 | } |
| 5186 | |
| 5187 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5188 | /// conversion of the expression From to an Objective-C pointer type. |
| 5189 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5190 | if (checkPlaceholderForOverload(*this, From)) |
| 5191 | return ExprError(); |
| 5192 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5193 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5194 | ImplicitConversionSequence ICS = |
| 5195 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5196 | if (!ICS.isBad()) |
| 5197 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5198 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5199 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5200 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5201 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5202 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5203 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5204 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5205 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5208 | static ExprResult |
| 5209 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5210 | Sema::ContextualImplicitConverter &Converter, |
| 5211 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5212 | |
| 5213 | if (Converter.Suppress) |
| 5214 | return ExprError(); |
| 5215 | |
| 5216 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5217 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5218 | CXXConversionDecl *Conv = |
| 5219 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5220 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5221 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5222 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5223 | return From; |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | static bool |
| 5227 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5228 | Sema::ContextualImplicitConverter &Converter, |
| 5229 | QualType T, bool HadMultipleCandidates, |
| 5230 | UnresolvedSetImpl &ExplicitConversions) { |
| 5231 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5232 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5233 | CXXConversionDecl *Conversion = |
| 5234 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5235 | |
| 5236 | // The user probably meant to invoke the given explicit |
| 5237 | // conversion; use it. |
| 5238 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5239 | std::string TypeStr; |
| 5240 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5241 | |
| 5242 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5243 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5244 | "static_cast<" + TypeStr + ">(") |
| 5245 | << FixItHint::CreateInsertion( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5246 | SemaRef.getLocForEndOfToken(From->getLocEnd()), ")"); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5247 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5248 | |
| 5249 | // If we aren't in a SFINAE context, build a call to the |
| 5250 | // explicit conversion function. |
| 5251 | if (SemaRef.isSFINAEContext()) |
| 5252 | return true; |
| 5253 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5254 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, nullptr, Found); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5255 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5256 | HadMultipleCandidates); |
| 5257 | if (Result.isInvalid()) |
| 5258 | return true; |
| 5259 | // Record usage of conversion in an implicit cast. |
| 5260 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5261 | CK_UserDefinedConversion, Result.get(), |
| 5262 | nullptr, Result.get()->getValueKind()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5263 | } |
| 5264 | return false; |
| 5265 | } |
| 5266 | |
| 5267 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5268 | Sema::ContextualImplicitConverter &Converter, |
| 5269 | QualType T, bool HadMultipleCandidates, |
| 5270 | DeclAccessPair &Found) { |
| 5271 | CXXConversionDecl *Conversion = |
| 5272 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5273 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, nullptr, Found); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5274 | |
| 5275 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5276 | if (!Converter.SuppressConversion) { |
| 5277 | if (SemaRef.isSFINAEContext()) |
| 5278 | return true; |
| 5279 | |
| 5280 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5281 | << From->getSourceRange(); |
| 5282 | } |
| 5283 | |
| 5284 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5285 | HadMultipleCandidates); |
| 5286 | if (Result.isInvalid()) |
| 5287 | return true; |
| 5288 | // Record usage of conversion in an implicit cast. |
| 5289 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5290 | CK_UserDefinedConversion, Result.get(), |
| 5291 | nullptr, Result.get()->getValueKind()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5292 | return false; |
| 5293 | } |
| 5294 | |
| 5295 | static ExprResult finishContextualImplicitConversion( |
| 5296 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5297 | Sema::ContextualImplicitConverter &Converter) { |
| 5298 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5299 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5300 | << From->getSourceRange(); |
| 5301 | |
| 5302 | return SemaRef.DefaultLvalueConversion(From); |
| 5303 | } |
| 5304 | |
| 5305 | static void |
| 5306 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5307 | UnresolvedSetImpl &ViableConversions, |
| 5308 | OverloadCandidateSet &CandidateSet) { |
| 5309 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5310 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5311 | NamedDecl *D = FoundDecl.getDecl(); |
| 5312 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5313 | if (isa<UsingShadowDecl>(D)) |
| 5314 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5315 | |
| 5316 | CXXConversionDecl *Conv; |
| 5317 | FunctionTemplateDecl *ConvTemplate; |
| 5318 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5319 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5320 | else |
| 5321 | Conv = cast<CXXConversionDecl>(D); |
| 5322 | |
| 5323 | if (ConvTemplate) |
| 5324 | SemaRef.AddTemplateConversionCandidate( |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5325 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet, |
| 5326 | /*AllowObjCConversionOnExplicit=*/false); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5327 | else |
| 5328 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5329 | ToType, CandidateSet, |
| 5330 | /*AllowObjCConversionOnExplicit=*/false); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5331 | } |
| 5332 | } |
| 5333 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5334 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5335 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5336 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5337 | /// This routine will attempt to convert an expression of class type to a |
| 5338 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5339 | /// must have a single non-explicit conversion function converting to a matching |
| 5340 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5341 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5342 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5343 | /// \param Loc The source location of the construct that requires the |
| 5344 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5345 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5346 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5347 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5348 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5349 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5350 | /// \returns The expression, converted to an integral or enumeration type if |
| 5351 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5352 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5353 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5354 | // We can't perform any more checking for type-dependent expressions. |
| 5355 | if (From->isTypeDependent()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5356 | return From; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5357 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5358 | // Process placeholders immediately. |
| 5359 | if (From->hasPlaceholderType()) { |
| 5360 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5361 | if (result.isInvalid()) |
| 5362 | return result; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5363 | From = result.get(); |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5366 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5367 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5368 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5369 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5370 | |
| 5371 | // FIXME: Check for missing '()' if T is a function type? |
| 5372 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5373 | // We can only perform contextual implicit conversions on objects of class |
| 5374 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5375 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5376 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5377 | if (!Converter.Suppress) |
| 5378 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5379 | return From; |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5382 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5383 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5384 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5385 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5386 | |
| 5387 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5388 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5389 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5390 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5391 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5392 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5393 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5394 | |
| 5395 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5396 | return From; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5398 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5399 | UnresolvedSet<4> |
| 5400 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5401 | UnresolvedSet<4> ExplicitConversions; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5402 | const auto &Conversions = |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5403 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5405 | bool HadMultipleCandidates = |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5406 | (std::distance(Conversions.begin(), Conversions.end()) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5407 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5408 | // To check that there is only one target type, in C++1y: |
| 5409 | QualType ToType; |
| 5410 | bool HasUniqueTargetType = true; |
| 5411 | |
| 5412 | // Collect explicit or viable (potentially in C++1y) conversions. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5413 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5414 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5415 | CXXConversionDecl *Conversion; |
| 5416 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5417 | if (ConvTemplate) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5418 | if (getLangOpts().CPlusPlus14) |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5419 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5420 | else |
| 5421 | continue; // C++11 does not consider conversion operator templates(?). |
| 5422 | } else |
| 5423 | Conversion = cast<CXXConversionDecl>(D); |
| 5424 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5425 | assert((!ConvTemplate || getLangOpts().CPlusPlus14) && |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5426 | "Conversion operator templates are considered potentially " |
| 5427 | "viable in C++1y"); |
| 5428 | |
| 5429 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5430 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5431 | |
| 5432 | if (Conversion->isExplicit()) { |
| 5433 | // FIXME: For C++1y, do we need this restriction? |
| 5434 | // cf. diagnoseNoViableConversion() |
| 5435 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5436 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5437 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5438 | if (!ConvTemplate && getLangOpts().CPlusPlus14) { |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5439 | if (ToType.isNull()) |
| 5440 | ToType = CurToType.getUnqualifiedType(); |
| 5441 | else if (HasUniqueTargetType && |
| 5442 | (CurToType.getUnqualifiedType() != ToType)) |
| 5443 | HasUniqueTargetType = false; |
| 5444 | } |
| 5445 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5446 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5447 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5449 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5450 | if (getLangOpts().CPlusPlus14) { |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5451 | // C++1y [conv]p6: |
| 5452 | // ... An expression e of class type E appearing in such a context |
| 5453 | // is said to be contextually implicitly converted to a specified |
| 5454 | // type T and is well-formed if and only if e can be implicitly |
| 5455 | // converted to a type T that is determined as follows: E is searched |
Larisse Voufo | 7acc5a6 | 2013-06-10 08:25:58 +0000 | [diff] [blame] | 5456 | // for conversion functions whose return type is cv T or reference to |
| 5457 | // cv T such that T is allowed by the context. There shall be |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5458 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5460 | // If no unique T is found: |
| 5461 | if (ToType.isNull()) { |
| 5462 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5463 | HadMultipleCandidates, |
| 5464 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5465 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5466 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5468 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5469 | // If more than one unique Ts are found: |
| 5470 | if (!HasUniqueTargetType) |
| 5471 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5472 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5473 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5474 | // If one unique T is found: |
| 5475 | // First, build a candidate set from the previously recorded |
| 5476 | // potentially viable conversions. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5477 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5478 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5479 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5480 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5481 | // Then, perform overload resolution over the candidate set. |
| 5482 | OverloadCandidateSet::iterator Best; |
| 5483 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5484 | case OR_Success: { |
| 5485 | // Apply this conversion. |
| 5486 | DeclAccessPair Found = |
| 5487 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5488 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5489 | HadMultipleCandidates, Found)) |
| 5490 | return ExprError(); |
| 5491 | break; |
| 5492 | } |
| 5493 | case OR_Ambiguous: |
| 5494 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5495 | ViableConversions); |
| 5496 | case OR_No_Viable_Function: |
| 5497 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5498 | HadMultipleCandidates, |
| 5499 | ExplicitConversions)) |
| 5500 | return ExprError(); |
| 5501 | // fall through 'OR_Deleted' case. |
| 5502 | case OR_Deleted: |
| 5503 | // We'll complain below about a non-integral condition type. |
| 5504 | break; |
| 5505 | } |
| 5506 | } else { |
| 5507 | switch (ViableConversions.size()) { |
| 5508 | case 0: { |
| 5509 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5510 | HadMultipleCandidates, |
| 5511 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5512 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5513 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5514 | // We'll complain below about a non-integral condition type. |
| 5515 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5516 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5517 | case 1: { |
| 5518 | // Apply this conversion. |
| 5519 | DeclAccessPair Found = ViableConversions[0]; |
| 5520 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5521 | HadMultipleCandidates, Found)) |
| 5522 | return ExprError(); |
| 5523 | break; |
| 5524 | } |
| 5525 | default: |
| 5526 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5527 | ViableConversions); |
| 5528 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5529 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5530 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5531 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5532 | } |
| 5533 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5534 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 5535 | /// an acceptable non-member overloaded operator for a call whose |
| 5536 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 5537 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 5538 | /// enumeration types. |
| 5539 | static bool IsAcceptableNonMemberOperatorCandidate(ASTContext &Context, |
| 5540 | FunctionDecl *Fn, |
| 5541 | ArrayRef<Expr *> Args) { |
| 5542 | QualType T1 = Args[0]->getType(); |
| 5543 | QualType T2 = Args.size() > 1 ? Args[1]->getType() : QualType(); |
| 5544 | |
| 5545 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 5546 | return true; |
| 5547 | |
| 5548 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 5549 | return true; |
| 5550 | |
| 5551 | const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); |
| 5552 | if (Proto->getNumParams() < 1) |
| 5553 | return false; |
| 5554 | |
| 5555 | if (T1->isEnumeralType()) { |
| 5556 | QualType ArgType = Proto->getParamType(0).getNonReferenceType(); |
| 5557 | if (Context.hasSameUnqualifiedType(T1, ArgType)) |
| 5558 | return true; |
| 5559 | } |
| 5560 | |
| 5561 | if (Proto->getNumParams() < 2) |
| 5562 | return false; |
| 5563 | |
| 5564 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 5565 | QualType ArgType = Proto->getParamType(1).getNonReferenceType(); |
| 5566 | if (Context.hasSameUnqualifiedType(T2, ArgType)) |
| 5567 | return true; |
| 5568 | } |
| 5569 | |
| 5570 | return false; |
| 5571 | } |
| 5572 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5573 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5574 | /// candidate functions, using the given function call arguments. If |
| 5575 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5576 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5577 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5578 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5579 | /// based on an incomplete set of function arguments. This feature is used by |
| 5580 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5581 | void |
| 5582 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5583 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5584 | ArrayRef<Expr *> Args, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5585 | OverloadCandidateSet &CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5586 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5587 | bool PartialOverloading, |
| 5588 | bool AllowExplicit) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5589 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5590 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5591 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5592 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5593 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5594 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5595 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5596 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5597 | // If we get here, it's because we're calling a member function |
| 5598 | // that is named without a member access expression (e.g., |
| 5599 | // "this->f") that was either written explicitly or created |
| 5600 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5601 | // function, e.g., X::f(). We use an empty type for the implied |
| 5602 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5603 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5604 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5605 | QualType(), Expr::Classification::makeSimpleLValue(), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5606 | Args, CandidateSet, SuppressUserConversions, |
| 5607 | PartialOverloading); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5608 | return; |
| 5609 | } |
| 5610 | // We treat a constructor like a non-member function, since its object |
| 5611 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5614 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5615 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5616 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5617 | // C++ [over.match.oper]p3: |
| 5618 | // if no operand has a class type, only those non-member functions in the |
| 5619 | // lookup set that have a first parameter of type T1 or "reference to |
| 5620 | // (possibly cv-qualified) T1", when T1 is an enumeration type, or (if there |
| 5621 | // is a right operand) a second parameter of type T2 or "reference to |
| 5622 | // (possibly cv-qualified) T2", when T2 is an enumeration type, are |
| 5623 | // candidate functions. |
| 5624 | if (CandidateSet.getKind() == OverloadCandidateSet::CSK_Operator && |
| 5625 | !IsAcceptableNonMemberOperatorCandidate(Context, Function, Args)) |
| 5626 | return; |
| 5627 | |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5628 | // C++11 [class.copy]p11: [DR1402] |
| 5629 | // A defaulted move constructor that is defined as deleted is ignored by |
| 5630 | // overload resolution. |
| 5631 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function); |
| 5632 | if (Constructor && Constructor->isDefaulted() && Constructor->isDeleted() && |
| 5633 | Constructor->isMoveConstructor()) |
| 5634 | return; |
| 5635 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5636 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5637 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5638 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5639 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5640 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5641 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5642 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5643 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5644 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5645 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5646 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5647 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5648 | if (Constructor) { |
| 5649 | // C++ [class.copy]p3: |
| 5650 | // A member function template is never instantiated to perform the copy |
| 5651 | // of a class object to an object of its class type. |
| 5652 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 5653 | if (Args.size() == 1 && |
| 5654 | Constructor->isSpecializationCopyingObject() && |
| 5655 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5656 | IsDerivedFrom(Args[0]->getType(), ClassType))) { |
| 5657 | Candidate.Viable = false; |
| 5658 | Candidate.FailureKind = ovl_fail_illegal_constructor; |
| 5659 | return; |
| 5660 | } |
| 5661 | } |
| 5662 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5663 | unsigned NumParams = Proto->getNumParams(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5664 | |
| 5665 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5666 | // parameters is viable only if it has an ellipsis in its parameter |
| 5667 | // list (8.3.5). |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5668 | if (TooManyArguments(NumParams, Args.size(), PartialOverloading) && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5669 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5670 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5671 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5672 | return; |
| 5673 | } |
| 5674 | |
| 5675 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5676 | // is viable only if the (m+1)st parameter has a default argument |
| 5677 | // (8.3.6). For the purposes of overload resolution, the |
| 5678 | // parameter list is truncated on the right, so that there are |
| 5679 | // exactly m parameters. |
| 5680 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5681 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5682 | // Not enough arguments. |
| 5683 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5684 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5685 | return; |
| 5686 | } |
| 5687 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5688 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5689 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5690 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5691 | // Skip the check for callers that are implicit members, because in this |
| 5692 | // case we may not yet know what the member's target is; the target is |
| 5693 | // inferred for the member automatically, based on the bases and fields of |
| 5694 | // the class. |
| 5695 | if (!Caller->isImplicit() && CheckCUDATarget(Caller, Function)) { |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5696 | Candidate.Viable = false; |
| 5697 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5698 | return; |
| 5699 | } |
| 5700 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5701 | // Determine the implicit conversion sequences for each of the |
| 5702 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5703 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5704 | if (ArgIdx < NumParams) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5705 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5706 | // exist for each argument an implicit conversion sequence |
| 5707 | // (13.3.3.1) that converts that argument to the corresponding |
| 5708 | // parameter of F. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5709 | QualType ParamType = Proto->getParamType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5710 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5711 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5712 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5713 | /*InOverloadResolution=*/true, |
| 5714 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5715 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5716 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5717 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5718 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5719 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5720 | return; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5721 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5722 | } else { |
| 5723 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5724 | // argument for which there is no corresponding parameter is |
| 5725 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5726 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5727 | } |
| 5728 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5729 | |
| 5730 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Function, Args)) { |
| 5731 | Candidate.Viable = false; |
| 5732 | Candidate.FailureKind = ovl_fail_enable_if; |
| 5733 | Candidate.DeductionFailure.Data = FailedAttr; |
| 5734 | return; |
| 5735 | } |
| 5736 | } |
| 5737 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5738 | ObjCMethodDecl *Sema::SelectBestMethod(Selector Sel, MultiExprArg Args, |
| 5739 | bool IsInstance) { |
| 5740 | SmallVector<ObjCMethodDecl*, 4> Methods; |
| 5741 | if (!CollectMultipleMethodsInGlobalPool(Sel, Methods, IsInstance)) |
| 5742 | return nullptr; |
| 5743 | |
| 5744 | for (unsigned b = 0, e = Methods.size(); b < e; b++) { |
| 5745 | bool Match = true; |
| 5746 | ObjCMethodDecl *Method = Methods[b]; |
| 5747 | unsigned NumNamedArgs = Sel.getNumArgs(); |
| 5748 | // Method might have more arguments than selector indicates. This is due |
| 5749 | // to addition of c-style arguments in method. |
| 5750 | if (Method->param_size() > NumNamedArgs) |
| 5751 | NumNamedArgs = Method->param_size(); |
| 5752 | if (Args.size() < NumNamedArgs) |
| 5753 | continue; |
| 5754 | |
| 5755 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
| 5756 | // We can't do any type-checking on a type-dependent argument. |
| 5757 | if (Args[i]->isTypeDependent()) { |
| 5758 | Match = false; |
| 5759 | break; |
| 5760 | } |
| 5761 | |
| 5762 | ParmVarDecl *param = Method->parameters()[i]; |
| 5763 | Expr *argExpr = Args[i]; |
| 5764 | assert(argExpr && "SelectBestMethod(): missing expression"); |
| 5765 | |
| 5766 | // Strip the unbridged-cast placeholder expression off unless it's |
| 5767 | // a consumed argument. |
| 5768 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
| 5769 | !param->hasAttr<CFConsumedAttr>()) |
| 5770 | argExpr = stripARCUnbridgedCast(argExpr); |
| 5771 | |
| 5772 | // If the parameter is __unknown_anytype, move on to the next method. |
| 5773 | if (param->getType() == Context.UnknownAnyTy) { |
| 5774 | Match = false; |
| 5775 | break; |
| 5776 | } |
| 5777 | |
| 5778 | ImplicitConversionSequence ConversionState |
| 5779 | = TryCopyInitialization(*this, argExpr, param->getType(), |
| 5780 | /*SuppressUserConversions*/false, |
| 5781 | /*InOverloadResolution=*/true, |
| 5782 | /*AllowObjCWritebackConversion=*/ |
| 5783 | getLangOpts().ObjCAutoRefCount, |
| 5784 | /*AllowExplicit*/false); |
| 5785 | if (ConversionState.isBad()) { |
| 5786 | Match = false; |
| 5787 | break; |
| 5788 | } |
| 5789 | } |
| 5790 | // Promote additional arguments to variadic methods. |
| 5791 | if (Match && Method->isVariadic()) { |
| 5792 | for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) { |
| 5793 | if (Args[i]->isTypeDependent()) { |
| 5794 | Match = false; |
| 5795 | break; |
| 5796 | } |
| 5797 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
| 5798 | nullptr); |
| 5799 | if (Arg.isInvalid()) { |
| 5800 | Match = false; |
| 5801 | break; |
| 5802 | } |
| 5803 | } |
| 5804 | } else { |
| 5805 | // Check for extra arguments to non-variadic methods. |
| 5806 | if (Args.size() != NumNamedArgs) |
| 5807 | Match = false; |
| 5808 | else if (Match && NumNamedArgs == 0 && Methods.size() > 1) { |
| 5809 | // Special case when selectors have no argument. In this case, select |
| 5810 | // one with the most general result type of 'id'. |
| 5811 | for (unsigned b = 0, e = Methods.size(); b < e; b++) { |
| 5812 | QualType ReturnT = Methods[b]->getReturnType(); |
| 5813 | if (ReturnT->isObjCIdType()) |
| 5814 | return Methods[b]; |
| 5815 | } |
| 5816 | } |
| 5817 | } |
| 5818 | |
| 5819 | if (Match) |
| 5820 | return Method; |
| 5821 | } |
| 5822 | return nullptr; |
| 5823 | } |
| 5824 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5825 | static bool IsNotEnableIfAttr(Attr *A) { return !isa<EnableIfAttr>(A); } |
| 5826 | |
| 5827 | EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, |
| 5828 | bool MissingImplicitThis) { |
| 5829 | // FIXME: specific_attr_iterator<EnableIfAttr> iterates in reverse order, but |
| 5830 | // we need to find the first failing one. |
| 5831 | if (!Function->hasAttrs()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5832 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5833 | AttrVec Attrs = Function->getAttrs(); |
| 5834 | AttrVec::iterator E = std::remove_if(Attrs.begin(), Attrs.end(), |
| 5835 | IsNotEnableIfAttr); |
| 5836 | if (Attrs.begin() == E) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5837 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5838 | std::reverse(Attrs.begin(), E); |
| 5839 | |
| 5840 | SFINAETrap Trap(*this); |
| 5841 | |
| 5842 | // Convert the arguments. |
| 5843 | SmallVector<Expr *, 16> ConvertedArgs; |
| 5844 | bool InitializationFailed = false; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5845 | bool ContainsValueDependentExpr = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5846 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 5847 | if (i == 0 && !MissingImplicitThis && isa<CXXMethodDecl>(Function) && |
| 5848 | !cast<CXXMethodDecl>(Function)->isStatic() && |
| 5849 | !isa<CXXConstructorDecl>(Function)) { |
| 5850 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Function); |
| 5851 | ExprResult R = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5852 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/nullptr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5853 | Method, Method); |
| 5854 | if (R.isInvalid()) { |
| 5855 | InitializationFailed = true; |
| 5856 | break; |
| 5857 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5858 | ContainsValueDependentExpr |= R.get()->isValueDependent(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5859 | ConvertedArgs.push_back(R.get()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5860 | } else { |
| 5861 | ExprResult R = |
| 5862 | PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 5863 | Context, |
| 5864 | Function->getParamDecl(i)), |
| 5865 | SourceLocation(), |
| 5866 | Args[i]); |
| 5867 | if (R.isInvalid()) { |
| 5868 | InitializationFailed = true; |
| 5869 | break; |
| 5870 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5871 | ContainsValueDependentExpr |= R.get()->isValueDependent(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5872 | ConvertedArgs.push_back(R.get()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | if (InitializationFailed || Trap.hasErrorOccurred()) |
| 5877 | return cast<EnableIfAttr>(Attrs[0]); |
| 5878 | |
| 5879 | for (AttrVec::iterator I = Attrs.begin(); I != E; ++I) { |
| 5880 | APValue Result; |
| 5881 | EnableIfAttr *EIA = cast<EnableIfAttr>(*I); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5882 | if (EIA->getCond()->isValueDependent()) { |
| 5883 | // Don't even try now, we'll examine it after instantiation. |
| 5884 | continue; |
| 5885 | } |
| 5886 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5887 | if (!EIA->getCond()->EvaluateWithSubstitution( |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5888 | Result, Context, Function, llvm::makeArrayRef(ConvertedArgs))) { |
| 5889 | if (!ContainsValueDependentExpr) |
| 5890 | return EIA; |
| 5891 | } else if (!Result.isInt() || !Result.getInt().getBoolValue()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5892 | return EIA; |
| 5893 | } |
| 5894 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5895 | return nullptr; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5898 | /// \brief Add all of the function declarations in the given function set to |
Nick Lewycky | 199e370 | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 5899 | /// the overload candidate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5900 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5901 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5902 | OverloadCandidateSet& CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5903 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5904 | bool SuppressUserConversions, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5905 | bool PartialOverloading) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5906 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5907 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5908 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5909 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5910 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5911 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5912 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5913 | Args.slice(1), CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5914 | SuppressUserConversions, PartialOverloading); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5915 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5916 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5917 | SuppressUserConversions, PartialOverloading); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5918 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5919 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5920 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5921 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5922 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5923 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5924 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5925 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5926 | Args[0]->Classify(Context), Args.slice(1), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5927 | CandidateSet, SuppressUserConversions, |
| 5928 | PartialOverloading); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5929 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5930 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5931 | ExplicitTemplateArgs, Args, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5932 | CandidateSet, SuppressUserConversions, |
| 5933 | PartialOverloading); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5934 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5935 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5936 | } |
| 5937 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5938 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5939 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5940 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5941 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5942 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5943 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5944 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5945 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5946 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5947 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5948 | |
| 5949 | if (isa<UsingShadowDecl>(Decl)) |
| 5950 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5951 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5952 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5953 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5954 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5955 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5956 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5957 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5958 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5959 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5960 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5961 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5962 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5963 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5964 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5965 | } |
| 5966 | } |
| 5967 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5968 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5969 | /// of candidate functions, using the given function call arguments |
| 5970 | /// and the object argument (@c Object). For example, in a call |
| 5971 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5972 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5973 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5974 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5975 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5976 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5977 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5978 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5979 | ArrayRef<Expr *> Args, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5980 | OverloadCandidateSet &CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5981 | bool SuppressUserConversions, |
| 5982 | bool PartialOverloading) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5983 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5984 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5985 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5986 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5987 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5988 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5989 | if (!CandidateSet.isNewCandidate(Method)) |
| 5990 | return; |
| 5991 | |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5992 | // C++11 [class.copy]p23: [DR1402] |
| 5993 | // A defaulted move assignment operator that is defined as deleted is |
| 5994 | // ignored by overload resolution. |
| 5995 | if (Method->isDefaulted() && Method->isDeleted() && |
| 5996 | Method->isMoveAssignmentOperator()) |
| 5997 | return; |
| 5998 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5999 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6000 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6001 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6002 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6003 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6004 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6005 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6006 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6007 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6008 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6009 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6010 | unsigned NumParams = Proto->getNumParams(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6011 | |
| 6012 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6013 | // parameters is viable only if it has an ellipsis in its parameter |
| 6014 | // list (8.3.5). |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6015 | if (TooManyArguments(NumParams, Args.size(), PartialOverloading) && |
| 6016 | !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6017 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6018 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6019 | return; |
| 6020 | } |
| 6021 | |
| 6022 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 6023 | // is viable only if the (m+1)st parameter has a default argument |
| 6024 | // (8.3.6). For the purposes of overload resolution, the |
| 6025 | // parameter list is truncated on the right, so that there are |
| 6026 | // exactly m parameters. |
| 6027 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6028 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6029 | // Not enough arguments. |
| 6030 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6031 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6032 | return; |
| 6033 | } |
| 6034 | |
| 6035 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6036 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6037 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6038 | // The implicit object argument is ignored. |
| 6039 | Candidate.IgnoreObjectArgument = true; |
| 6040 | else { |
| 6041 | // Determine the implicit conversion sequence for the object |
| 6042 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6043 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6044 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 6045 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6046 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6047 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6048 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6049 | return; |
| 6050 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6053 | // (CUDA B.1): Check for invalid calls between targets. |
| 6054 | if (getLangOpts().CUDA) |
| 6055 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 6056 | if (CheckCUDATarget(Caller, Method)) { |
| 6057 | Candidate.Viable = false; |
| 6058 | Candidate.FailureKind = ovl_fail_bad_target; |
| 6059 | return; |
| 6060 | } |
| 6061 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6062 | // Determine the implicit conversion sequences for each of the |
| 6063 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6064 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6065 | if (ArgIdx < NumParams) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6066 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6067 | // exist for each argument an implicit conversion sequence |
| 6068 | // (13.3.3.1) that converts that argument to the corresponding |
| 6069 | // parameter of F. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6070 | QualType ParamType = Proto->getParamType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6071 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6072 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6073 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6074 | /*InOverloadResolution=*/true, |
| 6075 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6076 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6077 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6078 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6079 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6080 | return; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6081 | } |
| 6082 | } else { |
| 6083 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6084 | // argument for which there is no corresponding parameter is |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6085 | // considered to "match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6086 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6087 | } |
| 6088 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6089 | |
| 6090 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Method, Args, true)) { |
| 6091 | Candidate.Viable = false; |
| 6092 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6093 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6094 | return; |
| 6095 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6096 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6098 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 6099 | /// set, using template argument deduction to produce an appropriate member |
| 6100 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6101 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6102 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6103 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6104 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6105 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6106 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6107 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6108 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6109 | OverloadCandidateSet& CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6110 | bool SuppressUserConversions, |
| 6111 | bool PartialOverloading) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6112 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 6113 | return; |
| 6114 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6115 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6116 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6117 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6118 | // 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] | 6119 | // candidate functions in the usual way.113) A given name can refer to one |
| 6120 | // or more function templates and also to a set of overloaded non-template |
| 6121 | // functions. In such a case, the candidate functions generated from each |
| 6122 | // function template are combined with the set of non-template candidate |
| 6123 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6124 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6125 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6126 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6127 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6128 | Specialization, Info, PartialOverloading)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6129 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6130 | Candidate.FoundDecl = FoundDecl; |
| 6131 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 6132 | Candidate.Viable = false; |
| 6133 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6134 | Candidate.IsSurrogate = false; |
| 6135 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6136 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6137 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6138 | Info); |
| 6139 | return; |
| 6140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6142 | // Add the function template specialization produced by template argument |
| 6143 | // deduction as a candidate. |
| 6144 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6145 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6146 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6147 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6148 | ActingContext, ObjectType, ObjectClassification, Args, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6149 | CandidateSet, SuppressUserConversions, PartialOverloading); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6150 | } |
| 6151 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6152 | /// \brief Add a C++ function template specialization as a candidate |
| 6153 | /// in the candidate set, using template argument deduction to produce |
| 6154 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6155 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6156 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6157 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6158 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6159 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6160 | OverloadCandidateSet& CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6161 | bool SuppressUserConversions, |
| 6162 | bool PartialOverloading) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6163 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 6164 | return; |
| 6165 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6166 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6167 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6168 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6169 | // 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] | 6170 | // candidate functions in the usual way.113) A given name can refer to one |
| 6171 | // or more function templates and also to a set of overloaded non-template |
| 6172 | // functions. In such a case, the candidate functions generated from each |
| 6173 | // function template are combined with the set of non-template candidate |
| 6174 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6175 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6176 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6177 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6178 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6179 | Specialization, Info, PartialOverloading)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6180 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6181 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6182 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 6183 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6184 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6185 | Candidate.IsSurrogate = false; |
| 6186 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6187 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6189 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6190 | return; |
| 6191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6193 | // Add the function template specialization produced by template argument |
| 6194 | // deduction as a candidate. |
| 6195 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6196 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6197 | SuppressUserConversions, PartialOverloading); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6198 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6199 | |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6200 | /// Determine whether this is an allowable conversion from the result |
| 6201 | /// of an explicit conversion operator to the expected type, per C++ |
| 6202 | /// [over.match.conv]p1 and [over.match.ref]p1. |
| 6203 | /// |
| 6204 | /// \param ConvType The return type of the conversion function. |
| 6205 | /// |
| 6206 | /// \param ToType The type we are converting to. |
| 6207 | /// |
| 6208 | /// \param AllowObjCPointerConversion Allow a conversion from one |
| 6209 | /// Objective-C pointer to another. |
| 6210 | /// |
| 6211 | /// \returns true if the conversion is allowable, false otherwise. |
| 6212 | static bool isAllowableExplicitConversion(Sema &S, |
| 6213 | QualType ConvType, QualType ToType, |
| 6214 | bool AllowObjCPointerConversion) { |
| 6215 | QualType ToNonRefType = ToType.getNonReferenceType(); |
| 6216 | |
| 6217 | // Easy case: the types are the same. |
| 6218 | if (S.Context.hasSameUnqualifiedType(ConvType, ToNonRefType)) |
| 6219 | return true; |
| 6220 | |
| 6221 | // Allow qualification conversions. |
| 6222 | bool ObjCLifetimeConversion; |
| 6223 | if (S.IsQualificationConversion(ConvType, ToNonRefType, /*CStyle*/false, |
| 6224 | ObjCLifetimeConversion)) |
| 6225 | return true; |
| 6226 | |
| 6227 | // If we're not allowed to consider Objective-C pointer conversions, |
| 6228 | // we're done. |
| 6229 | if (!AllowObjCPointerConversion) |
| 6230 | return false; |
| 6231 | |
| 6232 | // Is this an Objective-C pointer conversion? |
| 6233 | bool IncompatibleObjC = false; |
| 6234 | QualType ConvertedType; |
| 6235 | return S.isObjCPointerConversion(ConvType, ToNonRefType, ConvertedType, |
| 6236 | IncompatibleObjC); |
| 6237 | } |
| 6238 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6239 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6240 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6241 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6242 | /// 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] | 6243 | /// (which may or may not be the same type as the type that the |
| 6244 | /// conversion function produces). |
| 6245 | void |
| 6246 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6247 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6248 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6249 | Expr *From, QualType ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6250 | OverloadCandidateSet& CandidateSet, |
| 6251 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6252 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 6253 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6254 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6255 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6256 | return; |
| 6257 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6258 | // If the conversion function has an undeduced return type, trigger its |
| 6259 | // deduction now. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6260 | if (getLangOpts().CPlusPlus14 && ConvType->isUndeducedType()) { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6261 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 6262 | return; |
| 6263 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 6264 | } |
| 6265 | |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 6266 | // Per C++ [over.match.conv]p1, [over.match.ref]p1, an explicit conversion |
| 6267 | // operator is only a candidate if its return type is the target type or |
| 6268 | // can be converted to the target type with a qualification conversion. |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6269 | if (Conversion->isExplicit() && |
| 6270 | !isAllowableExplicitConversion(*this, ConvType, ToType, |
| 6271 | AllowObjCConversionOnExplicit)) |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 6272 | return; |
| 6273 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6274 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6275 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6277 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6278 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6279 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6280 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6281 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6282 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6283 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6284 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 6285 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6286 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6287 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6288 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 6289 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6290 | // For conversion functions, the function is considered to be a member of |
| 6291 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 6292 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6293 | // |
| 6294 | // Determine the implicit conversion sequence for the implicit |
| 6295 | // object parameter. |
| 6296 | QualType ImplicitParamType = From->getType(); |
| 6297 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 6298 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 6299 | CXXRecordDecl *ConversionContext |
| 6300 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6301 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6302 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6303 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 6304 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6305 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6306 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6307 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6308 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6309 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6310 | return; |
| 6311 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6312 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6313 | // We won't go through a user-defined type conversion function to convert a |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 6314 | // derived to base as such conversions are given Conversion Rank. They only |
| 6315 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 6316 | QualType FromCanon |
| 6317 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 6318 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 6319 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 6320 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6321 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 6322 | return; |
| 6323 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6325 | // To determine what the conversion from the result of calling the |
| 6326 | // conversion function to the type we're eventually trying to |
| 6327 | // convert to (ToType), we need to synthesize a call to the |
| 6328 | // conversion function and attempt copy initialization from it. This |
| 6329 | // makes sure that we get the right semantics with respect to |
| 6330 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 6331 | // call on the stack and we don't need its arguments to be |
| 6332 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 6333 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6334 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6335 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 6336 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6337 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6338 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6339 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6340 | QualType ConversionType = Conversion->getConversionType(); |
| 6341 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 6342 | Candidate.Viable = false; |
| 6343 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 6344 | return; |
| 6345 | } |
| 6346 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6347 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6348 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6349 | // 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] | 6350 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 6351 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6352 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6353 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 6354 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6355 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6356 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6357 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6358 | /*InOverloadResolution=*/false, |
| 6359 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6360 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6361 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6362 | case ImplicitConversionSequence::StandardConversion: |
| 6363 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6364 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6365 | // C++ [over.ics.user]p3: |
| 6366 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6367 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6368 | // shall have exact match rank. |
| 6369 | if (Conversion->getPrimaryTemplate() && |
| 6370 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 6371 | Candidate.Viable = false; |
| 6372 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6373 | return; |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6374 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6375 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6376 | // C++0x [dcl.init.ref]p5: |
| 6377 | // In the second case, if the reference is an rvalue reference and |
| 6378 | // the second standard conversion sequence of the user-defined |
| 6379 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 6380 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6381 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6382 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 6383 | Candidate.Viable = false; |
| 6384 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6385 | return; |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6386 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6387 | break; |
| 6388 | |
| 6389 | case ImplicitConversionSequence::BadConversion: |
| 6390 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6391 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6392 | return; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6393 | |
| 6394 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6395 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6396 | "Can only end up with a standard conversion sequence or failure"); |
| 6397 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6398 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6399 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, None)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6400 | Candidate.Viable = false; |
| 6401 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6402 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6403 | return; |
| 6404 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6407 | /// \brief Adds a conversion function template specialization |
| 6408 | /// candidate to the overload set, using template argument deduction |
| 6409 | /// to deduce the template arguments of the conversion function |
| 6410 | /// template from the type that we are converting to (C++ |
| 6411 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6412 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6413 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6414 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6415 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6416 | Expr *From, QualType ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6417 | OverloadCandidateSet &CandidateSet, |
| 6418 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6419 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 6420 | "Only conversion function templates permitted here"); |
| 6421 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6422 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 6423 | return; |
| 6424 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6425 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6426 | CXXConversionDecl *Specialization = nullptr; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6427 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6428 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6429 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6430 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6431 | Candidate.FoundDecl = FoundDecl; |
| 6432 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 6433 | Candidate.Viable = false; |
| 6434 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6435 | Candidate.IsSurrogate = false; |
| 6436 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6437 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6438 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6439 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6440 | return; |
| 6441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6442 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6443 | // Add the conversion function template specialization produced by |
| 6444 | // template argument deduction as a candidate. |
| 6445 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6446 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6447 | CandidateSet, AllowObjCConversionOnExplicit); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6448 | } |
| 6449 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6450 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6451 | /// converts the given @c Object to a function pointer via the |
| 6452 | /// conversion function @c Conversion, and then attempts to call it |
| 6453 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6454 | /// the type of function that we'll eventually be calling. |
| 6455 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6456 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6457 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6458 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6459 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6460 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6461 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6462 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6463 | return; |
| 6464 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6465 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6466 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6467 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6468 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6469 | Candidate.FoundDecl = FoundDecl; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6470 | Candidate.Function = nullptr; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6471 | Candidate.Surrogate = Conversion; |
| 6472 | Candidate.Viable = true; |
| 6473 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6474 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6475 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6476 | |
| 6477 | // Determine the implicit conversion sequence for the implicit |
| 6478 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6479 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6480 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6481 | Object->Classify(Context), |
| 6482 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6483 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6484 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6485 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6486 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6487 | return; |
| 6488 | } |
| 6489 | |
| 6490 | // The first conversion is actually a user-defined conversion whose |
| 6491 | // first conversion is ObjectInit's standard conversion (which is |
| 6492 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6493 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6494 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6495 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6496 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6497 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6498 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6500 | = Candidate.Conversions[0].UserDefined.Before; |
| 6501 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6502 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6503 | // Find the |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6504 | unsigned NumParams = Proto->getNumParams(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6505 | |
| 6506 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6507 | // parameters is viable only if it has an ellipsis in its parameter |
| 6508 | // list (8.3.5). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6509 | if (Args.size() > NumParams && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6510 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6511 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6512 | return; |
| 6513 | } |
| 6514 | |
| 6515 | // Function types don't have any default arguments, so just check if |
| 6516 | // we have enough arguments. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6517 | if (Args.size() < NumParams) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6518 | // Not enough arguments. |
| 6519 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6520 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6521 | return; |
| 6522 | } |
| 6523 | |
| 6524 | // Determine the implicit conversion sequences for each of the |
| 6525 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6526 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6527 | if (ArgIdx < NumParams) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6528 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6529 | // exist for each argument an implicit conversion sequence |
| 6530 | // (13.3.3.1) that converts that argument to the corresponding |
| 6531 | // parameter of F. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6532 | QualType ParamType = Proto->getParamType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6533 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6534 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6535 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6536 | /*InOverloadResolution=*/false, |
| 6537 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6538 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6539 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6540 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6541 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6542 | return; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6543 | } |
| 6544 | } else { |
| 6545 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6546 | // argument for which there is no corresponding parameter is |
| 6547 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6548 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6549 | } |
| 6550 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6551 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6552 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, None)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6553 | Candidate.Viable = false; |
| 6554 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6555 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6556 | return; |
| 6557 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6558 | } |
| 6559 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6560 | /// \brief Add overload candidates for overloaded operators that are |
| 6561 | /// member functions. |
| 6562 | /// |
| 6563 | /// Add the overloaded operator candidates that are member functions |
| 6564 | /// for the operator Op that was used in an operator expression such |
| 6565 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6566 | /// CandidateSet will store the added overload candidates. (C++ |
| 6567 | /// [over.match.oper]). |
| 6568 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6569 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6570 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6571 | OverloadCandidateSet& CandidateSet, |
| 6572 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6573 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6574 | |
| 6575 | // C++ [over.match.oper]p3: |
| 6576 | // For a unary operator @ with an operand of a type whose |
| 6577 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6578 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6579 | // a right operand of a type whose cv-unqualified version is T2, |
| 6580 | // three sets of candidate functions, designated member |
| 6581 | // candidates, non-member candidates and built-in candidates, are |
| 6582 | // constructed as follows: |
| 6583 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6584 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6585 | // -- If T1 is a complete class type or a class currently being |
| 6586 | // defined, the set of member candidates is the result of the |
| 6587 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6588 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6589 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6590 | // Complete the type if it can be completed. |
| 6591 | RequireCompleteType(OpLoc, T1, 0); |
| 6592 | // If the type is neither complete nor being defined, bail out now. |
| 6593 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6594 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6595 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6596 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6597 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6598 | Operators.suppressDiagnostics(); |
| 6599 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6600 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6601 | OperEnd = Operators.end(); |
| 6602 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6603 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6604 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6605 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6606 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6607 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6608 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6609 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6610 | } |
| 6611 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6612 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6613 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6614 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6615 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6616 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6617 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6618 | /// (at the beginning of the argument list) that will be contextually |
| 6619 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6620 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6621 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6622 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6623 | bool IsAssignmentOperator, |
| 6624 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6625 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6626 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6627 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6628 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6629 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6630 | Candidate.FoundDecl = DeclAccessPair::make(nullptr, AS_none); |
| 6631 | Candidate.Function = nullptr; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6632 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6633 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6634 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6635 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6636 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6637 | |
| 6638 | // Determine the implicit conversion sequences for each of the |
| 6639 | // arguments. |
| 6640 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6641 | Candidate.ExplicitCallArguments = Args.size(); |
| 6642 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6643 | // C++ [over.match.oper]p4: |
| 6644 | // For the built-in assignment operators, conversions of the |
| 6645 | // left operand are restricted as follows: |
| 6646 | // -- no temporaries are introduced to hold the left operand, and |
| 6647 | // -- no user-defined conversions are applied to the left |
| 6648 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6649 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6650 | // |
| 6651 | // We block these conversions by turning off user-defined |
| 6652 | // conversions, since that is the only way that initialization of |
| 6653 | // a reference to a non-class type can occur from something that |
| 6654 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6655 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6656 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6657 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6658 | Candidate.Conversions[ArgIdx] |
| 6659 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6660 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6661 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6662 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6663 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6664 | /*InOverloadResolution=*/false, |
| 6665 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6666 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6667 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6668 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6669 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6670 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6671 | break; |
| 6672 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6673 | } |
| 6674 | } |
| 6675 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6676 | namespace { |
| 6677 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6678 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6679 | /// candidate operator functions for built-in operators (C++ |
| 6680 | /// [over.built]). The types are separated into pointer types and |
| 6681 | /// enumeration types. |
| 6682 | class BuiltinCandidateTypeSet { |
| 6683 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6684 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6685 | |
| 6686 | /// PointerTypes - The set of pointer types that will be used in the |
| 6687 | /// built-in candidates. |
| 6688 | TypeSet PointerTypes; |
| 6689 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6690 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6691 | /// used in the built-in candidates. |
| 6692 | TypeSet MemberPointerTypes; |
| 6693 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6694 | /// EnumerationTypes - The set of enumeration types that will be |
| 6695 | /// used in the built-in candidates. |
| 6696 | TypeSet EnumerationTypes; |
| 6697 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6698 | /// \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] | 6699 | /// candidates. |
| 6700 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6701 | |
| 6702 | /// \brief A flag indicating non-record types are viable candidates |
| 6703 | bool HasNonRecordTypes; |
| 6704 | |
| 6705 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6706 | /// were present in the candidate set. |
| 6707 | bool HasArithmeticOrEnumeralTypes; |
| 6708 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6709 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6710 | /// candidate set. |
| 6711 | bool HasNullPtrType; |
| 6712 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6713 | /// Sema - The semantic analysis instance where we are building the |
| 6714 | /// candidate type set. |
| 6715 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6716 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6717 | /// Context - The AST context in which we will build the type sets. |
| 6718 | ASTContext &Context; |
| 6719 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6720 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6721 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6722 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6723 | |
| 6724 | public: |
| 6725 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6726 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6727 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6728 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6729 | : HasNonRecordTypes(false), |
| 6730 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6731 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6732 | SemaRef(SemaRef), |
| 6733 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6734 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6735 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6736 | SourceLocation Loc, |
| 6737 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6738 | bool AllowExplicitConversions, |
| 6739 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6740 | |
| 6741 | /// pointer_begin - First pointer type found; |
| 6742 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6743 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6744 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6745 | iterator pointer_end() { return PointerTypes.end(); } |
| 6746 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6747 | /// member_pointer_begin - First member pointer type found; |
| 6748 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6749 | |
| 6750 | /// member_pointer_end - Past the last member pointer type found; |
| 6751 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6752 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6753 | /// enumeration_begin - First enumeration type found; |
| 6754 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6755 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6756 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6757 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6758 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6759 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6760 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6761 | |
| 6762 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6763 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6764 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6765 | }; |
| 6766 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6767 | } // end anonymous namespace |
| 6768 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6769 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6770 | /// the set of pointer types along with any more-qualified variants of |
| 6771 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6772 | /// will add "int const *", "int const volatile *", "int const |
| 6773 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6774 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6775 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6776 | /// |
| 6777 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6778 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6779 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6780 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6781 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6782 | // Insert this type. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6783 | if (!PointerTypes.insert(Ty).second) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6784 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6785 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6786 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6787 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6788 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6789 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6790 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6791 | PointeeTy = PTy->getPointeeType(); |
| 6792 | buildObjCPtr = true; |
| 6793 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6794 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6797 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6798 | // (the qualifier would sink to the element type), and for another, the |
| 6799 | // only overload situation where it matters is subscript or pointer +- int, |
| 6800 | // and those shouldn't have qualifier variants anyway. |
| 6801 | if (PointeeTy->isArrayType()) |
| 6802 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6803 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6804 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6805 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6806 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6807 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6808 | // Iterate through all strict supersets of BaseCVR. |
| 6809 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6810 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6811 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6812 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6813 | |
| 6814 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6815 | // the type cannot be restrict-qualified. |
| 6816 | if ((CVR & Qualifiers::Restrict) && |
| 6817 | (!hasRestrict || |
| 6818 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6819 | continue; |
| 6820 | |
| 6821 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6822 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6823 | |
| 6824 | // Build qualified pointer type. |
| 6825 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6826 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6827 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6828 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6829 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6830 | |
| 6831 | // Insert qualified pointer type. |
| 6832 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | return true; |
| 6836 | } |
| 6837 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6838 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6839 | /// to the set of pointer types along with any more-qualified variants of |
| 6840 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6841 | /// will add "int const *", "int const volatile *", "int const |
| 6842 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6843 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6844 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6845 | /// |
| 6846 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6847 | bool |
| 6848 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6849 | QualType Ty) { |
| 6850 | // Insert this type. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6851 | if (!MemberPointerTypes.insert(Ty).second) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6852 | return false; |
| 6853 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6854 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6855 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6856 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6857 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6858 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6859 | // (the qualifier would sink to the element type), and for another, the |
| 6860 | // only overload situation where it matters is subscript or pointer +- int, |
| 6861 | // and those shouldn't have qualifier variants anyway. |
| 6862 | if (PointeeTy->isArrayType()) |
| 6863 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6864 | const Type *ClassTy = PointerTy->getClass(); |
| 6865 | |
| 6866 | // Iterate through all strict supersets of the pointee type's CVR |
| 6867 | // qualifiers. |
| 6868 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6869 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6870 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6871 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6872 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6873 | MemberPointerTypes.insert( |
| 6874 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6875 | } |
| 6876 | |
| 6877 | return true; |
| 6878 | } |
| 6879 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6880 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6881 | /// 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] | 6882 | /// primarily interested in pointer types and enumeration types. We also |
| 6883 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6884 | /// AllowUserConversions is true if we should look at the conversion |
| 6885 | /// functions of a class type, and AllowExplicitConversions if we |
| 6886 | /// should also include the explicit conversion functions of a class |
| 6887 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6888 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6889 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6890 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6891 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6892 | bool AllowExplicitConversions, |
| 6893 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6894 | // Only deal with canonical types. |
| 6895 | Ty = Context.getCanonicalType(Ty); |
| 6896 | |
| 6897 | // Look through reference types; they aren't part of the type of an |
| 6898 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6899 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6900 | Ty = RefTy->getPointeeType(); |
| 6901 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6902 | // If we're dealing with an array type, decay to the pointer. |
| 6903 | if (Ty->isArrayType()) |
| 6904 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6905 | |
| 6906 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6907 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6908 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6909 | // Flag if we ever add a non-record type. |
| 6910 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6911 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6912 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6913 | // Flag if we encounter an arithmetic type. |
| 6914 | HasArithmeticOrEnumeralTypes = |
| 6915 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6916 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6917 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6918 | PointerTypes.insert(Ty); |
| 6919 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6920 | // Insert our type, and its more-qualified variants, into the set |
| 6921 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6922 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6923 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6924 | } else if (Ty->isMemberPointerType()) { |
| 6925 | // Member pointers are far easier, since the pointee can't be converted. |
| 6926 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6927 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6928 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6929 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6930 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6931 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6932 | // We treat vector types as arithmetic types in many contexts as an |
| 6933 | // extension. |
| 6934 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6935 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6936 | } else if (Ty->isNullPtrType()) { |
| 6937 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6938 | } else if (AllowUserConversions && TyRec) { |
| 6939 | // No conversion functions in incomplete types. |
| 6940 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6941 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6942 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6943 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6944 | for (NamedDecl *D : ClassDecl->getVisibleConversionFunctions()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6945 | if (isa<UsingShadowDecl>(D)) |
| 6946 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6947 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6948 | // Skip conversion function templates; they don't tell us anything |
| 6949 | // about which builtin types we can convert to. |
| 6950 | if (isa<FunctionTemplateDecl>(D)) |
| 6951 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6952 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6953 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6954 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6955 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6956 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6957 | } |
| 6958 | } |
| 6959 | } |
| 6960 | } |
| 6961 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6962 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6963 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6964 | /// given type to the candidate set. |
| 6965 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6966 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6967 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6968 | OverloadCandidateSet &CandidateSet) { |
| 6969 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6970 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6971 | // T& operator=(T&, T) |
| 6972 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6973 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6974 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6975 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6976 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6977 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6978 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6979 | ParamTypes[0] |
| 6980 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6981 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6982 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6983 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6984 | } |
| 6985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6986 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6987 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6988 | /// 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] | 6989 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6990 | Qualifiers VRQuals; |
| 6991 | const RecordType *TyRec; |
| 6992 | if (const MemberPointerType *RHSMPType = |
| 6993 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6994 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6995 | else |
| 6996 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6997 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6998 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6999 | VRQuals.addVolatile(); |
| 7000 | VRQuals.addRestrict(); |
| 7001 | return VRQuals; |
| 7002 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7003 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7004 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 7005 | if (!ClassDecl->hasDefinition()) |
| 7006 | return VRQuals; |
| 7007 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7008 | for (NamedDecl *D : ClassDecl->getVisibleConversionFunctions()) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7009 | if (isa<UsingShadowDecl>(D)) |
| 7010 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7011 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7012 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 7013 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 7014 | CanTy = ResTypeRef->getPointeeType(); |
| 7015 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 7016 | // as see them. |
| 7017 | bool done = false; |
| 7018 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7019 | if (CanTy.isRestrictQualified()) |
| 7020 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7021 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 7022 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7023 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7024 | CanTy->getAs<MemberPointerType>()) |
| 7025 | CanTy = ResTypeMPtr->getPointeeType(); |
| 7026 | else |
| 7027 | done = true; |
| 7028 | if (CanTy.isVolatileQualified()) |
| 7029 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7030 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 7031 | return VRQuals; |
| 7032 | } |
| 7033 | } |
| 7034 | } |
| 7035 | return VRQuals; |
| 7036 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7037 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7038 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7039 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7040 | /// \brief Helper class to manage the addition of builtin operator overload |
| 7041 | /// candidates. It provides shared state and utility methods used throughout |
| 7042 | /// the process, as well as a helper method to add each group of builtin |
| 7043 | /// operator overloads from the standard to a candidate set. |
| 7044 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7045 | // Common instance state available to all overload candidate addition methods. |
| 7046 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7047 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7048 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7049 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7050 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7051 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7052 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7053 | // Define some constants used to index and iterate over the arithemetic types |
| 7054 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7055 | // The "promoted arithmetic types" are the arithmetic |
| 7056 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7057 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7058 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7059 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7060 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7061 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7062 | LastPromotedArithmeticType = 11; |
| 7063 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7064 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7065 | /// \brief Get the canonical type for a given arithmetic type index. |
| 7066 | CanQualType getArithmeticType(unsigned index) { |
| 7067 | assert(index < NumArithmeticTypes); |
| 7068 | static CanQualType ASTContext::* const |
| 7069 | ArithmeticTypes[NumArithmeticTypes] = { |
| 7070 | // Start of promoted types. |
| 7071 | &ASTContext::FloatTy, |
| 7072 | &ASTContext::DoubleTy, |
| 7073 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7074 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7075 | // Start of integral types. |
| 7076 | &ASTContext::IntTy, |
| 7077 | &ASTContext::LongTy, |
| 7078 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7079 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7080 | &ASTContext::UnsignedIntTy, |
| 7081 | &ASTContext::UnsignedLongTy, |
| 7082 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7083 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7084 | // End of promoted types. |
| 7085 | |
| 7086 | &ASTContext::BoolTy, |
| 7087 | &ASTContext::CharTy, |
| 7088 | &ASTContext::WCharTy, |
| 7089 | &ASTContext::Char16Ty, |
| 7090 | &ASTContext::Char32Ty, |
| 7091 | &ASTContext::SignedCharTy, |
| 7092 | &ASTContext::ShortTy, |
| 7093 | &ASTContext::UnsignedCharTy, |
| 7094 | &ASTContext::UnsignedShortTy, |
| 7095 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7096 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7097 | }; |
| 7098 | return S.Context.*ArithmeticTypes[index]; |
| 7099 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7100 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7101 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 7102 | /// converions for the given arithmetic types. |
| 7103 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 7104 | // Accelerator table for performing the usual arithmetic conversions. |
| 7105 | // The rules are basically: |
| 7106 | // - if either is floating-point, use the wider floating-point |
| 7107 | // - if same signedness, use the higher rank |
| 7108 | // - if same size, use unsigned of the higher rank |
| 7109 | // - use the larger type |
| 7110 | // These rules, together with the axiom that higher ranks are |
| 7111 | // never smaller, are sufficient to precompute all of these results |
| 7112 | // *except* when dealing with signed types of higher rank. |
| 7113 | // (we could precompute SLL x UI for all known platforms, but it's |
| 7114 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7115 | // 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] | 7116 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7117 | Dep=-1, |
| 7118 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7119 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 7120 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7121 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7122 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 7123 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 7124 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 7125 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 7126 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 7127 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 7128 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 7129 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 7130 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 7131 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 7132 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7133 | }; |
| 7134 | |
| 7135 | assert(L < LastPromotedArithmeticType); |
| 7136 | assert(R < LastPromotedArithmeticType); |
| 7137 | int Idx = ConversionsTable[L][R]; |
| 7138 | |
| 7139 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7140 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7141 | |
| 7142 | // Slow path: we need to compare widths. |
| 7143 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7144 | CanQualType LT = getArithmeticType(L), |
| 7145 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7146 | unsigned LW = S.Context.getIntWidth(LT), |
| 7147 | RW = S.Context.getIntWidth(RT); |
| 7148 | |
| 7149 | // If they're different widths, use the signed type. |
| 7150 | if (LW > RW) return LT; |
| 7151 | else if (LW < RW) return RT; |
| 7152 | |
| 7153 | // Otherwise, use the unsigned type of the signed type's rank. |
| 7154 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 7155 | assert(L == SLL || R == SLL); |
| 7156 | return S.Context.UnsignedLongLongTy; |
| 7157 | } |
| 7158 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 7159 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 7160 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7161 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7162 | bool HasVolatile, |
| 7163 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7164 | QualType ParamTypes[2] = { |
| 7165 | S.Context.getLValueReferenceType(CandidateTy), |
| 7166 | S.Context.IntTy |
| 7167 | }; |
| 7168 | |
| 7169 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7170 | if (Args.size() == 1) |
| 7171 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7172 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7173 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7174 | |
| 7175 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 7176 | // add volatile version only if there are conversions to a volatile type. |
| 7177 | if (HasVolatile) { |
| 7178 | ParamTypes[0] = |
| 7179 | S.Context.getLValueReferenceType( |
| 7180 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7181 | if (Args.size() == 1) |
| 7182 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7183 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7184 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7185 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7186 | |
| 7187 | // Add restrict version only if there are conversions to a restrict type |
| 7188 | // and our candidate type is a non-restrict-qualified pointer. |
| 7189 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 7190 | !CandidateTy.isRestrictQualified()) { |
| 7191 | ParamTypes[0] |
| 7192 | = S.Context.getLValueReferenceType( |
| 7193 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7194 | if (Args.size() == 1) |
| 7195 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7196 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7197 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7198 | |
| 7199 | if (HasVolatile) { |
| 7200 | ParamTypes[0] |
| 7201 | = S.Context.getLValueReferenceType( |
| 7202 | S.Context.getCVRQualifiedType(CandidateTy, |
| 7203 | (Qualifiers::Volatile | |
| 7204 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7205 | if (Args.size() == 1) |
| 7206 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7207 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7208 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7209 | } |
| 7210 | } |
| 7211 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7212 | } |
| 7213 | |
| 7214 | public: |
| 7215 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7216 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7217 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7218 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7219 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7220 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7221 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7222 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7223 | HasArithmeticOrEnumeralCandidateType( |
| 7224 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7225 | CandidateTypes(CandidateTypes), |
| 7226 | CandidateSet(CandidateSet) { |
| 7227 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7228 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7229 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7230 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7231 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7232 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7233 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 7234 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7235 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7236 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7237 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7238 | "Invalid last promoted arithmetic type"); |
| 7239 | } |
| 7240 | |
| 7241 | // C++ [over.built]p3: |
| 7242 | // |
| 7243 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 7244 | // is either volatile or empty, there exist candidate operator |
| 7245 | // functions of the form |
| 7246 | // |
| 7247 | // VQ T& operator++(VQ T&); |
| 7248 | // T operator++(VQ T&, int); |
| 7249 | // |
| 7250 | // C++ [over.built]p4: |
| 7251 | // |
| 7252 | // For every pair (T, VQ), where T is an arithmetic type other |
| 7253 | // than bool, and VQ is either volatile or empty, there exist |
| 7254 | // candidate operator functions of the form |
| 7255 | // |
| 7256 | // VQ T& operator--(VQ T&); |
| 7257 | // T operator--(VQ T&, int); |
| 7258 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7259 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7260 | return; |
| 7261 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7262 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 7263 | Arith < NumArithmeticTypes; ++Arith) { |
| 7264 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7265 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7266 | VisibleTypeConversionsQuals.hasVolatile(), |
| 7267 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7268 | } |
| 7269 | } |
| 7270 | |
| 7271 | // C++ [over.built]p5: |
| 7272 | // |
| 7273 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7274 | // cv-unqualified object type, and VQ is either volatile or |
| 7275 | // empty, there exist candidate operator functions of the form |
| 7276 | // |
| 7277 | // T*VQ& operator++(T*VQ&); |
| 7278 | // T*VQ& operator--(T*VQ&); |
| 7279 | // T* operator++(T*VQ&, int); |
| 7280 | // T* operator--(T*VQ&, int); |
| 7281 | void addPlusPlusMinusMinusPointerOverloads() { |
| 7282 | for (BuiltinCandidateTypeSet::iterator |
| 7283 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7284 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7285 | Ptr != PtrEnd; ++Ptr) { |
| 7286 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7287 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7288 | continue; |
| 7289 | |
| 7290 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7291 | (!(*Ptr).isVolatileQualified() && |
| 7292 | VisibleTypeConversionsQuals.hasVolatile()), |
| 7293 | (!(*Ptr).isRestrictQualified() && |
| 7294 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | // C++ [over.built]p6: |
| 7299 | // For every cv-qualified or cv-unqualified object type T, there |
| 7300 | // exist candidate operator functions of the form |
| 7301 | // |
| 7302 | // T& operator*(T*); |
| 7303 | // |
| 7304 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7305 | // 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] | 7306 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7307 | // T& operator*(T*); |
| 7308 | void addUnaryStarPointerOverloads() { |
| 7309 | for (BuiltinCandidateTypeSet::iterator |
| 7310 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7311 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7312 | Ptr != PtrEnd; ++Ptr) { |
| 7313 | QualType ParamTy = *Ptr; |
| 7314 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7315 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 7316 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7317 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 7318 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 7319 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 7320 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7321 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7322 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7323 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7324 | } |
| 7325 | } |
| 7326 | |
| 7327 | // C++ [over.built]p9: |
| 7328 | // For every promoted arithmetic type T, there exist candidate |
| 7329 | // operator functions of the form |
| 7330 | // |
| 7331 | // T operator+(T); |
| 7332 | // T operator-(T); |
| 7333 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7334 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7335 | return; |
| 7336 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7337 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 7338 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7339 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7340 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
| 7343 | // Extension: We also add these operators for vector types. |
| 7344 | for (BuiltinCandidateTypeSet::iterator |
| 7345 | Vec = CandidateTypes[0].vector_begin(), |
| 7346 | VecEnd = CandidateTypes[0].vector_end(); |
| 7347 | Vec != VecEnd; ++Vec) { |
| 7348 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7349 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7350 | } |
| 7351 | } |
| 7352 | |
| 7353 | // C++ [over.built]p8: |
| 7354 | // For every type T, there exist candidate operator functions of |
| 7355 | // the form |
| 7356 | // |
| 7357 | // T* operator+(T*); |
| 7358 | void addUnaryPlusPointerOverloads() { |
| 7359 | for (BuiltinCandidateTypeSet::iterator |
| 7360 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7361 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7362 | Ptr != PtrEnd; ++Ptr) { |
| 7363 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7364 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | // C++ [over.built]p10: |
| 7369 | // For every promoted integral type T, there exist candidate |
| 7370 | // operator functions of the form |
| 7371 | // |
| 7372 | // T operator~(T); |
| 7373 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7374 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7375 | return; |
| 7376 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7377 | for (unsigned Int = FirstPromotedIntegralType; |
| 7378 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7379 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7380 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
| 7383 | // Extension: We also add this operator for vector types. |
| 7384 | for (BuiltinCandidateTypeSet::iterator |
| 7385 | Vec = CandidateTypes[0].vector_begin(), |
| 7386 | VecEnd = CandidateTypes[0].vector_end(); |
| 7387 | Vec != VecEnd; ++Vec) { |
| 7388 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7389 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7390 | } |
| 7391 | } |
| 7392 | |
| 7393 | // C++ [over.match.oper]p16: |
| 7394 | // For every pointer to member type T, there exist candidate operator |
| 7395 | // functions of the form |
| 7396 | // |
| 7397 | // bool operator==(T,T); |
| 7398 | // bool operator!=(T,T); |
| 7399 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 7400 | /// Set of (canonical) types that we've already handled. |
| 7401 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7402 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7403 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7404 | for (BuiltinCandidateTypeSet::iterator |
| 7405 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7406 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7407 | MemPtr != MemPtrEnd; |
| 7408 | ++MemPtr) { |
| 7409 | // Don't add the same builtin candidate twice. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7410 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7411 | continue; |
| 7412 | |
| 7413 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7414 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7415 | } |
| 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | // C++ [over.built]p15: |
| 7420 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7421 | // For every T, where T is an enumeration type, a pointer type, or |
| 7422 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7423 | // |
| 7424 | // bool operator<(T, T); |
| 7425 | // bool operator>(T, T); |
| 7426 | // bool operator<=(T, T); |
| 7427 | // bool operator>=(T, T); |
| 7428 | // bool operator==(T, T); |
| 7429 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7430 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7431 | // C++ [over.match.oper]p3: |
| 7432 | // [...]the built-in candidates include all of the candidate operator |
| 7433 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 7434 | // do not have the same parameter-type-list as any non-template non-member |
| 7435 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7436 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7437 | // Note that in practice, this only affects enumeration types because there |
| 7438 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7439 | // must have an operand of record or enumeration type. Also, the only other |
| 7440 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7441 | // cannot be overloaded for enumeration types, so this is the only place |
| 7442 | // where we must suppress candidates like this. |
| 7443 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7444 | UserDefinedBinaryOperators; |
| 7445 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7446 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7447 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7448 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7449 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7450 | CEnd = CandidateSet.end(); |
| 7451 | C != CEnd; ++C) { |
| 7452 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7453 | continue; |
| 7454 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7455 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7456 | continue; |
| 7457 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7458 | QualType FirstParamType = |
| 7459 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7460 | QualType SecondParamType = |
| 7461 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7462 | |
| 7463 | // Skip if either parameter isn't of enumeral type. |
| 7464 | if (!FirstParamType->isEnumeralType() || |
| 7465 | !SecondParamType->isEnumeralType()) |
| 7466 | continue; |
| 7467 | |
| 7468 | // Add this operator to the set of known user-defined operators. |
| 7469 | UserDefinedBinaryOperators.insert( |
| 7470 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7471 | S.Context.getCanonicalType(SecondParamType))); |
| 7472 | } |
| 7473 | } |
| 7474 | } |
| 7475 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7476 | /// Set of (canonical) types that we've already handled. |
| 7477 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7478 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7479 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7480 | for (BuiltinCandidateTypeSet::iterator |
| 7481 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7482 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7483 | Ptr != PtrEnd; ++Ptr) { |
| 7484 | // Don't add the same builtin candidate twice. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7485 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7486 | continue; |
| 7487 | |
| 7488 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7489 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7490 | } |
| 7491 | for (BuiltinCandidateTypeSet::iterator |
| 7492 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7493 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7494 | Enum != EnumEnd; ++Enum) { |
| 7495 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7496 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7497 | // Don't add the same builtin candidate twice, or if a user defined |
| 7498 | // candidate exists. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7499 | if (!AddedTypes.insert(CanonType).second || |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7500 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7501 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7502 | continue; |
| 7503 | |
| 7504 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7505 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7506 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7507 | |
| 7508 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7509 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7510 | if (AddedTypes.insert(NullPtrTy).second && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7511 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7512 | NullPtrTy))) { |
| 7513 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7514 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7515 | CandidateSet); |
| 7516 | } |
| 7517 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | // C++ [over.built]p13: |
| 7522 | // |
| 7523 | // For every cv-qualified or cv-unqualified object type T |
| 7524 | // there exist candidate operator functions of the form |
| 7525 | // |
| 7526 | // T* operator+(T*, ptrdiff_t); |
| 7527 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7528 | // T* operator-(T*, ptrdiff_t); |
| 7529 | // T* operator+(ptrdiff_t, T*); |
| 7530 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7531 | // |
| 7532 | // C++ [over.built]p14: |
| 7533 | // |
| 7534 | // For every T, where T is a pointer to object type, there |
| 7535 | // exist candidate operator functions of the form |
| 7536 | // |
| 7537 | // ptrdiff_t operator-(T, T); |
| 7538 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7539 | /// Set of (canonical) types that we've already handled. |
| 7540 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7541 | |
| 7542 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7543 | QualType AsymetricParamTypes[2] = { |
| 7544 | S.Context.getPointerDiffType(), |
| 7545 | S.Context.getPointerDiffType(), |
| 7546 | }; |
| 7547 | for (BuiltinCandidateTypeSet::iterator |
| 7548 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7549 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7550 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7551 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7552 | if (!PointeeTy->isObjectType()) |
| 7553 | continue; |
| 7554 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7555 | AsymetricParamTypes[Arg] = *Ptr; |
| 7556 | if (Arg == 0 || Op == OO_Plus) { |
| 7557 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7558 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7559 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7560 | } |
| 7561 | if (Op == OO_Minus) { |
| 7562 | // ptrdiff_t operator-(T, T); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7563 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7564 | continue; |
| 7565 | |
| 7566 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7567 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7568 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7569 | } |
| 7570 | } |
| 7571 | } |
| 7572 | } |
| 7573 | |
| 7574 | // C++ [over.built]p12: |
| 7575 | // |
| 7576 | // For every pair of promoted arithmetic types L and R, there |
| 7577 | // exist candidate operator functions of the form |
| 7578 | // |
| 7579 | // LR operator*(L, R); |
| 7580 | // LR operator/(L, R); |
| 7581 | // LR operator+(L, R); |
| 7582 | // LR operator-(L, R); |
| 7583 | // bool operator<(L, R); |
| 7584 | // bool operator>(L, R); |
| 7585 | // bool operator<=(L, R); |
| 7586 | // bool operator>=(L, R); |
| 7587 | // bool operator==(L, R); |
| 7588 | // bool operator!=(L, R); |
| 7589 | // |
| 7590 | // where LR is the result of the usual arithmetic conversions |
| 7591 | // between types L and R. |
| 7592 | // |
| 7593 | // C++ [over.built]p24: |
| 7594 | // |
| 7595 | // For every pair of promoted arithmetic types L and R, there exist |
| 7596 | // candidate operator functions of the form |
| 7597 | // |
| 7598 | // LR operator?(bool, L, R); |
| 7599 | // |
| 7600 | // where LR is the result of the usual arithmetic conversions |
| 7601 | // between types L and R. |
| 7602 | // Our candidates ignore the first parameter. |
| 7603 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7604 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7605 | return; |
| 7606 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7607 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7608 | Left < LastPromotedArithmeticType; ++Left) { |
| 7609 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7610 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7611 | QualType LandR[2] = { getArithmeticType(Left), |
| 7612 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7613 | QualType Result = |
| 7614 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7615 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7616 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7617 | } |
| 7618 | } |
| 7619 | |
| 7620 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7621 | // conditional operator for vector types. |
| 7622 | for (BuiltinCandidateTypeSet::iterator |
| 7623 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7624 | Vec1End = CandidateTypes[0].vector_end(); |
| 7625 | Vec1 != Vec1End; ++Vec1) { |
| 7626 | for (BuiltinCandidateTypeSet::iterator |
| 7627 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7628 | Vec2End = CandidateTypes[1].vector_end(); |
| 7629 | Vec2 != Vec2End; ++Vec2) { |
| 7630 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7631 | QualType Result = S.Context.BoolTy; |
| 7632 | if (!isComparison) { |
| 7633 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7634 | Result = *Vec1; |
| 7635 | else |
| 7636 | Result = *Vec2; |
| 7637 | } |
| 7638 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7639 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7640 | } |
| 7641 | } |
| 7642 | } |
| 7643 | |
| 7644 | // C++ [over.built]p17: |
| 7645 | // |
| 7646 | // For every pair of promoted integral types L and R, there |
| 7647 | // exist candidate operator functions of the form |
| 7648 | // |
| 7649 | // LR operator%(L, R); |
| 7650 | // LR operator&(L, R); |
| 7651 | // LR operator^(L, R); |
| 7652 | // LR operator|(L, R); |
| 7653 | // L operator<<(L, R); |
| 7654 | // L operator>>(L, R); |
| 7655 | // |
| 7656 | // where LR is the result of the usual arithmetic conversions |
| 7657 | // between types L and R. |
| 7658 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7659 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7660 | return; |
| 7661 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7662 | for (unsigned Left = FirstPromotedIntegralType; |
| 7663 | Left < LastPromotedIntegralType; ++Left) { |
| 7664 | for (unsigned Right = FirstPromotedIntegralType; |
| 7665 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7666 | QualType LandR[2] = { getArithmeticType(Left), |
| 7667 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7668 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7669 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7670 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7671 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7672 | } |
| 7673 | } |
| 7674 | } |
| 7675 | |
| 7676 | // C++ [over.built]p20: |
| 7677 | // |
| 7678 | // For every pair (T, VQ), where T is an enumeration or |
| 7679 | // pointer to member type and VQ is either volatile or |
| 7680 | // empty, there exist candidate operator functions of the form |
| 7681 | // |
| 7682 | // VQ T& operator=(VQ T&, T); |
| 7683 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7684 | /// Set of (canonical) types that we've already handled. |
| 7685 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7686 | |
| 7687 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7688 | for (BuiltinCandidateTypeSet::iterator |
| 7689 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7690 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7691 | Enum != EnumEnd; ++Enum) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7692 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7693 | continue; |
| 7694 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7695 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7696 | } |
| 7697 | |
| 7698 | for (BuiltinCandidateTypeSet::iterator |
| 7699 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7700 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7701 | MemPtr != MemPtrEnd; ++MemPtr) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7702 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7703 | continue; |
| 7704 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7705 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7706 | } |
| 7707 | } |
| 7708 | } |
| 7709 | |
| 7710 | // C++ [over.built]p19: |
| 7711 | // |
| 7712 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7713 | // volatile or empty, there exist candidate operator functions |
| 7714 | // of the form |
| 7715 | // |
| 7716 | // T*VQ& operator=(T*VQ&, T*); |
| 7717 | // |
| 7718 | // C++ [over.built]p21: |
| 7719 | // |
| 7720 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7721 | // cv-unqualified object type and VQ is either volatile or |
| 7722 | // empty, there exist candidate operator functions of the form |
| 7723 | // |
| 7724 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7725 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7726 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7727 | /// Set of (canonical) types that we've already handled. |
| 7728 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7729 | |
| 7730 | for (BuiltinCandidateTypeSet::iterator |
| 7731 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7732 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7733 | Ptr != PtrEnd; ++Ptr) { |
| 7734 | // If this is operator=, keep track of the builtin candidates we added. |
| 7735 | if (isEqualOp) |
| 7736 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7737 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7738 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7739 | |
| 7740 | // non-volatile version |
| 7741 | QualType ParamTypes[2] = { |
| 7742 | S.Context.getLValueReferenceType(*Ptr), |
| 7743 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7744 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7745 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7746 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7747 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7748 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7749 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7750 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7751 | // volatile version |
| 7752 | ParamTypes[0] = |
| 7753 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7754 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7755 | /*IsAssigmentOperator=*/isEqualOp); |
| 7756 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7757 | |
| 7758 | if (!(*Ptr).isRestrictQualified() && |
| 7759 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7760 | // restrict version |
| 7761 | ParamTypes[0] |
| 7762 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7763 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7764 | /*IsAssigmentOperator=*/isEqualOp); |
| 7765 | |
| 7766 | if (NeedVolatile) { |
| 7767 | // volatile restrict version |
| 7768 | ParamTypes[0] |
| 7769 | = S.Context.getLValueReferenceType( |
| 7770 | S.Context.getCVRQualifiedType(*Ptr, |
| 7771 | (Qualifiers::Volatile | |
| 7772 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7773 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7774 | /*IsAssigmentOperator=*/isEqualOp); |
| 7775 | } |
| 7776 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | if (isEqualOp) { |
| 7780 | for (BuiltinCandidateTypeSet::iterator |
| 7781 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7782 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7783 | Ptr != PtrEnd; ++Ptr) { |
| 7784 | // Make sure we don't add the same candidate twice. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7785 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7786 | continue; |
| 7787 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7788 | QualType ParamTypes[2] = { |
| 7789 | S.Context.getLValueReferenceType(*Ptr), |
| 7790 | *Ptr, |
| 7791 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7792 | |
| 7793 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7794 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7795 | /*IsAssigmentOperator=*/true); |
| 7796 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7797 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7798 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7799 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7800 | // volatile version |
| 7801 | ParamTypes[0] = |
| 7802 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7803 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7804 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7805 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7806 | |
| 7807 | if (!(*Ptr).isRestrictQualified() && |
| 7808 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7809 | // restrict version |
| 7810 | ParamTypes[0] |
| 7811 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7812 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7813 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7814 | |
| 7815 | if (NeedVolatile) { |
| 7816 | // volatile restrict version |
| 7817 | ParamTypes[0] |
| 7818 | = S.Context.getLValueReferenceType( |
| 7819 | S.Context.getCVRQualifiedType(*Ptr, |
| 7820 | (Qualifiers::Volatile | |
| 7821 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7822 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7823 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7824 | } |
| 7825 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7826 | } |
| 7827 | } |
| 7828 | } |
| 7829 | |
| 7830 | // C++ [over.built]p18: |
| 7831 | // |
| 7832 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7833 | // VQ is either volatile or empty, and R is a promoted |
| 7834 | // arithmetic type, there exist candidate operator functions of |
| 7835 | // the form |
| 7836 | // |
| 7837 | // VQ L& operator=(VQ L&, R); |
| 7838 | // VQ L& operator*=(VQ L&, R); |
| 7839 | // VQ L& operator/=(VQ L&, R); |
| 7840 | // VQ L& operator+=(VQ L&, R); |
| 7841 | // VQ L& operator-=(VQ L&, R); |
| 7842 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7843 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7844 | return; |
| 7845 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7846 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7847 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7848 | Right < LastPromotedArithmeticType; ++Right) { |
| 7849 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7850 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7851 | |
| 7852 | // Add this built-in operator as a candidate (VQ is empty). |
| 7853 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7854 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7855 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7856 | /*IsAssigmentOperator=*/isEqualOp); |
| 7857 | |
| 7858 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7859 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7860 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7861 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7862 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7863 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7864 | /*IsAssigmentOperator=*/isEqualOp); |
| 7865 | } |
| 7866 | } |
| 7867 | } |
| 7868 | |
| 7869 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7870 | for (BuiltinCandidateTypeSet::iterator |
| 7871 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7872 | Vec1End = CandidateTypes[0].vector_end(); |
| 7873 | Vec1 != Vec1End; ++Vec1) { |
| 7874 | for (BuiltinCandidateTypeSet::iterator |
| 7875 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7876 | Vec2End = CandidateTypes[1].vector_end(); |
| 7877 | Vec2 != Vec2End; ++Vec2) { |
| 7878 | QualType ParamTypes[2]; |
| 7879 | ParamTypes[1] = *Vec2; |
| 7880 | // Add this built-in operator as a candidate (VQ is empty). |
| 7881 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7882 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7883 | /*IsAssigmentOperator=*/isEqualOp); |
| 7884 | |
| 7885 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7886 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7887 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7888 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7889 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7890 | /*IsAssigmentOperator=*/isEqualOp); |
| 7891 | } |
| 7892 | } |
| 7893 | } |
| 7894 | } |
| 7895 | |
| 7896 | // C++ [over.built]p22: |
| 7897 | // |
| 7898 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7899 | // is either volatile or empty, and R is a promoted integral |
| 7900 | // type, there exist candidate operator functions of the form |
| 7901 | // |
| 7902 | // VQ L& operator%=(VQ L&, R); |
| 7903 | // VQ L& operator<<=(VQ L&, R); |
| 7904 | // VQ L& operator>>=(VQ L&, R); |
| 7905 | // VQ L& operator&=(VQ L&, R); |
| 7906 | // VQ L& operator^=(VQ L&, R); |
| 7907 | // VQ L& operator|=(VQ L&, R); |
| 7908 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7909 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7910 | return; |
| 7911 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7912 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7913 | for (unsigned Right = FirstPromotedIntegralType; |
| 7914 | Right < LastPromotedIntegralType; ++Right) { |
| 7915 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7916 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7917 | |
| 7918 | // Add this built-in operator as a candidate (VQ is empty). |
| 7919 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7920 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7921 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7922 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7923 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7924 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7925 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7926 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7927 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7928 | } |
| 7929 | } |
| 7930 | } |
| 7931 | } |
| 7932 | |
| 7933 | // C++ [over.operator]p23: |
| 7934 | // |
| 7935 | // There also exist candidate operator functions of the form |
| 7936 | // |
| 7937 | // bool operator!(bool); |
| 7938 | // bool operator&&(bool, bool); |
| 7939 | // bool operator||(bool, bool); |
| 7940 | void addExclaimOverload() { |
| 7941 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7942 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7943 | /*IsAssignmentOperator=*/false, |
| 7944 | /*NumContextualBoolArguments=*/1); |
| 7945 | } |
| 7946 | void addAmpAmpOrPipePipeOverload() { |
| 7947 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7948 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7949 | /*IsAssignmentOperator=*/false, |
| 7950 | /*NumContextualBoolArguments=*/2); |
| 7951 | } |
| 7952 | |
| 7953 | // C++ [over.built]p13: |
| 7954 | // |
| 7955 | // For every cv-qualified or cv-unqualified object type T there |
| 7956 | // exist candidate operator functions of the form |
| 7957 | // |
| 7958 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7959 | // T& operator[](T*, ptrdiff_t); |
| 7960 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7961 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7962 | // T& operator[](ptrdiff_t, T*); |
| 7963 | void addSubscriptOverloads() { |
| 7964 | for (BuiltinCandidateTypeSet::iterator |
| 7965 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7966 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7967 | Ptr != PtrEnd; ++Ptr) { |
| 7968 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7969 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7970 | if (!PointeeType->isObjectType()) |
| 7971 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7972 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7973 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7974 | |
| 7975 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7976 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7977 | } |
| 7978 | |
| 7979 | for (BuiltinCandidateTypeSet::iterator |
| 7980 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7981 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7982 | Ptr != PtrEnd; ++Ptr) { |
| 7983 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7984 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7985 | if (!PointeeType->isObjectType()) |
| 7986 | continue; |
| 7987 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7988 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7989 | |
| 7990 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7991 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7992 | } |
| 7993 | } |
| 7994 | |
| 7995 | // C++ [over.built]p11: |
| 7996 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7997 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7998 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7999 | // there exist candidate operator functions of the form |
| 8000 | // |
| 8001 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 8002 | // |
| 8003 | // where CV12 is the union of CV1 and CV2. |
| 8004 | void addArrowStarOverloads() { |
| 8005 | for (BuiltinCandidateTypeSet::iterator |
| 8006 | Ptr = CandidateTypes[0].pointer_begin(), |
| 8007 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 8008 | Ptr != PtrEnd; ++Ptr) { |
| 8009 | QualType C1Ty = (*Ptr); |
| 8010 | QualType C1; |
| 8011 | QualifierCollector Q1; |
| 8012 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 8013 | if (!isa<RecordType>(C1)) |
| 8014 | continue; |
| 8015 | // heuristic to reduce number of builtin candidates in the set. |
| 8016 | // Add volatile/restrict version only if there are conversions to a |
| 8017 | // volatile/restrict type. |
| 8018 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 8019 | continue; |
| 8020 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 8021 | continue; |
| 8022 | for (BuiltinCandidateTypeSet::iterator |
| 8023 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 8024 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 8025 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 8026 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 8027 | QualType C2 = QualType(mptr->getClass(), 0); |
| 8028 | C2 = C2.getUnqualifiedType(); |
| 8029 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 8030 | break; |
| 8031 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 8032 | // build CV12 T& |
| 8033 | QualType T = mptr->getPointeeType(); |
| 8034 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 8035 | T.isVolatileQualified()) |
| 8036 | continue; |
| 8037 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 8038 | T.isRestrictQualified()) |
| 8039 | continue; |
| 8040 | T = Q1.apply(S.Context, T); |
| 8041 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8042 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8043 | } |
| 8044 | } |
| 8045 | } |
| 8046 | |
| 8047 | // Note that we don't consider the first argument, since it has been |
| 8048 | // contextually converted to bool long ago. The candidates below are |
| 8049 | // therefore added as binary. |
| 8050 | // |
| 8051 | // C++ [over.built]p25: |
| 8052 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 8053 | // enumeration type, there exist candidate operator functions of the form |
| 8054 | // |
| 8055 | // T operator?(bool, T, T); |
| 8056 | // |
| 8057 | void addConditionalOperatorOverloads() { |
| 8058 | /// Set of (canonical) types that we've already handled. |
| 8059 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 8060 | |
| 8061 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 8062 | for (BuiltinCandidateTypeSet::iterator |
| 8063 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 8064 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 8065 | Ptr != PtrEnd; ++Ptr) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8066 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8067 | continue; |
| 8068 | |
| 8069 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8070 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8071 | } |
| 8072 | |
| 8073 | for (BuiltinCandidateTypeSet::iterator |
| 8074 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 8075 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 8076 | MemPtr != MemPtrEnd; ++MemPtr) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8077 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8078 | continue; |
| 8079 | |
| 8080 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8081 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8082 | } |
| 8083 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8084 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8085 | for (BuiltinCandidateTypeSet::iterator |
| 8086 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 8087 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 8088 | Enum != EnumEnd; ++Enum) { |
| 8089 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 8090 | continue; |
| 8091 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8092 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum)).second) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8093 | continue; |
| 8094 | |
| 8095 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8096 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8097 | } |
| 8098 | } |
| 8099 | } |
| 8100 | } |
| 8101 | }; |
| 8102 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8103 | } // end anonymous namespace |
| 8104 | |
| 8105 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 8106 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 8107 | /// on the operator @p Op and the arguments given. For example, if the |
| 8108 | /// operator is a binary '+', this routine might add "int |
| 8109 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 8110 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 8111 | SourceLocation OpLoc, |
| 8112 | ArrayRef<Expr *> Args, |
| 8113 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8114 | // Find all of the types that the arguments can convert to, but only |
| 8115 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8116 | // that make use of these types. Also record whether we encounter non-record |
| 8117 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 8118 | Qualifiers VisibleTypeConversionsQuals; |
| 8119 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8120 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 8121 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8122 | |
| 8123 | bool HasNonRecordCandidateType = false; |
| 8124 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8125 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8126 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8127 | CandidateTypes.emplace_back(*this); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 8128 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 8129 | OpLoc, |
| 8130 | true, |
| 8131 | (Op == OO_Exclaim || |
| 8132 | Op == OO_AmpAmp || |
| 8133 | Op == OO_PipePipe), |
| 8134 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8135 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 8136 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 8137 | HasArithmeticOrEnumeralCandidateType = |
| 8138 | HasArithmeticOrEnumeralCandidateType || |
| 8139 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 8140 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8141 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8142 | // Exit early when no non-record types have been added to the candidate set |
| 8143 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 8144 | // |
| 8145 | // We can't exit early for !, ||, or &&, since there we have always have |
| 8146 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8147 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 8148 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8149 | return; |
| 8150 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8151 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8152 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8153 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8154 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8155 | CandidateTypes, CandidateSet); |
| 8156 | |
| 8157 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8158 | switch (Op) { |
| 8159 | case OO_None: |
| 8160 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8161 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8162 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8163 | case OO_New: |
| 8164 | case OO_Delete: |
| 8165 | case OO_Array_New: |
| 8166 | case OO_Array_Delete: |
| 8167 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8168 | llvm_unreachable( |
| 8169 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8170 | |
| 8171 | case OO_Comma: |
| 8172 | case OO_Arrow: |
| 8173 | // C++ [over.match.oper]p3: |
| 8174 | // -- For the operator ',', the unary operator '&', or the |
| 8175 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8176 | break; |
| 8177 | |
| 8178 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8179 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8180 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 8181 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8182 | |
| 8183 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8184 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8185 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 8186 | } else { |
| 8187 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 8188 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 8189 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8190 | break; |
| 8191 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8192 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8193 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8194 | OpBuilder.addUnaryStarPointerOverloads(); |
| 8195 | else |
| 8196 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 8197 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8198 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8199 | case OO_Slash: |
| 8200 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 8201 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8202 | |
| 8203 | case OO_PlusPlus: |
| 8204 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8205 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 8206 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8207 | break; |
| 8208 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 8209 | case OO_EqualEqual: |
| 8210 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8211 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 8212 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 8213 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8214 | case OO_Less: |
| 8215 | case OO_Greater: |
| 8216 | case OO_LessEqual: |
| 8217 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 8218 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 8219 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 8220 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8221 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8222 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8223 | case OO_Caret: |
| 8224 | case OO_Pipe: |
| 8225 | case OO_LessLess: |
| 8226 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8227 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8228 | break; |
| 8229 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8230 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8231 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8232 | // C++ [over.match.oper]p3: |
| 8233 | // -- For the operator ',', the unary operator '&', or the |
| 8234 | // operator '->', the built-in candidates set is empty. |
| 8235 | break; |
| 8236 | |
| 8237 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 8238 | break; |
| 8239 | |
| 8240 | case OO_Tilde: |
| 8241 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 8242 | break; |
| 8243 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8244 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8245 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 8246 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8247 | |
| 8248 | case OO_PlusEqual: |
| 8249 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8250 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8251 | // Fall through. |
| 8252 | |
| 8253 | case OO_StarEqual: |
| 8254 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8255 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8256 | break; |
| 8257 | |
| 8258 | case OO_PercentEqual: |
| 8259 | case OO_LessLessEqual: |
| 8260 | case OO_GreaterGreaterEqual: |
| 8261 | case OO_AmpEqual: |
| 8262 | case OO_CaretEqual: |
| 8263 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8264 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8265 | break; |
| 8266 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8267 | case OO_Exclaim: |
| 8268 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8269 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8270 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8271 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8272 | case OO_PipePipe: |
| 8273 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8274 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8275 | |
| 8276 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8277 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8278 | break; |
| 8279 | |
| 8280 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8281 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8282 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8283 | |
| 8284 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8285 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 8286 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 8287 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8288 | } |
| 8289 | } |
| 8290 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8291 | /// \brief Add function candidates found via argument-dependent lookup |
| 8292 | /// to the set of overloading candidates. |
| 8293 | /// |
| 8294 | /// This routine performs argument-dependent name lookup based on the |
| 8295 | /// given function name (which may also be an operator name) and adds |
| 8296 | /// all of the overload candidates found by ADL to the overload |
| 8297 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8298 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8299 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8300 | SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8301 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8302 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8303 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 8304 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8305 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8306 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 8307 | // FIXME: This approach for uniquing ADL results (and removing |
| 8308 | // redundant candidates from the set) relies on pointer-equality, |
| 8309 | // which means we need to key off the canonical decl. However, |
| 8310 | // always going back to the canonical decl might not get us the |
| 8311 | // right set of default arguments. What default arguments are |
| 8312 | // we supposed to consider on ADL candidates, anyway? |
| 8313 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8314 | // FIXME: Pass in the explicit template arguments? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8315 | ArgumentDependentLookup(Name, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8316 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8317 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8318 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 8319 | CandEnd = CandidateSet.end(); |
| 8320 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8321 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8322 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8323 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8324 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8325 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8326 | |
| 8327 | // For each of the ADL candidates we found, add it to the overload |
| 8328 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8329 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8330 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8331 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8332 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8333 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8334 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8335 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 8336 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8337 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8338 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8339 | FoundDecl, ExplicitTemplateArgs, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8340 | Args, CandidateSet, PartialOverloading); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8341 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8344 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 8345 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8346 | bool clang::isBetterOverloadCandidate(Sema &S, const OverloadCandidate &Cand1, |
| 8347 | const OverloadCandidate &Cand2, |
| 8348 | SourceLocation Loc, |
| 8349 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8350 | // Define viable functions to be better candidates than non-viable |
| 8351 | // functions. |
| 8352 | if (!Cand2.Viable) |
| 8353 | return Cand1.Viable; |
| 8354 | else if (!Cand1.Viable) |
| 8355 | return false; |
| 8356 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8357 | // C++ [over.match.best]p1: |
| 8358 | // |
| 8359 | // -- if F is a static member function, ICS1(F) is defined such |
| 8360 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 8361 | // any function G, and, symmetrically, ICS1(G) is neither |
| 8362 | // better nor worse than ICS1(F). |
| 8363 | unsigned StartArg = 0; |
| 8364 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 8365 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8366 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8367 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8368 | // A viable function F1 is defined to be a better function than another |
| 8369 | // 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] | 8370 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8371 | unsigned NumArgs = Cand1.NumConversions; |
| 8372 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8373 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8374 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8375 | switch (CompareImplicitConversionSequences(S, |
| 8376 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8377 | Cand2.Conversions[ArgIdx])) { |
| 8378 | case ImplicitConversionSequence::Better: |
| 8379 | // Cand1 has a better conversion sequence. |
| 8380 | HasBetterConversion = true; |
| 8381 | break; |
| 8382 | |
| 8383 | case ImplicitConversionSequence::Worse: |
| 8384 | // Cand1 can't be better than Cand2. |
| 8385 | return false; |
| 8386 | |
| 8387 | case ImplicitConversionSequence::Indistinguishable: |
| 8388 | // Do nothing. |
| 8389 | break; |
| 8390 | } |
| 8391 | } |
| 8392 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8393 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8394 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8395 | if (HasBetterConversion) |
| 8396 | return true; |
| 8397 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8398 | // -- the context is an initialization by user-defined conversion |
| 8399 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 8400 | // from the return type of F1 to the destination type (i.e., |
| 8401 | // the type of the entity being initialized) is a better |
| 8402 | // conversion sequence than the standard conversion sequence |
| 8403 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8404 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8405 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8406 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 8407 | // First check whether we prefer one of the conversion functions over the |
| 8408 | // other. This only distinguishes the results in non-standard, extension |
| 8409 | // cases such as the conversion from a lambda closure type to a function |
| 8410 | // pointer or block. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8411 | ImplicitConversionSequence::CompareKind Result = |
| 8412 | compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 8413 | if (Result == ImplicitConversionSequence::Indistinguishable) |
| 8414 | Result = CompareStandardConversionSequences(S, |
| 8415 | Cand1.FinalConversion, |
| 8416 | Cand2.FinalConversion); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8417 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8418 | if (Result != ImplicitConversionSequence::Indistinguishable) |
| 8419 | return Result == ImplicitConversionSequence::Better; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8420 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8421 | // FIXME: Compare kind of reference binding if conversion functions |
| 8422 | // convert to a reference type used in direct reference binding, per |
| 8423 | // C++14 [over.match.best]p1 section 2 bullet 3. |
| 8424 | } |
| 8425 | |
| 8426 | // -- F1 is a non-template function and F2 is a function template |
| 8427 | // specialization, or, if not that, |
| 8428 | bool Cand1IsSpecialization = Cand1.Function && |
| 8429 | Cand1.Function->getPrimaryTemplate(); |
| 8430 | bool Cand2IsSpecialization = Cand2.Function && |
| 8431 | Cand2.Function->getPrimaryTemplate(); |
| 8432 | if (Cand1IsSpecialization != Cand2IsSpecialization) |
| 8433 | return Cand2IsSpecialization; |
| 8434 | |
| 8435 | // -- F1 and F2 are function template specializations, and the function |
| 8436 | // template for F1 is more specialized than the template for F2 |
| 8437 | // according to the partial ordering rules described in 14.5.5.2, or, |
| 8438 | // if not that, |
| 8439 | if (Cand1IsSpecialization && Cand2IsSpecialization) { |
| 8440 | if (FunctionTemplateDecl *BetterTemplate |
| 8441 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 8442 | Cand2.Function->getPrimaryTemplate(), |
| 8443 | Loc, |
| 8444 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 8445 | : TPOC_Call, |
| 8446 | Cand1.ExplicitCallArguments, |
| 8447 | Cand2.ExplicitCallArguments)) |
| 8448 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8449 | } |
| 8450 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8451 | // Check for enable_if value-based overload resolution. |
| 8452 | if (Cand1.Function && Cand2.Function && |
| 8453 | (Cand1.Function->hasAttr<EnableIfAttr>() || |
| 8454 | Cand2.Function->hasAttr<EnableIfAttr>())) { |
| 8455 | // FIXME: The next several lines are just |
| 8456 | // specific_attr_iterator<EnableIfAttr> but going in declaration order, |
| 8457 | // instead of reverse order which is how they're stored in the AST. |
| 8458 | AttrVec Cand1Attrs; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8459 | if (Cand1.Function->hasAttrs()) { |
| 8460 | Cand1Attrs = Cand1.Function->getAttrs(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8461 | Cand1Attrs.erase(std::remove_if(Cand1Attrs.begin(), Cand1Attrs.end(), |
| 8462 | IsNotEnableIfAttr), |
| 8463 | Cand1Attrs.end()); |
| 8464 | std::reverse(Cand1Attrs.begin(), Cand1Attrs.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8465 | } |
| 8466 | |
| 8467 | AttrVec Cand2Attrs; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8468 | if (Cand2.Function->hasAttrs()) { |
| 8469 | Cand2Attrs = Cand2.Function->getAttrs(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8470 | Cand2Attrs.erase(std::remove_if(Cand2Attrs.begin(), Cand2Attrs.end(), |
| 8471 | IsNotEnableIfAttr), |
| 8472 | Cand2Attrs.end()); |
| 8473 | std::reverse(Cand2Attrs.begin(), Cand2Attrs.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8474 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8475 | |
| 8476 | // Candidate 1 is better if it has strictly more attributes and |
| 8477 | // the common sequence is identical. |
| 8478 | if (Cand1Attrs.size() <= Cand2Attrs.size()) |
| 8479 | return false; |
| 8480 | |
| 8481 | auto Cand1I = Cand1Attrs.begin(); |
| 8482 | for (auto &Cand2A : Cand2Attrs) { |
| 8483 | auto &Cand1A = *Cand1I++; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8484 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8485 | cast<EnableIfAttr>(Cand1A)->getCond()->Profile(Cand1ID, |
| 8486 | S.getASTContext(), true); |
| 8487 | cast<EnableIfAttr>(Cand2A)->getCond()->Profile(Cand2ID, |
| 8488 | S.getASTContext(), true); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8489 | if (Cand1ID != Cand2ID) |
| 8490 | return false; |
| 8491 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8492 | |
| 8493 | return true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8494 | } |
| 8495 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8496 | return false; |
| 8497 | } |
| 8498 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8499 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8500 | /// within an overload candidate set. |
| 8501 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8502 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8503 | /// which overload resolution occurs. |
| 8504 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8505 | /// \param Best If overload resolution was successful or found a deleted |
| 8506 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8507 | /// |
| 8508 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8509 | OverloadingResult |
| 8510 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8511 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8512 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8513 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8514 | Best = end(); |
| 8515 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8516 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8517 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8518 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8519 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8520 | } |
| 8521 | |
| 8522 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8523 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8524 | return OR_No_Viable_Function; |
| 8525 | |
| 8526 | // Make sure that this function is better than every other viable |
| 8527 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8528 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8529 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8530 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8531 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8532 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8533 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8534 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8535 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8537 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8538 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8539 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8540 | (Best->Function->isDeleted() || |
| 8541 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8542 | return OR_Deleted; |
| 8543 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8544 | return OR_Success; |
| 8545 | } |
| 8546 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8547 | namespace { |
| 8548 | |
| 8549 | enum OverloadCandidateKind { |
| 8550 | oc_function, |
| 8551 | oc_method, |
| 8552 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8553 | oc_function_template, |
| 8554 | oc_method_template, |
| 8555 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8556 | oc_implicit_default_constructor, |
| 8557 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8558 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8559 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8560 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8561 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8562 | }; |
| 8563 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8564 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8565 | FunctionDecl *Fn, |
| 8566 | std::string &Description) { |
| 8567 | bool isTemplate = false; |
| 8568 | |
| 8569 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8570 | isTemplate = true; |
| 8571 | Description = S.getTemplateArgumentBindingsText( |
| 8572 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8573 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8574 | |
| 8575 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8576 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8577 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8578 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8579 | if (Ctor->getInheritedConstructor()) |
| 8580 | return oc_implicit_inherited_constructor; |
| 8581 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8582 | if (Ctor->isDefaultConstructor()) |
| 8583 | return oc_implicit_default_constructor; |
| 8584 | |
| 8585 | if (Ctor->isMoveConstructor()) |
| 8586 | return oc_implicit_move_constructor; |
| 8587 | |
| 8588 | assert(Ctor->isCopyConstructor() && |
| 8589 | "unexpected sort of implicit constructor"); |
| 8590 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8591 | } |
| 8592 | |
| 8593 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8594 | // This actually gets spelled 'candidate function' for now, but |
| 8595 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8596 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8597 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8598 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8599 | if (Meth->isMoveAssignmentOperator()) |
| 8600 | return oc_implicit_move_assignment; |
| 8601 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8602 | if (Meth->isCopyAssignmentOperator()) |
| 8603 | return oc_implicit_copy_assignment; |
| 8604 | |
| 8605 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8606 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8607 | } |
| 8608 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8609 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8612 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8613 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8614 | if (!Ctor) return; |
| 8615 | |
| 8616 | Ctor = Ctor->getInheritedConstructor(); |
| 8617 | if (!Ctor) return; |
| 8618 | |
| 8619 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8620 | } |
| 8621 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8622 | } // end anonymous namespace |
| 8623 | |
| 8624 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8625 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8626 | std::string FnDesc; |
| 8627 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8628 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8629 | << (unsigned) K << FnDesc; |
| 8630 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8631 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8632 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8633 | } |
| 8634 | |
Nick Lewycky | 199e370 | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 8635 | // Notes the location of all overload candidates designated through |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8636 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8637 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8638 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8639 | |
| 8640 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8641 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8642 | |
| 8643 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8644 | IEnd = OvlExpr->decls_end(); |
| 8645 | I != IEnd; ++I) { |
| 8646 | if (FunctionTemplateDecl *FunTmpl = |
| 8647 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8648 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8649 | } else if (FunctionDecl *Fun |
| 8650 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8651 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8652 | } |
| 8653 | } |
| 8654 | } |
| 8655 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8656 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8657 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8658 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8659 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8660 | Sema &S, |
| 8661 | SourceLocation CaretLoc, |
| 8662 | const PartialDiagnostic &PDiag) const { |
| 8663 | S.Diag(CaretLoc, PDiag) |
| 8664 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8665 | // FIXME: The note limiting machinery is borrowed from |
| 8666 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8667 | // refactoring here. |
| 8668 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8669 | unsigned CandsShown = 0; |
| 8670 | AmbiguousConversionSequence::const_iterator I, E; |
| 8671 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8672 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8673 | break; |
| 8674 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8675 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8676 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8677 | if (I != E) |
| 8678 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8679 | } |
| 8680 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8681 | static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, |
| 8682 | unsigned I) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8683 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8684 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8685 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8686 | FunctionDecl *Fn = Cand->Function; |
| 8687 | |
| 8688 | // There's a conversion slot for the object argument if this is a |
| 8689 | // non-constructor method. Note that 'I' corresponds the |
| 8690 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8691 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8692 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8693 | if (I == 0) |
| 8694 | isObjectArgument = true; |
| 8695 | else |
| 8696 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8699 | std::string FnDesc; |
| 8700 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8701 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8702 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8703 | QualType FromTy = Conv.Bad.getFromType(); |
| 8704 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8705 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8706 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8707 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8708 | Expr *E = FromExpr->IgnoreParens(); |
| 8709 | if (isa<UnaryOperator>(E)) |
| 8710 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8711 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8712 | |
| 8713 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8714 | << (unsigned) FnKind << FnDesc |
| 8715 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8716 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8717 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8718 | return; |
| 8719 | } |
| 8720 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8721 | // Do some hand-waving analysis to see if the non-viability is due |
| 8722 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8723 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8724 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8725 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8726 | CToTy = RT->getPointeeType(); |
| 8727 | else { |
| 8728 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8729 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8730 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8731 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8732 | } |
| 8733 | |
| 8734 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8735 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8736 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8737 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8738 | |
| 8739 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8740 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8741 | << (unsigned) FnKind << FnDesc |
| 8742 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8743 | << FromTy |
| 8744 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8745 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8746 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8747 | return; |
| 8748 | } |
| 8749 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8750 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8751 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8752 | << (unsigned) FnKind << FnDesc |
| 8753 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8754 | << FromTy |
| 8755 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8756 | << (unsigned) isObjectArgument << I+1; |
| 8757 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8758 | return; |
| 8759 | } |
| 8760 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8761 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8762 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8763 | << (unsigned) FnKind << FnDesc |
| 8764 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8765 | << FromTy |
| 8766 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8767 | << (unsigned) isObjectArgument << I+1; |
| 8768 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8769 | return; |
| 8770 | } |
| 8771 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8772 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8773 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8774 | |
| 8775 | if (isObjectArgument) { |
| 8776 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8777 | << (unsigned) FnKind << FnDesc |
| 8778 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8779 | << FromTy << (CVR - 1); |
| 8780 | } else { |
| 8781 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8782 | << (unsigned) FnKind << FnDesc |
| 8783 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8784 | << FromTy << (CVR - 1) << I+1; |
| 8785 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8786 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8787 | return; |
| 8788 | } |
| 8789 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8790 | // Special diagnostic for failure to convert an initializer list, since |
| 8791 | // telling the user that it has type void is not useful. |
| 8792 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8793 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8794 | << (unsigned) FnKind << FnDesc |
| 8795 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8796 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8797 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8798 | return; |
| 8799 | } |
| 8800 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8801 | // Diagnose references or pointers to incomplete types differently, |
| 8802 | // since it's far from impossible that the incompleteness triggered |
| 8803 | // the failure. |
| 8804 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8805 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8806 | TempFromTy = PTy->getPointeeType(); |
| 8807 | if (TempFromTy->isIncompleteType()) { |
| 8808 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8809 | << (unsigned) FnKind << FnDesc |
| 8810 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8811 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8812 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8813 | return; |
| 8814 | } |
| 8815 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8816 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8817 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8818 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8819 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8820 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8821 | FromPtrTy->getPointeeType()) && |
| 8822 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8823 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8824 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8825 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8826 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8827 | } |
| 8828 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8829 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8830 | if (const ObjCObjectPointerType *ToPtrTy |
| 8831 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8832 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8833 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8834 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8835 | FromPtrTy->getPointeeType()) && |
| 8836 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8837 | BaseToDerivedConversion = 2; |
| 8838 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8839 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8840 | !FromTy->isIncompleteType() && |
| 8841 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8842 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8843 | BaseToDerivedConversion = 3; |
| 8844 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8845 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8846 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8847 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8848 | << (unsigned) FnKind << FnDesc |
| 8849 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8850 | << (unsigned) isObjectArgument << I + 1; |
| 8851 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8852 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8853 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8854 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8855 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8856 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8857 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8858 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8859 | << (unsigned) FnKind << FnDesc |
| 8860 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8861 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8862 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8863 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8864 | return; |
| 8865 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8866 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8867 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8868 | isa<PointerType>(CToTy)) { |
| 8869 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8870 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8871 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8872 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8873 | << (unsigned) FnKind << FnDesc |
| 8874 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8875 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8876 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8877 | return; |
| 8878 | } |
| 8879 | } |
| 8880 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8881 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8882 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8883 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8884 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8885 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8886 | << (unsigned) (Cand->Fix.Kind); |
| 8887 | |
| 8888 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8889 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8890 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8891 | FDiag << *HI; |
| 8892 | S.Diag(Fn->getLocation(), FDiag); |
| 8893 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8894 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8895 | } |
| 8896 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8897 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8898 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8899 | /// over a candidate in any candidate set. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8900 | static bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8901 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8902 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8903 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8904 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8905 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8906 | // have an arity mismatch when in fact it looks like we have the |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8907 | // right number of arguments, because only overloaded operators have |
| 8908 | // the weird behavior of overloading member and non-member functions. |
| 8909 | // Just don't report anything. |
| 8910 | if (Fn->isInvalidDecl() && |
| 8911 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8912 | return true; |
| 8913 | |
| 8914 | if (NumArgs < MinParams) { |
| 8915 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8916 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8917 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8918 | } else { |
| 8919 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8920 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8921 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8922 | } |
| 8923 | |
| 8924 | return false; |
| 8925 | } |
| 8926 | |
| 8927 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8928 | static void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8929 | assert(isa<FunctionDecl>(D) && |
| 8930 | "The templated declaration should at least be a function" |
| 8931 | " when diagnosing bad template argument deduction due to too many" |
| 8932 | " or too few arguments"); |
| 8933 | |
| 8934 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8935 | |
| 8936 | // TODO: treat calls to a missing default constructor as a special case |
| 8937 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8938 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8939 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8940 | // at least / at most / exactly |
| 8941 | unsigned mode, modeCount; |
| 8942 | if (NumFormalArgs < MinParams) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8943 | if (MinParams != FnTy->getNumParams() || FnTy->isVariadic() || |
| 8944 | FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8945 | mode = 0; // "at least" |
| 8946 | else |
| 8947 | mode = 2; // "exactly" |
| 8948 | modeCount = MinParams; |
| 8949 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8950 | if (MinParams != FnTy->getNumParams()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8951 | mode = 1; // "at most" |
| 8952 | else |
| 8953 | mode = 2; // "exactly" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8954 | modeCount = FnTy->getNumParams(); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8955 | } |
| 8956 | |
| 8957 | std::string Description; |
| 8958 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8959 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8960 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8961 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8962 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != nullptr) |
| 8963 | << mode << Fn->getParamDecl(0) << NumFormalArgs; |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8964 | else |
| 8965 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8966 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != nullptr) |
| 8967 | << mode << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8968 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8969 | } |
| 8970 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8971 | /// Arity mismatch diagnosis specific to a function overload candidate. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8972 | static void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8973 | unsigned NumFormalArgs) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8974 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8975 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8976 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8977 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8978 | static TemplateDecl *getDescribedTemplate(Decl *Templated) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8979 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8980 | return FD->getDescribedFunctionTemplate(); |
| 8981 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8982 | return RD->getDescribedClassTemplate(); |
| 8983 | |
| 8984 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8985 | " for bad deduction diagnosis"); |
| 8986 | } |
| 8987 | |
| 8988 | /// Diagnose a failed template-argument deduction. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8989 | static void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8990 | DeductionFailureInfo &DeductionFailure, |
| 8991 | unsigned NumArgs) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8992 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8993 | NamedDecl *ParamD; |
| 8994 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8995 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8996 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8997 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8998 | case Sema::TDK_Success: |
| 8999 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 9000 | |
| 9001 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9002 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9003 | S.Diag(Templated->getLocation(), |
| 9004 | diag::note_ovl_candidate_incomplete_deduction) |
| 9005 | << ParamD->getDeclName(); |
| 9006 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9007 | return; |
| 9008 | } |
| 9009 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9010 | case Sema::TDK_Underqualified: { |
| 9011 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 9012 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 9013 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9014 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9015 | |
| 9016 | // Param will have been canonicalized, but it should just be a |
| 9017 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 9018 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9019 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 9020 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9021 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 9022 | |
| 9023 | // Arg has also been canonicalized, but there's nothing we can do |
| 9024 | // about that. It also doesn't matter as much, because it won't |
| 9025 | // have any template parameters in it (because deduction isn't |
| 9026 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9027 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9028 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9029 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 9030 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 9031 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9032 | return; |
| 9033 | } |
| 9034 | |
| 9035 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9036 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9037 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9038 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9039 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9040 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9041 | which = 1; |
| 9042 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9043 | which = 2; |
| 9044 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9045 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9046 | S.Diag(Templated->getLocation(), |
| 9047 | diag::note_ovl_candidate_inconsistent_deduction) |
| 9048 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 9049 | << *DeductionFailure.getSecondArg(); |
| 9050 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9051 | return; |
| 9052 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 9053 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9054 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9055 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9056 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9057 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9058 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9059 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9060 | else { |
| 9061 | int index = 0; |
| 9062 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 9063 | index = TTP->getIndex(); |
| 9064 | else if (NonTypeTemplateParmDecl *NTTP |
| 9065 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 9066 | index = NTTP->getIndex(); |
| 9067 | else |
| 9068 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9069 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9070 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9071 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9072 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9073 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9074 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9075 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 9076 | case Sema::TDK_TooManyArguments: |
| 9077 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9078 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 9079 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 9080 | |
| 9081 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9082 | S.Diag(Templated->getLocation(), |
| 9083 | diag::note_ovl_candidate_instantiation_depth); |
| 9084 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 9085 | return; |
| 9086 | |
| 9087 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9088 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9089 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9090 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9091 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9092 | TemplateArgString = " "; |
| 9093 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9094 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9095 | } |
| 9096 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9097 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9098 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9099 | if (PDiag && PDiag->second.getDiagID() == |
| 9100 | diag::err_typename_nested_not_found_enable_if) { |
| 9101 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 9102 | // name of the enable_if template. These are both present in PDiag. |
| 9103 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 9104 | << "'enable_if'" << TemplateArgString; |
| 9105 | return; |
| 9106 | } |
| 9107 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9108 | // Format the SFINAE diagnostic into the argument string. |
| 9109 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 9110 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9111 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9112 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9113 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9114 | SFINAEArgString = ": "; |
| 9115 | R = SourceRange(PDiag->first, PDiag->first); |
| 9116 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 9117 | } |
| 9118 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9119 | S.Diag(Templated->getLocation(), |
| 9120 | diag::note_ovl_candidate_substitution_failure) |
| 9121 | << TemplateArgString << SFINAEArgString << R; |
| 9122 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 9123 | return; |
| 9124 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9125 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 9126 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9127 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 9128 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 9129 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9130 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 9131 | return; |
| 9132 | } |
| 9133 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 9134 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 9135 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9136 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 9137 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 9138 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 9139 | SecondTA.getKind() == TemplateArgument::Template) { |
| 9140 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 9141 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 9142 | if (FirstTN.getKind() == TemplateName::Template && |
| 9143 | SecondTN.getKind() == TemplateName::Template) { |
| 9144 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 9145 | SecondTN.getAsTemplateDecl()->getName()) { |
| 9146 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 9147 | // the same. This particular case is a bit difficult since: |
| 9148 | // 1) It is passed as a string to the diagnostic printer. |
| 9149 | // 2) The diagnostic printer only attempts to find a better |
| 9150 | // name for types, not decls. |
| 9151 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9152 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 9153 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 9154 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 9155 | return; |
| 9156 | } |
| 9157 | } |
| 9158 | } |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 9159 | // FIXME: For generic lambda parameters, check if the function is a lambda |
| 9160 | // call operator, and if so, emit a prettier and more informative |
| 9161 | // diagnostic that mentions 'auto' and lambda in addition to |
| 9162 | // (or instead of?) the canonical template type parameters. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9163 | S.Diag(Templated->getLocation(), |
| 9164 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 9165 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 9166 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 9167 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9168 | // TODO: diagnose these individually, then kill off |
| 9169 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 9170 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9171 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 9172 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9173 | return; |
| 9174 | } |
| 9175 | } |
| 9176 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9177 | /// Diagnose a failed template-argument deduction, for function calls. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9178 | static void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 9179 | unsigned NumArgs) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9180 | unsigned TDK = Cand->DeductionFailure.Result; |
| 9181 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 9182 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 9183 | return; |
| 9184 | } |
| 9185 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 9186 | Cand->DeductionFailure, NumArgs); |
| 9187 | } |
| 9188 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9189 | /// CUDA: diagnose an invalid call across targets. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9190 | static void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9191 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 9192 | FunctionDecl *Callee = Cand->Function; |
| 9193 | |
| 9194 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 9195 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 9196 | |
| 9197 | std::string FnDesc; |
| 9198 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 9199 | |
| 9200 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9201 | << (unsigned)FnKind << CalleeTarget << CallerTarget; |
| 9202 | |
| 9203 | // This could be an implicit constructor for which we could not infer the |
| 9204 | // target due to a collsion. Diagnose that case. |
| 9205 | CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Callee); |
| 9206 | if (Meth != nullptr && Meth->isImplicit()) { |
| 9207 | CXXRecordDecl *ParentClass = Meth->getParent(); |
| 9208 | Sema::CXXSpecialMember CSM; |
| 9209 | |
| 9210 | switch (FnKind) { |
| 9211 | default: |
| 9212 | return; |
| 9213 | case oc_implicit_default_constructor: |
| 9214 | CSM = Sema::CXXDefaultConstructor; |
| 9215 | break; |
| 9216 | case oc_implicit_copy_constructor: |
| 9217 | CSM = Sema::CXXCopyConstructor; |
| 9218 | break; |
| 9219 | case oc_implicit_move_constructor: |
| 9220 | CSM = Sema::CXXMoveConstructor; |
| 9221 | break; |
| 9222 | case oc_implicit_copy_assignment: |
| 9223 | CSM = Sema::CXXCopyAssignment; |
| 9224 | break; |
| 9225 | case oc_implicit_move_assignment: |
| 9226 | CSM = Sema::CXXMoveAssignment; |
| 9227 | break; |
| 9228 | }; |
| 9229 | |
| 9230 | bool ConstRHS = false; |
| 9231 | if (Meth->getNumParams()) { |
| 9232 | if (const ReferenceType *RT = |
| 9233 | Meth->getParamDecl(0)->getType()->getAs<ReferenceType>()) { |
| 9234 | ConstRHS = RT->getPointeeType().isConstQualified(); |
| 9235 | } |
| 9236 | } |
| 9237 | |
| 9238 | S.inferCUDATargetForImplicitSpecialMember(ParentClass, CSM, Meth, |
| 9239 | /* ConstRHS */ ConstRHS, |
| 9240 | /* Diagnose */ true); |
| 9241 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9244 | static void DiagnoseFailedEnableIfAttr(Sema &S, OverloadCandidate *Cand) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9245 | FunctionDecl *Callee = Cand->Function; |
| 9246 | EnableIfAttr *Attr = static_cast<EnableIfAttr*>(Cand->DeductionFailure.Data); |
| 9247 | |
| 9248 | S.Diag(Callee->getLocation(), |
| 9249 | diag::note_ovl_candidate_disabled_by_enable_if_attr) |
| 9250 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); |
| 9251 | } |
| 9252 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9253 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 9254 | /// already generated a primary error at the call site. |
| 9255 | /// |
| 9256 | /// It really does need to be a single diagnostic with its caret |
| 9257 | /// pointed at the candidate declaration. Yes, this creates some |
| 9258 | /// major challenges of technical writing. Yes, this makes pointing |
| 9259 | /// out problems with specific arguments quite awkward. It's still |
| 9260 | /// better than generating twenty screens of text for every failed |
| 9261 | /// overload. |
| 9262 | /// |
| 9263 | /// It would be great to be able to express per-candidate problems |
| 9264 | /// more richly for those diagnostic clients that cared, but we'd |
| 9265 | /// still have to be just as careful with the default diagnostics. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9266 | static void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 9267 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9268 | FunctionDecl *Fn = Cand->Function; |
| 9269 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9270 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 9271 | if (Cand->Viable && (Fn->isDeleted() || |
| 9272 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9273 | std::string FnDesc; |
| 9274 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9275 | |
| 9276 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 9277 | << FnKind << FnDesc |
| 9278 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9279 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9280 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9283 | // We don't really have anything else to say about viable candidates. |
| 9284 | if (Cand->Viable) { |
| 9285 | S.NoteOverloadCandidate(Fn); |
| 9286 | return; |
| 9287 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9288 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9289 | switch (Cand->FailureKind) { |
| 9290 | case ovl_fail_too_many_arguments: |
| 9291 | case ovl_fail_too_few_arguments: |
| 9292 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9293 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9294 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9295 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9296 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 9297 | case ovl_fail_illegal_constructor: { |
| 9298 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_illegal_constructor) |
| 9299 | << (Fn->getPrimaryTemplate() ? 1 : 0); |
| 9300 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 9301 | return; |
| 9302 | } |
| 9303 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9304 | case ovl_fail_trivial_conversion: |
| 9305 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 9306 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9307 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9308 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9309 | case ovl_fail_bad_conversion: { |
| 9310 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9311 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9312 | if (Cand->Conversions[I].isBad()) |
| 9313 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9314 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9315 | // FIXME: this currently happens when we're called from SemaInit |
| 9316 | // when user-conversion overload fails. Figure out how to handle |
| 9317 | // those conditions and diagnose them well. |
| 9318 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9319 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9320 | |
| 9321 | case ovl_fail_bad_target: |
| 9322 | return DiagnoseBadTarget(S, Cand); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9323 | |
| 9324 | case ovl_fail_enable_if: |
| 9325 | return DiagnoseFailedEnableIfAttr(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9326 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9327 | } |
| 9328 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9329 | static void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9330 | // Desugar the type of the surrogate down to a function type, |
| 9331 | // retaining as many typedefs as possible while still showing |
| 9332 | // the function type (and, therefore, its parameter types). |
| 9333 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 9334 | bool isLValueReference = false; |
| 9335 | bool isRValueReference = false; |
| 9336 | bool isPointer = false; |
| 9337 | if (const LValueReferenceType *FnTypeRef = |
| 9338 | FnType->getAs<LValueReferenceType>()) { |
| 9339 | FnType = FnTypeRef->getPointeeType(); |
| 9340 | isLValueReference = true; |
| 9341 | } else if (const RValueReferenceType *FnTypeRef = |
| 9342 | FnType->getAs<RValueReferenceType>()) { |
| 9343 | FnType = FnTypeRef->getPointeeType(); |
| 9344 | isRValueReference = true; |
| 9345 | } |
| 9346 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 9347 | FnType = FnTypePtr->getPointeeType(); |
| 9348 | isPointer = true; |
| 9349 | } |
| 9350 | // Desugar down to a function type. |
| 9351 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 9352 | // Reconstruct the pointer/reference as appropriate. |
| 9353 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 9354 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 9355 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 9356 | |
| 9357 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 9358 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9359 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9360 | } |
| 9361 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9362 | static void NoteBuiltinOperatorCandidate(Sema &S, StringRef Opc, |
| 9363 | SourceLocation OpLoc, |
| 9364 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9365 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9366 | std::string TypeStr("operator"); |
| 9367 | TypeStr += Opc; |
| 9368 | TypeStr += "("; |
| 9369 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9370 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9371 | TypeStr += ")"; |
| 9372 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 9373 | } else { |
| 9374 | TypeStr += ", "; |
| 9375 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 9376 | TypeStr += ")"; |
| 9377 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 9378 | } |
| 9379 | } |
| 9380 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9381 | static void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 9382 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9383 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9384 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 9385 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9386 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 9387 | if (!ICS.isAmbiguous()) continue; |
| 9388 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9389 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9390 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9391 | } |
| 9392 | } |
| 9393 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9394 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9395 | if (Cand->Function) |
| 9396 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 9397 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9398 | return Cand->Surrogate->getLocation(); |
| 9399 | return SourceLocation(); |
| 9400 | } |
| 9401 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9402 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 9403 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9404 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9405 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 9406 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 9407 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9408 | case Sema::TDK_Incomplete: |
| 9409 | return 1; |
| 9410 | |
| 9411 | case Sema::TDK_Underqualified: |
| 9412 | case Sema::TDK_Inconsistent: |
| 9413 | return 2; |
| 9414 | |
| 9415 | case Sema::TDK_SubstitutionFailure: |
| 9416 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 9417 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9418 | return 3; |
| 9419 | |
| 9420 | case Sema::TDK_InstantiationDepth: |
| 9421 | case Sema::TDK_FailedOverloadResolution: |
| 9422 | return 4; |
| 9423 | |
| 9424 | case Sema::TDK_InvalidExplicitArguments: |
| 9425 | return 5; |
| 9426 | |
| 9427 | case Sema::TDK_TooManyArguments: |
| 9428 | case Sema::TDK_TooFewArguments: |
| 9429 | return 6; |
| 9430 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 9431 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9434 | namespace { |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9435 | struct CompareOverloadCandidatesForDisplay { |
| 9436 | Sema &S; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9437 | size_t NumArgs; |
| 9438 | |
| 9439 | CompareOverloadCandidatesForDisplay(Sema &S, size_t nArgs) |
| 9440 | : S(S), NumArgs(nArgs) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9441 | |
| 9442 | bool operator()(const OverloadCandidate *L, |
| 9443 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 9444 | // Fast-path this check. |
| 9445 | if (L == R) return false; |
| 9446 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9447 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9448 | if (L->Viable) { |
| 9449 | if (!R->Viable) return true; |
| 9450 | |
| 9451 | // TODO: introduce a tri-valued comparison for overload |
| 9452 | // candidates. Would be more worthwhile if we had a sort |
| 9453 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9454 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 9455 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9456 | } else if (R->Viable) |
| 9457 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9458 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9459 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9460 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9461 | // Criteria by which we can sort non-viable candidates: |
| 9462 | if (!L->Viable) { |
| 9463 | // 1. Arity mismatches come after other candidates. |
| 9464 | if (L->FailureKind == ovl_fail_too_many_arguments || |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9465 | L->FailureKind == ovl_fail_too_few_arguments) { |
| 9466 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 9467 | R->FailureKind == ovl_fail_too_few_arguments) { |
| 9468 | int LDist = std::abs((int)L->getNumParams() - (int)NumArgs); |
| 9469 | int RDist = std::abs((int)R->getNumParams() - (int)NumArgs); |
| 9470 | if (LDist == RDist) { |
| 9471 | if (L->FailureKind == R->FailureKind) |
| 9472 | // Sort non-surrogates before surrogates. |
| 9473 | return !L->IsSurrogate && R->IsSurrogate; |
| 9474 | // Sort candidates requiring fewer parameters than there were |
| 9475 | // arguments given after candidates requiring more parameters |
| 9476 | // than there were arguments given. |
| 9477 | return L->FailureKind == ovl_fail_too_many_arguments; |
| 9478 | } |
| 9479 | return LDist < RDist; |
| 9480 | } |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9481 | return false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9482 | } |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9483 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 9484 | R->FailureKind == ovl_fail_too_few_arguments) |
| 9485 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9486 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9487 | // 2. Bad conversions come first and are ordered by the number |
| 9488 | // of bad conversions and quality of good conversions. |
| 9489 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 9490 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 9491 | return true; |
| 9492 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9493 | // The conversion that can be fixed with a smaller number of changes, |
| 9494 | // comes first. |
| 9495 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 9496 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 9497 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 9498 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 9499 | if (numLFixes != numRFixes) { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 9500 | return numLFixes < numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 9501 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9502 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9503 | // If there's any ordering between the defined conversions... |
| 9504 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9505 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9506 | |
| 9507 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 9508 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9509 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9510 | switch (CompareImplicitConversionSequences(S, |
| 9511 | L->Conversions[I], |
| 9512 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9513 | case ImplicitConversionSequence::Better: |
| 9514 | leftBetter++; |
| 9515 | break; |
| 9516 | |
| 9517 | case ImplicitConversionSequence::Worse: |
| 9518 | leftBetter--; |
| 9519 | break; |
| 9520 | |
| 9521 | case ImplicitConversionSequence::Indistinguishable: |
| 9522 | break; |
| 9523 | } |
| 9524 | } |
| 9525 | if (leftBetter > 0) return true; |
| 9526 | if (leftBetter < 0) return false; |
| 9527 | |
| 9528 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 9529 | return false; |
| 9530 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9531 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 9532 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 9533 | return true; |
| 9534 | |
| 9535 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9536 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 9537 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 9538 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 9539 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9540 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9541 | // TODO: others? |
| 9542 | } |
| 9543 | |
| 9544 | // Sort everything else by location. |
| 9545 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9546 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9547 | |
| 9548 | // Put candidates without locations (e.g. builtins) at the end. |
| 9549 | if (LLoc.isInvalid()) return false; |
| 9550 | if (RLoc.isInvalid()) return true; |
| 9551 | |
| 9552 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9553 | } |
| 9554 | }; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9555 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9556 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9557 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9558 | /// computes up to the first. Produces the FixIt set if possible. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9559 | static void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 9560 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9561 | assert(!Cand->Viable); |
| 9562 | |
| 9563 | // Don't do anything on failures other than bad conversion. |
| 9564 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9565 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9566 | // We only want the FixIts if all the arguments can be corrected. |
| 9567 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9568 | // Use a implicit copy initialization to check conversion fixes. |
| 9569 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9570 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9571 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9572 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9573 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9574 | while (true) { |
| 9575 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9576 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9577 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9578 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9579 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9580 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
| 9583 | if (ConvIdx == ConvCount) |
| 9584 | return; |
| 9585 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9586 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9587 | "remaining conversion is initialized?"); |
| 9588 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9589 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9590 | // operation somehow. |
| 9591 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9592 | |
| 9593 | const FunctionProtoType* Proto; |
| 9594 | unsigned ArgIdx = ConvIdx; |
| 9595 | |
| 9596 | if (Cand->IsSurrogate) { |
| 9597 | QualType ConvType |
| 9598 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9599 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9600 | ConvType = ConvPtrType->getPointeeType(); |
| 9601 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9602 | ArgIdx--; |
| 9603 | } else if (Cand->Function) { |
| 9604 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9605 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9606 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9607 | ArgIdx--; |
| 9608 | } else { |
| 9609 | // Builtin binary operator with a bad first conversion. |
| 9610 | assert(ConvCount <= 3); |
| 9611 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9612 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9613 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9614 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9615 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9616 | /*InOverloadResolution*/ true, |
| 9617 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9618 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9619 | return; |
| 9620 | } |
| 9621 | |
| 9622 | // Fill in the rest of the conversions. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9623 | unsigned NumParams = Proto->getNumParams(); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9624 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9625 | if (ArgIdx < NumParams) { |
| 9626 | Cand->Conversions[ConvIdx] = TryCopyInitialization( |
| 9627 | S, Args[ArgIdx], Proto->getParamType(ArgIdx), SuppressUserConversions, |
| 9628 | /*InOverloadResolution=*/true, |
| 9629 | /*AllowObjCWritebackConversion=*/ |
| 9630 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9631 | // Store the FixIt in the candidate if it exists. |
| 9632 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9633 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9634 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9635 | else |
| 9636 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9637 | } |
| 9638 | } |
| 9639 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9640 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9641 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9642 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9643 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9644 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9645 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9646 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9647 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9648 | // Sort the candidates by viability and position. Sorting directly would |
| 9649 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9650 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9651 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9652 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9653 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9654 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9655 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9656 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9657 | if (Cand->Function || Cand->IsSurrogate) |
| 9658 | Cands.push_back(Cand); |
| 9659 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9660 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9661 | } |
| 9662 | } |
| 9663 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9664 | std::sort(Cands.begin(), Cands.end(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9665 | CompareOverloadCandidatesForDisplay(S, Args.size())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9666 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9667 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9668 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9669 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9670 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9671 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9672 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9673 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9674 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9675 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9676 | // the user with. FIXME: This limit should depend on details of the |
| 9677 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9678 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9679 | break; |
| 9680 | } |
| 9681 | ++CandsShown; |
| 9682 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9683 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9684 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9685 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9686 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9687 | else { |
| 9688 | assert(Cand->Viable && |
| 9689 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9690 | // Generally we only see ambiguities including viable builtin |
| 9691 | // operators if overload resolution got screwed up by an |
| 9692 | // ambiguous user-defined conversion. |
| 9693 | // |
| 9694 | // FIXME: It's quite possible for different conversions to see |
| 9695 | // different ambiguities, though. |
| 9696 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9697 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9698 | ReportedAmbiguousConversions = true; |
| 9699 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9700 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9701 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9702 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9703 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9704 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9705 | |
| 9706 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9707 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9708 | } |
| 9709 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9710 | static SourceLocation |
| 9711 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9712 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9713 | : SourceLocation(); |
| 9714 | } |
| 9715 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9716 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9717 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9718 | Sema &S; |
| 9719 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9720 | |
| 9721 | bool operator()(const TemplateSpecCandidate *L, |
| 9722 | const TemplateSpecCandidate *R) { |
| 9723 | // Fast-path this check. |
| 9724 | if (L == R) |
| 9725 | return false; |
| 9726 | |
| 9727 | // Assuming that both candidates are not matches... |
| 9728 | |
| 9729 | // Sort by the ranking of deduction failures. |
| 9730 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9731 | return RankDeductionFailure(L->DeductionFailure) < |
| 9732 | RankDeductionFailure(R->DeductionFailure); |
| 9733 | |
| 9734 | // Sort everything else by location. |
| 9735 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9736 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9737 | |
| 9738 | // Put candidates without locations (e.g. builtins) at the end. |
| 9739 | if (LLoc.isInvalid()) |
| 9740 | return false; |
| 9741 | if (RLoc.isInvalid()) |
| 9742 | return true; |
| 9743 | |
| 9744 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9745 | } |
| 9746 | }; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9747 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9748 | |
| 9749 | /// Diagnose a template argument deduction failure. |
| 9750 | /// We are treating these failures as overload failures due to bad |
| 9751 | /// deductions. |
| 9752 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9753 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9754 | DeductionFailure, /*NumArgs=*/0); |
| 9755 | } |
| 9756 | |
| 9757 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9758 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9759 | i->DeductionFailure.Destroy(); |
| 9760 | } |
| 9761 | } |
| 9762 | |
| 9763 | void TemplateSpecCandidateSet::clear() { |
| 9764 | destroyCandidates(); |
| 9765 | Candidates.clear(); |
| 9766 | } |
| 9767 | |
| 9768 | /// NoteCandidates - When no template specialization match is found, prints |
| 9769 | /// diagnostic messages containing the non-matching specializations that form |
| 9770 | /// the candidate set. |
| 9771 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9772 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9773 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9774 | // Sort the candidates by position (assuming no candidate is a match). |
| 9775 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9776 | // and sort those. |
| 9777 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9778 | Cands.reserve(size()); |
| 9779 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9780 | if (Cand->Specialization) |
| 9781 | Cands.push_back(Cand); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9782 | // Otherwise, this is a non-matching builtin candidate. We do not, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9783 | // in general, want to list every possible builtin candidate. |
| 9784 | } |
| 9785 | |
| 9786 | std::sort(Cands.begin(), Cands.end(), |
| 9787 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9788 | |
| 9789 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9790 | // for generalization purposes (?). |
| 9791 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9792 | |
| 9793 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9794 | unsigned CandsShown = 0; |
| 9795 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9796 | TemplateSpecCandidate *Cand = *I; |
| 9797 | |
| 9798 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9799 | // the user with. FIXME: This limit should depend on details of the |
| 9800 | // candidate list. |
| 9801 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9802 | break; |
| 9803 | ++CandsShown; |
| 9804 | |
| 9805 | assert(Cand->Specialization && |
| 9806 | "Non-matching built-in candidates are not added to Cands."); |
| 9807 | Cand->NoteDeductionFailure(S); |
| 9808 | } |
| 9809 | |
| 9810 | if (I != E) |
| 9811 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9812 | } |
| 9813 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9814 | // [PossiblyAFunctionType] --> [Return] |
| 9815 | // NonFunctionType --> NonFunctionType |
| 9816 | // R (A) --> R(A) |
| 9817 | // R (*)(A) --> R (A) |
| 9818 | // R (&)(A) --> R (A) |
| 9819 | // R (S::*)(A) --> R (A) |
| 9820 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9821 | QualType Ret = PossiblyAFunctionType; |
| 9822 | if (const PointerType *ToTypePtr = |
| 9823 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9824 | Ret = ToTypePtr->getPointeeType(); |
| 9825 | else if (const ReferenceType *ToTypeRef = |
| 9826 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9827 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9828 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9829 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9830 | Ret = MemTypePtr->getPointeeType(); |
| 9831 | Ret = |
| 9832 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9833 | return Ret; |
| 9834 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9835 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9836 | namespace { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9837 | // A helper class to help with address of function resolution |
| 9838 | // - allows us to avoid passing around all those ugly parameters |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9839 | class AddressOfFunctionResolver { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9840 | Sema& S; |
| 9841 | Expr* SourceExpr; |
| 9842 | const QualType& TargetType; |
| 9843 | QualType TargetFunctionType; // Extracted function type from target type |
| 9844 | |
| 9845 | bool Complain; |
| 9846 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9847 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9848 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9849 | bool TargetTypeIsNonStaticMemberFunction; |
| 9850 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9851 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9852 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9853 | OverloadExpr::FindResult OvlExprInfo; |
| 9854 | OverloadExpr *OvlExpr; |
| 9855 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9856 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9857 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9858 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9859 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9860 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9861 | const QualType &TargetType, bool Complain) |
| 9862 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9863 | Complain(Complain), Context(S.getASTContext()), |
| 9864 | TargetTypeIsNonStaticMemberFunction( |
| 9865 | !!TargetType->getAs<MemberPointerType>()), |
| 9866 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9867 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9868 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9869 | OvlExpr(OvlExprInfo.Expression), |
| 9870 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9871 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9872 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9873 | if (TargetFunctionType->isFunctionType()) { |
| 9874 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9875 | if (!UME->isImplicitAccess() && |
| 9876 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9877 | StaticMemberFunctionFromBoundPointer = true; |
| 9878 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9879 | DeclAccessPair dap; |
| 9880 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9881 | OvlExpr, false, &dap)) { |
| 9882 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9883 | if (!Method->isStatic()) { |
| 9884 | // If the target type is a non-function type and the function found |
| 9885 | // is a non-static member function, pretend as if that was the |
| 9886 | // target, it's the only possible type to end up with. |
| 9887 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9888 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9889 | // And skip adding the function if its not in the proper form. |
| 9890 | // We'll diagnose this due to an empty set of functions. |
| 9891 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9892 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9893 | } |
| 9894 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9895 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9896 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9897 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9898 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9899 | |
| 9900 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9901 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9902 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9903 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9904 | // C++ [over.over]p4: |
| 9905 | // If more than one function is selected, [...] |
| 9906 | if (Matches.size() > 1) { |
| 9907 | if (FoundNonTemplateFunction) |
| 9908 | EliminateAllTemplateMatches(); |
| 9909 | else |
| 9910 | EliminateAllExceptMostSpecializedTemplate(); |
| 9911 | } |
| 9912 | } |
| 9913 | } |
| 9914 | |
| 9915 | private: |
| 9916 | bool isTargetTypeAFunction() const { |
| 9917 | return TargetFunctionType->isFunctionType(); |
| 9918 | } |
| 9919 | |
| 9920 | // [ToType] [Return] |
| 9921 | |
| 9922 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9923 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9924 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9925 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9926 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9927 | } |
| 9928 | |
| 9929 | // return true if any matching specializations were found |
| 9930 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9931 | const DeclAccessPair& CurAccessFunPair) { |
| 9932 | if (CXXMethodDecl *Method |
| 9933 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9934 | // Skip non-static function templates when converting to pointer, and |
| 9935 | // static when converting to member pointer. |
| 9936 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9937 | return false; |
| 9938 | } |
| 9939 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9940 | return false; |
| 9941 | |
| 9942 | // C++ [over.over]p2: |
| 9943 | // If the name is a function template, template argument deduction is |
| 9944 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9945 | // resulting template argument list is used to generate a single |
| 9946 | // function template specialization, which is added to the set of |
| 9947 | // overloaded functions considered. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9948 | FunctionDecl *Specialization = nullptr; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9949 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9950 | if (Sema::TemplateDeductionResult Result |
| 9951 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9952 | &OvlExplicitTemplateArgs, |
| 9953 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9954 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9955 | // Make a note of the failed deduction for diagnostics. |
| 9956 | FailedCandidates.addCandidate() |
| 9957 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9958 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9959 | return false; |
| 9960 | } |
| 9961 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9962 | // Template argument deduction ensures that we have an exact match or |
| 9963 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9964 | // This function template specicalization works. |
| 9965 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9966 | assert(S.isSameOrCompatibleFunctionType( |
| 9967 | Context.getCanonicalType(Specialization->getType()), |
| 9968 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9969 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9970 | return true; |
| 9971 | } |
| 9972 | |
| 9973 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9974 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9975 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9976 | // Skip non-static functions when converting to pointer, and static |
| 9977 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9978 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9979 | return false; |
| 9980 | } |
| 9981 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9982 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9983 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9984 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9985 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9986 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9987 | if (!Caller->isImplicit() && S.CheckCUDATarget(Caller, FunDecl)) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9988 | return false; |
| 9989 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9990 | // If any candidate has a placeholder return type, trigger its deduction |
| 9991 | // now. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9992 | if (S.getLangOpts().CPlusPlus14 && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9993 | FunDecl->getReturnType()->isUndeducedType() && |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9994 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9995 | return false; |
| 9996 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9997 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9998 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9999 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 10000 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 10001 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10002 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 10003 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 10004 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10005 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 10006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10007 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10008 | |
| 10009 | return false; |
| 10010 | } |
| 10011 | |
| 10012 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 10013 | bool Ret = false; |
| 10014 | |
| 10015 | // If the overload expression doesn't have the form of a pointer to |
| 10016 | // member, don't try to convert it to a pointer-to-member type. |
| 10017 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 10018 | return false; |
| 10019 | |
| 10020 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 10021 | E = OvlExpr->decls_end(); |
| 10022 | I != E; ++I) { |
| 10023 | // Look through any using declarations to find the underlying function. |
| 10024 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 10025 | |
| 10026 | // C++ [over.over]p3: |
| 10027 | // Non-member functions and static member functions match |
| 10028 | // targets of type "pointer-to-function" or "reference-to-function." |
| 10029 | // Nonstatic member functions match targets of |
| 10030 | // type "pointer-to-member-function." |
| 10031 | // Note that according to DR 247, the containing class does not matter. |
| 10032 | if (FunctionTemplateDecl *FunctionTemplate |
| 10033 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 10034 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 10035 | Ret = true; |
| 10036 | } |
| 10037 | // If we have explicit template arguments supplied, skip non-templates. |
| 10038 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 10039 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 10040 | Ret = true; |
| 10041 | } |
| 10042 | assert(Ret || Matches.empty()); |
| 10043 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10044 | } |
| 10045 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10046 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 10047 | // [...] and any given function template specialization F1 is |
| 10048 | // eliminated if the set contains a second function template |
| 10049 | // specialization whose function template is more specialized |
| 10050 | // than the function template of F1 according to the partial |
| 10051 | // ordering rules of 14.5.5.2. |
| 10052 | |
| 10053 | // The algorithm specified above is quadratic. We instead use a |
| 10054 | // two-pass algorithm (similar to the one used to identify the |
| 10055 | // best viable function in an overload set) that identifies the |
| 10056 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10057 | |
| 10058 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 10059 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 10060 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10061 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10062 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 10063 | // here, since the no_viable diagnostic has index 0. |
| 10064 | UnresolvedSetIterator Result = S.getMostSpecialized( |
Richard Smith | 4ad09e6 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 10065 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10066 | SourceExpr->getLocStart(), S.PDiag(), |
| 10067 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 10068 | .second->getDeclName(), |
| 10069 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 10070 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10071 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10072 | if (Result != MatchesCopy.end()) { |
| 10073 | // Make it the first and only element |
| 10074 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 10075 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 10076 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10077 | } |
| 10078 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10079 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10080 | void EliminateAllTemplateMatches() { |
| 10081 | // [...] any function template specializations in the set are |
| 10082 | // eliminated if the set also contains a non-template function, [...] |
| 10083 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10084 | if (Matches[I].second->getPrimaryTemplate() == nullptr) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10085 | ++I; |
| 10086 | else { |
| 10087 | Matches[I] = Matches[--N]; |
| 10088 | Matches.set_size(N); |
| 10089 | } |
| 10090 | } |
| 10091 | } |
| 10092 | |
| 10093 | public: |
| 10094 | void ComplainNoMatchesFound() const { |
| 10095 | assert(Matches.empty()); |
| 10096 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 10097 | << OvlExpr->getName() << TargetFunctionType |
| 10098 | << OvlExpr->getSourceRange(); |
Richard Smith | 72a36a1 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 10099 | if (FailedCandidates.empty()) |
| 10100 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 10101 | else { |
| 10102 | // We have some deduction failure messages. Use them to diagnose |
| 10103 | // the function templates, and diagnose the non-template candidates |
| 10104 | // normally. |
| 10105 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 10106 | IEnd = OvlExpr->decls_end(); |
| 10107 | I != IEnd; ++I) |
| 10108 | if (FunctionDecl *Fun = |
| 10109 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 10110 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 10111 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 10112 | } |
| 10113 | } |
| 10114 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10115 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 10116 | return TargetTypeIsNonStaticMemberFunction && |
| 10117 | !OvlExprInfo.HasFormOfMemberPointer; |
| 10118 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10119 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10120 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 10121 | // TODO: Should we condition this on whether any functions might |
| 10122 | // have matched, or is it more appropriate to do that in callers? |
| 10123 | // TODO: a fixit wouldn't hurt. |
| 10124 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 10125 | << TargetType << OvlExpr->getSourceRange(); |
| 10126 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10127 | |
| 10128 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 10129 | return StaticMemberFunctionFromBoundPointer; |
| 10130 | } |
| 10131 | |
| 10132 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 10133 | S.Diag(OvlExpr->getLocStart(), |
| 10134 | diag::err_invalid_form_pointer_member_function) |
| 10135 | << OvlExpr->getSourceRange(); |
| 10136 | } |
| 10137 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10138 | void ComplainOfInvalidConversion() const { |
| 10139 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 10140 | << OvlExpr->getName() << TargetType; |
| 10141 | } |
| 10142 | |
| 10143 | void ComplainMultipleMatchesFound() const { |
| 10144 | assert(Matches.size() > 1); |
| 10145 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 10146 | << OvlExpr->getName() |
| 10147 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 10148 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10149 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 10150 | |
| 10151 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 10152 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10153 | int getNumMatches() const { return Matches.size(); } |
| 10154 | |
| 10155 | FunctionDecl* getMatchingFunctionDecl() const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10156 | if (Matches.size() != 1) return nullptr; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10157 | return Matches[0].second; |
| 10158 | } |
| 10159 | |
| 10160 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10161 | if (Matches.size() != 1) return nullptr; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10162 | return &Matches[0].first; |
| 10163 | } |
| 10164 | }; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10165 | } |
| 10166 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10167 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 10168 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 10169 | /// expression with overloaded function type and @p ToType is the type |
| 10170 | /// we're trying to resolve to. For example: |
| 10171 | /// |
| 10172 | /// @code |
| 10173 | /// int f(double); |
| 10174 | /// int f(int); |
| 10175 | /// |
| 10176 | /// int (*pfd)(double) = f; // selects f(double) |
| 10177 | /// @endcode |
| 10178 | /// |
| 10179 | /// This routine returns the resulting FunctionDecl if it could be |
| 10180 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 10181 | /// routine will emit diagnostics if there is an error. |
| 10182 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 10183 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 10184 | QualType TargetType, |
| 10185 | bool Complain, |
| 10186 | DeclAccessPair &FoundResult, |
| 10187 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10188 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 10189 | |
| 10190 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 10191 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10192 | int NumMatches = Resolver.getNumMatches(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10193 | FunctionDecl *Fn = nullptr; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 10194 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10195 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 10196 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 10197 | else |
| 10198 | Resolver.ComplainNoMatchesFound(); |
| 10199 | } |
| 10200 | else if (NumMatches > 1 && Complain) |
| 10201 | Resolver.ComplainMultipleMatchesFound(); |
| 10202 | else if (NumMatches == 1) { |
| 10203 | Fn = Resolver.getMatchingFunctionDecl(); |
| 10204 | assert(Fn); |
| 10205 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10206 | if (Complain) { |
| 10207 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 10208 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 10209 | else |
| 10210 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 10211 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 10212 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 10213 | |
| 10214 | if (pHadMultipleCandidates) |
| 10215 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10216 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10217 | } |
| 10218 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10219 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10220 | /// resolve that overloaded function expression down to a single function. |
| 10221 | /// |
| 10222 | /// This routine can only resolve template-ids that refer to a single function |
| 10223 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10224 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10225 | /// as described in C++0x [temp.arg.explicit]p3. |
Alp Toker | 8b40772 | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 10226 | /// |
| 10227 | /// If no template-ids are found, no diagnostics are emitted and NULL is |
| 10228 | /// returned. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10229 | FunctionDecl * |
| 10230 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 10231 | bool Complain, |
| 10232 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10233 | // C++ [over.over]p1: |
| 10234 | // [...] [Note: any redundant set of parentheses surrounding the |
| 10235 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10236 | // C++ [over.over]p1: |
| 10237 | // [...] The overloaded function name can be preceded by the & |
| 10238 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10239 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10240 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10241 | if (!ovl->hasExplicitTemplateArgs()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10242 | return nullptr; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 10243 | |
| 10244 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10245 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10246 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10247 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10248 | // Look through all of the overloaded functions, searching for one |
| 10249 | // whose type matches exactly. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10250 | FunctionDecl *Matched = nullptr; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10251 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 10252 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10253 | // C++0x [temp.arg.explicit]p3: |
| 10254 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10255 | // where deduction is not done, if a template argument list is |
| 10256 | // specified and it, along with any default template arguments, |
| 10257 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10258 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 10259 | FunctionTemplateDecl *FunctionTemplate |
| 10260 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10261 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10262 | // C++ [over.over]p2: |
| 10263 | // If the name is a function template, template argument deduction is |
| 10264 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 10265 | // resulting template argument list is used to generate a single |
| 10266 | // function template specialization, which is added to the set of |
| 10267 | // overloaded functions considered. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10268 | FunctionDecl *Specialization = nullptr; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10269 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10270 | if (TemplateDeductionResult Result |
| 10271 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 10272 | Specialization, Info, |
| 10273 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10274 | // Make a note of the failed deduction for diagnostics. |
| 10275 | // TODO: Actually use the failed-deduction info? |
| 10276 | FailedCandidates.addCandidate() |
| 10277 | .set(FunctionTemplate->getTemplatedDecl(), |
| 10278 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10279 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10280 | } |
| 10281 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10282 | assert(Specialization && "no specialization and no error?"); |
| 10283 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10284 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10285 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10286 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10287 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 10288 | << ovl->getName(); |
| 10289 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10290 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10291 | return nullptr; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10292 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10293 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10294 | Matched = Specialization; |
| 10295 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10296 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10297 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10298 | if (Matched && getLangOpts().CPlusPlus14 && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10299 | Matched->getReturnType()->isUndeducedType() && |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 10300 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10301 | return nullptr; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 10302 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10303 | return Matched; |
| 10304 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10305 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10306 | |
| 10307 | |
| 10308 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10309 | // Resolve and fix an overloaded expression that can be resolved |
| 10310 | // because it identifies a single function template specialization. |
| 10311 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10312 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10313 | // |
| 10314 | // Return true if it was logically possible to so resolve the |
| 10315 | // expression, regardless of whether or not it succeeded. Always |
| 10316 | // returns true if 'complain' is set. |
| 10317 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 10318 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 10319 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10320 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10321 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10322 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10323 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10324 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10325 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10326 | DeclAccessPair found; |
| 10327 | ExprResult SingleFunctionExpression; |
| 10328 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 10329 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10330 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10331 | SrcExpr = ExprError(); |
| 10332 | return true; |
| 10333 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10334 | |
| 10335 | // It is only correct to resolve to an instance method if we're |
| 10336 | // resolving a form that's permitted to be a pointer to member. |
| 10337 | // Otherwise we'll end up making a bound member expression, which |
| 10338 | // is illegal in all the contexts we resolve like this. |
| 10339 | if (!ovl.HasFormOfMemberPointer && |
| 10340 | isa<CXXMethodDecl>(fn) && |
| 10341 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10342 | if (!complain) return false; |
| 10343 | |
| 10344 | Diag(ovl.Expression->getExprLoc(), |
| 10345 | diag::err_bound_member_function) |
| 10346 | << 0 << ovl.Expression->getSourceRange(); |
| 10347 | |
| 10348 | // TODO: I believe we only end up here if there's a mix of |
| 10349 | // static and non-static candidates (otherwise the expression |
| 10350 | // would have 'bound member' type, not 'overload' type). |
| 10351 | // Ideally we would note which candidate was chosen and why |
| 10352 | // the static candidates were rejected. |
| 10353 | SrcExpr = ExprError(); |
| 10354 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10355 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10356 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 10357 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10358 | SingleFunctionExpression = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10359 | FixOverloadedFunctionReference(SrcExpr.get(), found, fn); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10360 | |
| 10361 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10362 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10363 | SingleFunctionExpression = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10364 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.get()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10365 | if (SingleFunctionExpression.isInvalid()) { |
| 10366 | SrcExpr = ExprError(); |
| 10367 | return true; |
| 10368 | } |
| 10369 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10370 | } |
| 10371 | |
| 10372 | if (!SingleFunctionExpression.isUsable()) { |
| 10373 | if (complain) { |
| 10374 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 10375 | << ovl.Expression->getName() |
| 10376 | << DestTypeForComplaining |
| 10377 | << OpRangeForComplaining |
| 10378 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10379 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 10380 | |
| 10381 | SrcExpr = ExprError(); |
| 10382 | return true; |
| 10383 | } |
| 10384 | |
| 10385 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10386 | } |
| 10387 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10388 | SrcExpr = SingleFunctionExpression; |
| 10389 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10390 | } |
| 10391 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10392 | /// \brief Add a single candidate to the overload set. |
| 10393 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10394 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 10395 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10396 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10397 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10398 | bool PartialOverloading, |
| 10399 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10400 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10401 | if (isa<UsingShadowDecl>(Callee)) |
| 10402 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 10403 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10404 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10405 | if (ExplicitTemplateArgs) { |
| 10406 | assert(!KnownValid && "Explicit template arguments?"); |
| 10407 | return; |
| 10408 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 10409 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, |
| 10410 | /*SuppressUsedConversions=*/false, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10411 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10412 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10413 | } |
| 10414 | |
| 10415 | if (FunctionTemplateDecl *FuncTemplate |
| 10416 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10417 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 10418 | ExplicitTemplateArgs, Args, CandidateSet, |
| 10419 | /*SuppressUsedConversions=*/false, |
| 10420 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10421 | return; |
| 10422 | } |
| 10423 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10424 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10425 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10426 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10427 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 10428 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10429 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10430 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10431 | OverloadCandidateSet &CandidateSet, |
| 10432 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10433 | |
| 10434 | #ifndef NDEBUG |
| 10435 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 10436 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10437 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10438 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 10439 | // and let Y be the lookup set produced by argument dependent |
| 10440 | // lookup (defined as follows). If X contains |
| 10441 | // |
| 10442 | // -- a declaration of a class member, or |
| 10443 | // |
| 10444 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10445 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10446 | // |
| 10447 | // -- a declaration that is neither a function or a function |
| 10448 | // template |
| 10449 | // |
| 10450 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10451 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10452 | if (ULE->requiresADL()) { |
| 10453 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 10454 | E = ULE->decls_end(); I != E; ++I) { |
| 10455 | assert(!(*I)->getDeclContext()->isRecord()); |
| 10456 | assert(isa<UsingShadowDecl>(*I) || |
| 10457 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 10458 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10459 | } |
| 10460 | } |
| 10461 | #endif |
| 10462 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10463 | // It would be nice to avoid this copy. |
| 10464 | TemplateArgumentListInfo TABuffer; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10465 | TemplateArgumentListInfo *ExplicitTemplateArgs = nullptr; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10466 | if (ULE->hasExplicitTemplateArgs()) { |
| 10467 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10468 | ExplicitTemplateArgs = &TABuffer; |
| 10469 | } |
| 10470 | |
| 10471 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 10472 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10473 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 10474 | CandidateSet, PartialOverloading, |
| 10475 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10476 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10477 | if (ULE->requiresADL()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10478 | AddArgumentDependentLookupCandidates(ULE->getName(), ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10479 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10480 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10481 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10482 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10483 | /// Determine whether a declaration with the specified name could be moved into |
| 10484 | /// a different namespace. |
| 10485 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 10486 | switch (Name.getCXXOverloadedOperator()) { |
| 10487 | case OO_New: case OO_Array_New: |
| 10488 | case OO_Delete: case OO_Array_Delete: |
| 10489 | return false; |
| 10490 | |
| 10491 | default: |
| 10492 | return true; |
| 10493 | } |
| 10494 | } |
| 10495 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10496 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 10497 | /// template, where the non-dependent name was declared after the template |
| 10498 | /// was defined. This is common in code written for a compilers which do not |
| 10499 | /// correctly implement two-stage name lookup. |
| 10500 | /// |
| 10501 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10502 | static bool |
| 10503 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 10504 | const CXXScopeSpec &SS, LookupResult &R, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10505 | OverloadCandidateSet::CandidateSetKind CSK, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10506 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10507 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10508 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 10509 | return false; |
| 10510 | |
| 10511 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 10512 | if (DC->isTransparentContext()) |
| 10513 | continue; |
| 10514 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10515 | SemaRef.LookupQualifiedName(R, DC); |
| 10516 | |
| 10517 | if (!R.empty()) { |
| 10518 | R.suppressDiagnostics(); |
| 10519 | |
| 10520 | if (isa<CXXRecordDecl>(DC)) { |
| 10521 | // Don't diagnose names we find in classes; we get much better |
| 10522 | // diagnostics for these from DiagnoseEmptyLookup. |
| 10523 | R.clear(); |
| 10524 | return false; |
| 10525 | } |
| 10526 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10527 | OverloadCandidateSet Candidates(FnLoc, CSK); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10528 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 10529 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10530 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10531 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10532 | |
| 10533 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10534 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10535 | // No viable functions. Don't bother the user with notes for functions |
| 10536 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10537 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10538 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10539 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10540 | |
| 10541 | // Find the namespaces where ADL would have looked, and suggest |
| 10542 | // declaring the function there instead. |
| 10543 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 10544 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 10545 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10546 | AssociatedNamespaces, |
| 10547 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10548 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10549 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 10550 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 10551 | for (Sema::AssociatedNamespaceSet::iterator |
| 10552 | it = AssociatedNamespaces.begin(), |
| 10553 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 10554 | // Never suggest declaring a function within namespace 'std'. |
| 10555 | if (Std && Std->Encloses(*it)) |
| 10556 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 10557 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10558 | // Never suggest declaring a function within a namespace with a |
| 10559 | // reserved name, like __gnu_cxx. |
| 10560 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 10561 | if (NS && |
| 10562 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 10563 | continue; |
| 10564 | |
| 10565 | SuggestedNamespaces.insert(*it); |
| 10566 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10567 | } |
| 10568 | |
| 10569 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 10570 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10571 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10572 | SemaRef.Diag(Best->Function->getLocation(), |
| 10573 | diag::note_not_found_by_two_phase_lookup) |
| 10574 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10575 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10576 | SemaRef.Diag(Best->Function->getLocation(), |
| 10577 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10578 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10579 | } else { |
| 10580 | // FIXME: It would be useful to list the associated namespaces here, |
| 10581 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10582 | // a localized representation of a list of items. |
| 10583 | SemaRef.Diag(Best->Function->getLocation(), |
| 10584 | diag::note_not_found_by_two_phase_lookup) |
| 10585 | << R.getLookupName() << 2; |
| 10586 | } |
| 10587 | |
| 10588 | // Try to recover by calling this function. |
| 10589 | return true; |
| 10590 | } |
| 10591 | |
| 10592 | R.clear(); |
| 10593 | } |
| 10594 | |
| 10595 | return false; |
| 10596 | } |
| 10597 | |
| 10598 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10599 | /// template, where the non-dependent operator was declared after the template |
| 10600 | /// was defined. |
| 10601 | /// |
| 10602 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10603 | static bool |
| 10604 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10605 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10606 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10607 | DeclarationName OpName = |
| 10608 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10609 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10610 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10611 | OverloadCandidateSet::CSK_Operator, |
| 10612 | /*ExplicitTemplateArgs=*/nullptr, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10613 | } |
| 10614 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10615 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10616 | class BuildRecoveryCallExprRAII { |
| 10617 | Sema &SemaRef; |
| 10618 | public: |
| 10619 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10620 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10621 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10622 | } |
| 10623 | |
| 10624 | ~BuildRecoveryCallExprRAII() { |
| 10625 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10626 | } |
| 10627 | }; |
| 10628 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10629 | } |
| 10630 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10631 | static std::unique_ptr<CorrectionCandidateCallback> |
| 10632 | MakeValidator(Sema &SemaRef, MemberExpr *ME, size_t NumArgs, |
| 10633 | bool HasTemplateArgs, bool AllowTypoCorrection) { |
| 10634 | if (!AllowTypoCorrection) |
| 10635 | return llvm::make_unique<NoTypoCorrectionCCC>(); |
| 10636 | return llvm::make_unique<FunctionCallFilterCCC>(SemaRef, NumArgs, |
| 10637 | HasTemplateArgs, ME); |
| 10638 | } |
| 10639 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10640 | /// Attempts to recover from a call where no functions were found. |
| 10641 | /// |
| 10642 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10643 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10644 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10645 | UnresolvedLookupExpr *ULE, |
| 10646 | SourceLocation LParenLoc, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10647 | MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10648 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10649 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10650 | // Do not try to recover if it is already building a recovery call. |
| 10651 | // This stops infinite loops for template instantiations like |
| 10652 | // |
| 10653 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10654 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10655 | // |
| 10656 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10657 | return ExprError(); |
| 10658 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10659 | |
| 10660 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10661 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10662 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10663 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10664 | TemplateArgumentListInfo TABuffer; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10665 | TemplateArgumentListInfo *ExplicitTemplateArgs = nullptr; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10666 | if (ULE->hasExplicitTemplateArgs()) { |
| 10667 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10668 | ExplicitTemplateArgs = &TABuffer; |
| 10669 | } |
| 10670 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10671 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10672 | Sema::LookupOrdinaryName); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10673 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10674 | OverloadCandidateSet::CSK_Normal, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10675 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10676 | (!EmptyLookup || |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10677 | SemaRef.DiagnoseEmptyLookup( |
| 10678 | S, SS, R, |
| 10679 | MakeValidator(SemaRef, dyn_cast<MemberExpr>(Fn), Args.size(), |
| 10680 | ExplicitTemplateArgs != nullptr, AllowTypoCorrection), |
| 10681 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10682 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10683 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10684 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10685 | |
| 10686 | // Build an implicit member call if appropriate. Just drop the |
| 10687 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10688 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10689 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10690 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10691 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10692 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10693 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10694 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10695 | else |
| 10696 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10697 | |
| 10698 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10699 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10700 | |
| 10701 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10702 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10703 | // end up here. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10704 | return SemaRef.ActOnCallExpr(/*Scope*/ nullptr, NewFn.get(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10705 | MultiExprArg(Args.data(), Args.size()), |
| 10706 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10707 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10708 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10709 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10710 | /// the given function. |
| 10711 | /// \returns true when an the ExprResult output parameter has been set. |
| 10712 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10713 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10714 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10715 | SourceLocation RParenLoc, |
| 10716 | OverloadCandidateSet *CandidateSet, |
| 10717 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10718 | #ifndef NDEBUG |
| 10719 | if (ULE->requiresADL()) { |
| 10720 | // To do ADL, we must have found an unqualified name. |
| 10721 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10722 | |
| 10723 | // We don't perform ADL for implicit declarations of builtins. |
| 10724 | // Verify that this was correctly set up. |
| 10725 | FunctionDecl *F; |
| 10726 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10727 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10728 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10729 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10730 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10731 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10732 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10733 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10734 | #endif |
| 10735 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10736 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10737 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10738 | *Result = ExprError(); |
| 10739 | return true; |
| 10740 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10741 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10742 | // Add the functions denoted by the callee to the set of candidate |
| 10743 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10744 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10745 | |
| 10746 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10747 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10748 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10749 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10750 | // In Microsoft mode, if we are inside a template class member function then |
| 10751 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10752 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10753 | // classes. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10754 | if (getLangOpts().MSVCCompat && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10755 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10756 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10757 | Context.DependentTy, VK_RValue, |
| 10758 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10759 | CE->setTypeDependent(true); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10760 | *Result = CE; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10761 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10762 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10763 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10764 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10765 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10766 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10767 | return false; |
| 10768 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10769 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10770 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10771 | /// the completed call expression. If overload resolution fails, emits |
| 10772 | /// diagnostics and returns ExprError() |
| 10773 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10774 | UnresolvedLookupExpr *ULE, |
| 10775 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10776 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10777 | SourceLocation RParenLoc, |
| 10778 | Expr *ExecConfig, |
| 10779 | OverloadCandidateSet *CandidateSet, |
| 10780 | OverloadCandidateSet::iterator *Best, |
| 10781 | OverloadingResult OverloadResult, |
| 10782 | bool AllowTypoCorrection) { |
| 10783 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10784 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10785 | RParenLoc, /*EmptyLookup=*/true, |
| 10786 | AllowTypoCorrection); |
| 10787 | |
| 10788 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10789 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10790 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10791 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10792 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10793 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10794 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10795 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10796 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10797 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10798 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10799 | case OR_No_Viable_Function: { |
| 10800 | // Try to recover by looking for viable functions which the user might |
| 10801 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10802 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10803 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10804 | /*EmptyLookup=*/false, |
| 10805 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10806 | if (!Recovery.isInvalid()) |
| 10807 | return Recovery; |
| 10808 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10809 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10810 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10811 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10812 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10813 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10814 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10815 | |
| 10816 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10817 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10818 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10819 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10820 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10821 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10822 | case OR_Deleted: { |
| 10823 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10824 | << (*Best)->Function->isDeleted() |
| 10825 | << ULE->getName() |
| 10826 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10827 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10828 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10829 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10830 | // We emitted an error for the unvailable/deleted function call but keep |
| 10831 | // the call in the AST. |
| 10832 | FunctionDecl *FDecl = (*Best)->Function; |
| 10833 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10834 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10835 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10836 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10837 | } |
| 10838 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10839 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10840 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10841 | } |
| 10842 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10843 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10844 | /// (which eventually refers to the declaration Func) and the call |
| 10845 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10846 | /// to a specific function. If overload resolution succeeds, returns |
| 10847 | /// the call expression produced by overload resolution. |
| 10848 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10849 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10850 | UnresolvedLookupExpr *ULE, |
| 10851 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10852 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10853 | SourceLocation RParenLoc, |
| 10854 | Expr *ExecConfig, |
| 10855 | bool AllowTypoCorrection) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10856 | OverloadCandidateSet CandidateSet(Fn->getExprLoc(), |
| 10857 | OverloadCandidateSet::CSK_Normal); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10858 | ExprResult result; |
| 10859 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10860 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10861 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10862 | return result; |
| 10863 | |
| 10864 | OverloadCandidateSet::iterator Best; |
| 10865 | OverloadingResult OverloadResult = |
| 10866 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10867 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10868 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10869 | RParenLoc, ExecConfig, &CandidateSet, |
| 10870 | &Best, OverloadResult, |
| 10871 | AllowTypoCorrection); |
| 10872 | } |
| 10873 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10874 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10875 | return Functions.size() > 1 || |
| 10876 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10877 | } |
| 10878 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10879 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10880 | /// operator. |
| 10881 | /// |
| 10882 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10883 | /// |
| 10884 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10885 | /// operator. |
| 10886 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10887 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10888 | /// considered by overload resolution. The caller needs to build this |
| 10889 | /// set based on the context using, e.g., |
| 10890 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10891 | /// set should not contain any member functions; those will be added |
| 10892 | /// by CreateOverloadedUnaryOp(). |
| 10893 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10894 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10895 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10896 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10897 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10898 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10899 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10900 | |
| 10901 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10902 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10903 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10904 | // TODO: provide better source location info. |
| 10905 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10906 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10907 | if (checkPlaceholderForOverload(*this, Input)) |
| 10908 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10909 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10910 | Expr *Args[2] = { Input, nullptr }; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10911 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10912 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10913 | // For post-increment and post-decrement, add the implicit '0' as |
| 10914 | // the second argument, so that we know this is a post-increment or |
| 10915 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10916 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10917 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10918 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10919 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10920 | NumArgs = 2; |
| 10921 | } |
| 10922 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10923 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10924 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10925 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10926 | if (Fns.empty()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10927 | return new (Context) UnaryOperator(Input, Opc, Context.DependentTy, |
| 10928 | VK_RValue, OK_Ordinary, OpLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10929 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10930 | CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10931 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10932 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10933 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10934 | /*ADL*/ true, IsOverloaded(Fns), |
| 10935 | Fns.begin(), Fns.end()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10936 | return new (Context) |
| 10937 | CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, Context.DependentTy, |
| 10938 | VK_RValue, OpLoc, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10939 | } |
| 10940 | |
| 10941 | // Build an empty overload set. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10942 | OverloadCandidateSet CandidateSet(OpLoc, OverloadCandidateSet::CSK_Operator); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10943 | |
| 10944 | // Add the candidates from the given function set. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 10945 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10946 | |
| 10947 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10948 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10949 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10950 | // Add candidates from ADL. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10951 | AddArgumentDependentLookupCandidates(OpName, OpLoc, ArgsArray, |
| 10952 | /*ExplicitTemplateArgs*/nullptr, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10953 | CandidateSet); |
| 10954 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10955 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10956 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10957 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10958 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10959 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10960 | // Perform overload resolution. |
| 10961 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10962 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10963 | case OR_Success: { |
| 10964 | // We found a built-in operator or an overloaded operator. |
| 10965 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10966 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10967 | if (FnDecl) { |
| 10968 | // We matched an overloaded operator. Build a call to that |
| 10969 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10970 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10971 | // Convert the arguments. |
| 10972 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10973 | CheckMemberOperatorAccess(OpLoc, Args[0], nullptr, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10974 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10975 | ExprResult InputRes = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10976 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/nullptr, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10977 | Best->FoundDecl, Method); |
| 10978 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10979 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10980 | Input = InputRes.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10981 | } else { |
| 10982 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10983 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10984 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10985 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10986 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10987 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10988 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10989 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10990 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10991 | Input = InputInit.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10992 | } |
| 10993 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10994 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10995 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10996 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10997 | if (FnExpr.isInvalid()) |
| 10998 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10999 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11000 | // Determine the result type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11001 | QualType ResultTy = FnDecl->getReturnType(); |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11002 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11003 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11004 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 11005 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11006 | CallExpr *TheCall = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11007 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.get(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11008 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 11009 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11010 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, FnDecl)) |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 11011 | return ExprError(); |
| 11012 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11013 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 11014 | } else { |
| 11015 | // We matched a built-in operator. Convert the arguments, then |
| 11016 | // break out so that we will build the appropriate built-in |
| 11017 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11018 | ExprResult InputRes = |
| 11019 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 11020 | Best->Conversions[0], AA_Passing); |
| 11021 | if (InputRes.isInvalid()) |
| 11022 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11023 | Input = InputRes.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 11024 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 11025 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11026 | } |
| 11027 | |
| 11028 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11029 | // This is an erroneous use of an operator which can be overloaded by |
| 11030 | // a non-member function. Check for non-member operators which were |
| 11031 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11032 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11033 | // FIXME: Recover by calling the found function. |
| 11034 | return ExprError(); |
| 11035 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11036 | // No viable function; fall through to handling this as a |
| 11037 | // built-in operator, which will produce an error message for us. |
| 11038 | break; |
| 11039 | |
| 11040 | case OR_Ambiguous: |
| 11041 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11042 | << UnaryOperator::getOpcodeStr(Opc) |
| 11043 | << Input->getType() |
| 11044 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11045 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11046 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 11047 | return ExprError(); |
| 11048 | |
| 11049 | case OR_Deleted: |
| 11050 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11051 | << Best->Function->isDeleted() |
| 11052 | << UnaryOperator::getOpcodeStr(Opc) |
| 11053 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 11054 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11055 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 11056 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11057 | return ExprError(); |
| 11058 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 11059 | |
| 11060 | // Either we found no viable overloaded operator or we matched a |
| 11061 | // built-in operator. In either case, fall through to trying to |
| 11062 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11063 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 11064 | } |
| 11065 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11066 | /// \brief Create a binary operation that may resolve to an overloaded |
| 11067 | /// operator. |
| 11068 | /// |
| 11069 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 11070 | /// |
| 11071 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 11072 | /// operator. |
| 11073 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 11074 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11075 | /// considered by overload resolution. The caller needs to build this |
| 11076 | /// set based on the context using, e.g., |
| 11077 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 11078 | /// set should not contain any member functions; those will be added |
| 11079 | /// by CreateOverloadedBinOp(). |
| 11080 | /// |
| 11081 | /// \param LHS Left-hand argument. |
| 11082 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11083 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11084 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11085 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 11086 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11087 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11088 | Expr *Args[2] = { LHS, RHS }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11089 | LHS=RHS=nullptr; // Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11090 | |
| 11091 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 11092 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 11093 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 11094 | |
| 11095 | // If either side is type-dependent, create an appropriate dependent |
| 11096 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11097 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 11098 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11099 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 11100 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11101 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11102 | return new (Context) BinaryOperator( |
| 11103 | Args[0], Args[1], Opc, Context.DependentTy, VK_RValue, OK_Ordinary, |
| 11104 | OpLoc, FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11105 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11106 | return new (Context) CompoundAssignOperator( |
| 11107 | Args[0], Args[1], Opc, Context.DependentTy, VK_LValue, OK_Ordinary, |
| 11108 | Context.DependentTy, Context.DependentTy, OpLoc, |
| 11109 | FPFeatures.fp_contract); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 11110 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 11111 | |
| 11112 | // FIXME: save results of ADL from here? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11113 | CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11114 | // TODO: provide better source location info in DNLoc component. |
| 11115 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11116 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11117 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 11118 | NestedNameSpecifierLoc(), OpNameInfo, |
| 11119 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 11120 | Fns.begin(), Fns.end()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11121 | return new (Context) |
| 11122 | CXXOperatorCallExpr(Context, Op, Fn, Args, Context.DependentTy, |
| 11123 | VK_RValue, OpLoc, FPFeatures.fp_contract); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11124 | } |
| 11125 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11126 | // Always do placeholder-like conversions on the RHS. |
| 11127 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 11128 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11129 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 11130 | // Do placeholder-like conversion on the LHS; note that we should |
| 11131 | // not get here with a PseudoObject LHS. |
| 11132 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11133 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 11134 | return ExprError(); |
| 11135 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 11136 | // If this is the assignment operator, we only perform overload resolution |
| 11137 | // if the left-hand side is a class or enumeration type. This is actually |
| 11138 | // a hack. The standard requires that we do overload resolution between the |
| 11139 | // various built-in candidates, but as DR507 points out, this can lead to |
| 11140 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 11141 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11142 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11143 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11144 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11145 | // If this is the .* operator, which is not overloadable, just |
| 11146 | // create a built-in binary operator. |
| 11147 | if (Opc == BO_PtrMemD) |
| 11148 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 11149 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 11150 | // Build an empty overload set. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11151 | OverloadCandidateSet CandidateSet(OpLoc, OverloadCandidateSet::CSK_Operator); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11152 | |
| 11153 | // Add the candidates from the given function set. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 11154 | AddFunctionCandidates(Fns, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11155 | |
| 11156 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11157 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11158 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11159 | // Add candidates from ADL. Per [over.match.oper]p2, this lookup is not |
| 11160 | // performed for an assignment operator (nor for operator[] nor operator->, |
| 11161 | // which don't get here). |
| 11162 | if (Opc != BO_Assign) |
| 11163 | AddArgumentDependentLookupCandidates(OpName, OpLoc, Args, |
| 11164 | /*ExplicitTemplateArgs*/ nullptr, |
| 11165 | CandidateSet); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 11166 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11167 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11168 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11169 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11170 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11171 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11172 | // Perform overload resolution. |
| 11173 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11174 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 11175 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11176 | // We found a built-in operator or an overloaded operator. |
| 11177 | FunctionDecl *FnDecl = Best->Function; |
| 11178 | |
| 11179 | if (FnDecl) { |
| 11180 | // We matched an overloaded operator. Build a call to that |
| 11181 | // operator. |
| 11182 | |
| 11183 | // Convert the arguments. |
| 11184 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 11185 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11186 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 11187 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11188 | ExprResult Arg1 = |
| 11189 | PerformCopyInitialization( |
| 11190 | InitializedEntity::InitializeParameter(Context, |
| 11191 | FnDecl->getParamDecl(0)), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11192 | SourceLocation(), Args[1]); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 11193 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11194 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 11195 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11196 | ExprResult Arg0 = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11197 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/nullptr, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11198 | Best->FoundDecl, Method); |
| 11199 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 11200 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11201 | Args[0] = Arg0.getAs<Expr>(); |
| 11202 | Args[1] = RHS = Arg1.getAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11203 | } else { |
| 11204 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11205 | ExprResult Arg0 = PerformCopyInitialization( |
| 11206 | InitializedEntity::InitializeParameter(Context, |
| 11207 | FnDecl->getParamDecl(0)), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11208 | SourceLocation(), Args[0]); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 11209 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11210 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 11211 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11212 | ExprResult Arg1 = |
| 11213 | PerformCopyInitialization( |
| 11214 | InitializedEntity::InitializeParameter(Context, |
| 11215 | FnDecl->getParamDecl(1)), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11216 | SourceLocation(), Args[1]); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 11217 | if (Arg1.isInvalid()) |
| 11218 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11219 | Args[0] = LHS = Arg0.getAs<Expr>(); |
| 11220 | Args[1] = RHS = Arg1.getAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11221 | } |
| 11222 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11223 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11224 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11225 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11226 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11227 | if (FnExpr.isInvalid()) |
| 11228 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11229 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11230 | // Determine the result type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11231 | QualType ResultTy = FnDecl->getReturnType(); |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11232 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11233 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11234 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11235 | CXXOperatorCallExpr *TheCall = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11236 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.get(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11237 | Args, ResultTy, VK, OpLoc, |
| 11238 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11239 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11240 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11241 | FnDecl)) |
| 11242 | return ExprError(); |
| 11243 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 11244 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 11245 | // Cut off the implicit 'this'. |
| 11246 | if (isa<CXXMethodDecl>(FnDecl)) |
| 11247 | ArgsArray = ArgsArray.slice(1); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 11248 | |
| 11249 | // Check for a self move. |
| 11250 | if (Op == OO_Equal) |
| 11251 | DiagnoseSelfMove(Args[0], Args[1], OpLoc); |
| 11252 | |
| 11253 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 11254 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 11255 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11256 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11257 | } else { |
| 11258 | // We matched a built-in operator. Convert the arguments, then |
| 11259 | // break out so that we will build the appropriate built-in |
| 11260 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11261 | ExprResult ArgsRes0 = |
| 11262 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 11263 | Best->Conversions[0], AA_Passing); |
| 11264 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11265 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11266 | Args[0] = ArgsRes0.get(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11267 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11268 | ExprResult ArgsRes1 = |
| 11269 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 11270 | Best->Conversions[1], AA_Passing); |
| 11271 | if (ArgsRes1.isInvalid()) |
| 11272 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11273 | Args[1] = ArgsRes1.get(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11274 | break; |
| 11275 | } |
| 11276 | } |
| 11277 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11278 | case OR_No_Viable_Function: { |
| 11279 | // C++ [over.match.oper]p9: |
| 11280 | // If the operator is the operator , [...] and there are no |
| 11281 | // viable functions, then the operator is assumed to be the |
| 11282 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11283 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11284 | break; |
| 11285 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11286 | // For class as left operand for assignment or compound assigment |
| 11287 | // operator do not fall through to handling in built-in, but report that |
| 11288 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11289 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11290 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11291 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 11292 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 11293 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11294 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 3f93d4c | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 11295 | if (Args[0]->getType()->isIncompleteType()) { |
| 11296 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 11297 | << Args[0]->getType() |
| 11298 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11299 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11300 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11301 | // This is an erroneous use of an operator which can be overloaded by |
| 11302 | // a non-member function. Check for non-member operators which were |
| 11303 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11304 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11305 | // FIXME: Recover by calling the found function. |
| 11306 | return ExprError(); |
| 11307 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11308 | // No viable function; try to create a built-in operation, which will |
| 11309 | // produce an error. Then, show the non-viable candidates. |
| 11310 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 11311 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11312 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11313 | "C++ binary operator overloading is missing candidates!"); |
| 11314 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11315 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11316 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11317 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11318 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11319 | |
| 11320 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11321 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11322 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11323 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11324 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11325 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11326 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11327 | return ExprError(); |
| 11328 | |
| 11329 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 11330 | if (isImplicitlyDeleted(Best->Function)) { |
| 11331 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 11332 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 11333 | << Context.getRecordType(Method->getParent()) |
| 11334 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 11335 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 11336 | // The user probably meant to call this special member. Just |
| 11337 | // explain why it's deleted. |
| 11338 | NoteDeletedFunction(Method); |
| 11339 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 11340 | } else { |
| 11341 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11342 | << Best->Function->isDeleted() |
| 11343 | << BinaryOperator::getOpcodeStr(Opc) |
| 11344 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 11345 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11346 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11347 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 11348 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11349 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 11350 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11351 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11352 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11353 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11354 | } |
| 11355 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11356 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11357 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 11358 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11359 | Expr *Base, Expr *Idx) { |
| 11360 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11361 | DeclarationName OpName = |
| 11362 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 11363 | |
| 11364 | // If either side is type-dependent, create an appropriate dependent |
| 11365 | // expression. |
| 11366 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 11367 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11368 | CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11369 | // CHECKME: no 'operator' keyword? |
| 11370 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 11371 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11372 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 11373 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11374 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 11375 | /*ADL*/ true, /*Overloaded*/ false, |
| 11376 | UnresolvedSetIterator(), |
| 11377 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11378 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11379 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11380 | return new (Context) |
| 11381 | CXXOperatorCallExpr(Context, OO_Subscript, Fn, Args, |
| 11382 | Context.DependentTy, VK_RValue, RLoc, false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11383 | } |
| 11384 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11385 | // Handle placeholders on both operands. |
| 11386 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 11387 | return ExprError(); |
| 11388 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 11389 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11390 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11391 | // Build an empty overload set. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11392 | OverloadCandidateSet CandidateSet(LLoc, OverloadCandidateSet::CSK_Operator); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11393 | |
| 11394 | // Subscript can only be overloaded as a member function. |
| 11395 | |
| 11396 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11397 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11398 | |
| 11399 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11400 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11401 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11402 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11403 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11404 | // Perform overload resolution. |
| 11405 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11406 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11407 | case OR_Success: { |
| 11408 | // We found a built-in operator or an overloaded operator. |
| 11409 | FunctionDecl *FnDecl = Best->Function; |
| 11410 | |
| 11411 | if (FnDecl) { |
| 11412 | // We matched an overloaded operator. Build a call to that |
| 11413 | // operator. |
| 11414 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11415 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 11416 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11417 | // Convert the arguments. |
| 11418 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11419 | ExprResult Arg0 = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11420 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/nullptr, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11421 | Best->FoundDecl, Method); |
| 11422 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11423 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11424 | Args[0] = Arg0.get(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11425 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11426 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11427 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11428 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11429 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11430 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11431 | SourceLocation(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11432 | Args[1]); |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11433 | if (InputInit.isInvalid()) |
| 11434 | return ExprError(); |
| 11435 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11436 | Args[1] = InputInit.getAs<Expr>(); |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11437 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11438 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11439 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 11440 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11441 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11442 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11443 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11444 | OpLocInfo.getLoc(), |
| 11445 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11446 | if (FnExpr.isInvalid()) |
| 11447 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11448 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11449 | // Determine the result type |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11450 | QualType ResultTy = FnDecl->getReturnType(); |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11451 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11452 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11453 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11454 | CXXOperatorCallExpr *TheCall = |
| 11455 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11456 | FnExpr.get(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11457 | ResultTy, VK, RLoc, |
| 11458 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11459 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11460 | if (CheckCallReturnType(FnDecl->getReturnType(), LLoc, TheCall, FnDecl)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11461 | return ExprError(); |
| 11462 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11463 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11464 | } else { |
| 11465 | // We matched a built-in operator. Convert the arguments, then |
| 11466 | // break out so that we will build the appropriate built-in |
| 11467 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11468 | ExprResult ArgsRes0 = |
| 11469 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 11470 | Best->Conversions[0], AA_Passing); |
| 11471 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11472 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11473 | Args[0] = ArgsRes0.get(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11474 | |
| 11475 | ExprResult ArgsRes1 = |
| 11476 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 11477 | Best->Conversions[1], AA_Passing); |
| 11478 | if (ArgsRes1.isInvalid()) |
| 11479 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11480 | Args[1] = ArgsRes1.get(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11481 | |
| 11482 | break; |
| 11483 | } |
| 11484 | } |
| 11485 | |
| 11486 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11487 | if (CandidateSet.empty()) |
| 11488 | Diag(LLoc, diag::err_ovl_no_oper) |
| 11489 | << Args[0]->getType() << /*subscript*/ 0 |
| 11490 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11491 | else |
| 11492 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 11493 | << Args[0]->getType() |
| 11494 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11495 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11496 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11497 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11498 | } |
| 11499 | |
| 11500 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11501 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11502 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11503 | << Args[0]->getType() << Args[1]->getType() |
| 11504 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11505 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11506 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11507 | return ExprError(); |
| 11508 | |
| 11509 | case OR_Deleted: |
| 11510 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 11511 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11512 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11513 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11514 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11515 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11516 | return ExprError(); |
| 11517 | } |
| 11518 | |
| 11519 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11520 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11521 | } |
| 11522 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11523 | /// BuildCallToMemberFunction - Build a call to a member |
| 11524 | /// function. MemExpr is the expression that refers to the member |
| 11525 | /// function (and includes the object parameter), Args/NumArgs are the |
| 11526 | /// arguments to the function call (not including the object |
| 11527 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11528 | /// expression refers to a non-static member function or an overloaded |
| 11529 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11530 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11531 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11532 | SourceLocation LParenLoc, |
| 11533 | MultiExprArg Args, |
| 11534 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11535 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 11536 | MemExprE->getType() == Context.OverloadTy); |
| 11537 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11538 | // Dig out the member expression. This holds both the object |
| 11539 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11540 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11541 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11542 | // Determine whether this is a call to a pointer-to-member function. |
| 11543 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 11544 | assert(op->getType() == Context.BoundMemberTy); |
| 11545 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 11546 | |
| 11547 | QualType fnType = |
| 11548 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 11549 | |
| 11550 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 11551 | QualType resultType = proto->getCallResultType(Context); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11552 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getReturnType()); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11553 | |
| 11554 | // Check that the object type isn't more qualified than the |
| 11555 | // member function we're calling. |
| 11556 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 11557 | |
| 11558 | QualType objectType = op->getLHS()->getType(); |
| 11559 | if (op->getOpcode() == BO_PtrMemI) |
| 11560 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 11561 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 11562 | |
| 11563 | Qualifiers difference = objectQuals - funcQuals; |
| 11564 | difference.removeObjCGCAttr(); |
| 11565 | difference.removeAddressSpace(); |
| 11566 | if (difference) { |
| 11567 | std::string qualsString = difference.getAsString(); |
| 11568 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 11569 | << fnType.getUnqualifiedType() |
| 11570 | << qualsString |
| 11571 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 11572 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11573 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11574 | if (resultType->isMemberPointerType()) |
| 11575 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 11576 | RequireCompleteType(LParenLoc, resultType, 0); |
| 11577 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11578 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11579 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11580 | resultType, valueKind, RParenLoc); |
| 11581 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11582 | if (CheckCallReturnType(proto->getReturnType(), op->getRHS()->getLocStart(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11583 | call, nullptr)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11584 | return ExprError(); |
| 11585 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11586 | if (ConvertArgumentsForCall(call, op, nullptr, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11587 | return ExprError(); |
| 11588 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11589 | if (CheckOtherCall(call, proto)) |
| 11590 | return ExprError(); |
| 11591 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11592 | return MaybeBindToTemporary(call); |
| 11593 | } |
| 11594 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 11595 | if (isa<CXXPseudoDestructorExpr>(NakedMemExpr)) |
| 11596 | return new (Context) |
| 11597 | CallExpr(Context, MemExprE, Args, Context.VoidTy, VK_RValue, RParenLoc); |
| 11598 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11599 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11600 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11601 | return ExprError(); |
| 11602 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11603 | MemberExpr *MemExpr; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11604 | CXXMethodDecl *Method = nullptr; |
| 11605 | DeclAccessPair FoundDecl = DeclAccessPair::make(nullptr, AS_public); |
| 11606 | NestedNameSpecifier *Qualifier = nullptr; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11607 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11608 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11609 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11610 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11611 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11612 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11613 | } else { |
| 11614 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11615 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11616 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11617 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11618 | Expr::Classification ObjectClassification |
| 11619 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11620 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11621 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11622 | // Add overload candidates |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11623 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc(), |
| 11624 | OverloadCandidateSet::CSK_Normal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11625 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11626 | // FIXME: avoid copy. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11627 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11628 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11629 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11630 | TemplateArgs = &TemplateArgsBuffer; |
| 11631 | } |
| 11632 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11633 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11634 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11635 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11636 | NamedDecl *Func = *I; |
| 11637 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11638 | if (isa<UsingShadowDecl>(Func)) |
| 11639 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11640 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11641 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11642 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11643 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11644 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11645 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11646 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11647 | // If explicit template arguments were provided, we can't call a |
| 11648 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11649 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11650 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11651 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11652 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11653 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11654 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11655 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11656 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11657 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11658 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11659 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11660 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11661 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11663 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11664 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11665 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11666 | UnbridgedCasts.restore(); |
| 11667 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11668 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11669 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11670 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11671 | case OR_Success: |
| 11672 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11673 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11674 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11675 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11676 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11677 | // If FoundDecl is different from Method (such as if one is a template |
| 11678 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11679 | // called on both. |
| 11680 | // FIXME: This would be more comprehensively addressed by modifying |
| 11681 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11682 | // being used. |
| 11683 | if (Method != FoundDecl.getDecl() && |
| 11684 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11685 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11686 | break; |
| 11687 | |
| 11688 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11689 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11690 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11691 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11692 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11693 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11694 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11695 | |
| 11696 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11697 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11698 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11699 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11700 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11701 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11702 | |
| 11703 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11704 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11705 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11706 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11707 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11708 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11709 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11710 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11711 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11712 | } |
| 11713 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11714 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11715 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11716 | // If overload resolution picked a static member, build a |
| 11717 | // non-member call based on that function. |
| 11718 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11719 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11720 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11721 | } |
| 11722 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11723 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11724 | } |
| 11725 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11726 | QualType ResultType = Method->getReturnType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11727 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11728 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11729 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11730 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11731 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11732 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11733 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11734 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11735 | // (CUDA B.1): Check for invalid calls between targets. |
| 11736 | if (getLangOpts().CUDA) { |
| 11737 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) { |
| 11738 | if (CheckCUDATarget(Caller, Method)) { |
| 11739 | Diag(MemExpr->getMemberLoc(), diag::err_ref_bad_target) |
| 11740 | << IdentifyCUDATarget(Method) << Method->getIdentifier() |
| 11741 | << IdentifyCUDATarget(Caller); |
| 11742 | return ExprError(); |
| 11743 | } |
| 11744 | } |
| 11745 | } |
| 11746 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11747 | // Check for a valid return type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11748 | if (CheckCallReturnType(Method->getReturnType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11749 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11750 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11751 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11752 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11753 | // We only need to do this if there was actually an overload; otherwise |
| 11754 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11755 | if (!Method->isStatic()) { |
| 11756 | ExprResult ObjectArg = |
| 11757 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11758 | FoundDecl, Method); |
| 11759 | if (ObjectArg.isInvalid()) |
| 11760 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11761 | MemExpr->setBase(ObjectArg.get()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11762 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11763 | |
| 11764 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11765 | const FunctionProtoType *Proto = |
| 11766 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11767 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11768 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11769 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11770 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11771 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11772 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11773 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11774 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11775 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11776 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11777 | isa<CXXDestructorDecl>(CurContext)) && |
| 11778 | TheCall->getMethodDecl()->isPure()) { |
| 11779 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11780 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11781 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11782 | Diag(MemExpr->getLocStart(), |
| 11783 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11784 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11785 | << MD->getParent()->getDeclName(); |
| 11786 | |
| 11787 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11788 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11789 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11790 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11791 | } |
| 11792 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11793 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11794 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11795 | /// overloaded function call operator (@c operator()) or performing a |
| 11796 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11797 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11798 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11799 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11800 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11801 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11802 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11803 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11804 | ExprResult Object = Obj; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11805 | |
| 11806 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11807 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11808 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11809 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11810 | assert(Object.get()->getType()->isRecordType() && |
| 11811 | "Requires object type argument"); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11812 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11813 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11814 | // C++ [over.call.object]p1: |
| 11815 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11816 | // 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] | 11817 | // candidate functions includes at least the function call |
| 11818 | // operators of T. The function call operators of T are obtained by |
| 11819 | // ordinary lookup of the name operator() in the context of |
| 11820 | // (E).operator(). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11821 | OverloadCandidateSet CandidateSet(LParenLoc, |
| 11822 | OverloadCandidateSet::CSK_Operator); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11823 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11824 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11825 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11826 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11827 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11828 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11829 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11830 | LookupQualifiedName(R, Record->getDecl()); |
| 11831 | R.suppressDiagnostics(); |
| 11832 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11833 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11834 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11835 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11836 | Object.get()->Classify(Context), |
| 11837 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11838 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11839 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11840 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11841 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11842 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11843 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11844 | // |
| 11845 | // operator conversion-type-id () cv-qualifier; |
| 11846 | // |
| 11847 | // where cv-qualifier is the same cv-qualification as, or a |
| 11848 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11849 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11850 | // R", or the type "reference to pointer to function of |
| 11851 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11852 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11853 | // is also considered as a candidate function. Similarly, |
| 11854 | // surrogate call functions are added to the set of candidate |
| 11855 | // functions for each conversion function declared in an |
| 11856 | // accessible base class provided the function is not hidden |
| 11857 | // within T by another intervening declaration. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 11858 | const auto &Conversions = |
| 11859 | cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
| 11860 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11861 | NamedDecl *D = *I; |
| 11862 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11863 | if (isa<UsingShadowDecl>(D)) |
| 11864 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11865 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11866 | // Skip over templated conversion functions; they aren't |
| 11867 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11868 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11869 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11870 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11871 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11872 | if (!Conv->isExplicit()) { |
| 11873 | // Strip the reference type (if any) and then the pointer type (if |
| 11874 | // any) to get down to what might be a function type. |
| 11875 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11876 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11877 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11878 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11879 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11880 | { |
| 11881 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11882 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11883 | } |
| 11884 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11886 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11887 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11888 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11889 | // Perform overload resolution. |
| 11890 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11891 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11892 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11893 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11894 | // Overload resolution succeeded; we'll build the appropriate call |
| 11895 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11896 | break; |
| 11897 | |
| 11898 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11899 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11900 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11901 | << Object.get()->getType() << /*call*/ 1 |
| 11902 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11903 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11904 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11905 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11906 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11907 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11908 | break; |
| 11909 | |
| 11910 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11911 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11912 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11913 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11914 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11915 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11916 | |
| 11917 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11918 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11919 | diag::err_ovl_deleted_object_call) |
| 11920 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11921 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11922 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11923 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11924 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11925 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11926 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11927 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11928 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11929 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11930 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11931 | UnbridgedCasts.restore(); |
| 11932 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11933 | if (Best->Function == nullptr) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11934 | // Since there is no function declaration, this is one of the |
| 11935 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11936 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11937 | = cast<CXXConversionDecl>( |
| 11938 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11939 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11940 | CheckMemberOperatorAccess(LParenLoc, Object.get(), nullptr, |
| 11941 | Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11942 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11943 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11944 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11945 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11946 | // We selected one of the surrogate functions that converts the |
| 11947 | // object parameter to a function pointer. Perform the conversion |
| 11948 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11949 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11950 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11951 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11952 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11953 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11954 | if (Call.isInvalid()) |
| 11955 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11956 | // Record usage of conversion in an implicit cast. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11957 | Call = ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11958 | CK_UserDefinedConversion, Call.get(), |
| 11959 | nullptr, VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11960 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11961 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11962 | } |
| 11963 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11964 | CheckMemberOperatorAccess(LParenLoc, Object.get(), nullptr, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11965 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11966 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11967 | // that calls this method, using Object for the implicit object |
| 11968 | // parameter and passing along the remaining arguments. |
| 11969 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11970 | |
| 11971 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11972 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11973 | return ExprError(); |
| 11974 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11975 | const FunctionProtoType *Proto = |
| 11976 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11977 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11978 | unsigned NumParams = Proto->getNumParams(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11979 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11980 | DeclarationNameInfo OpLocInfo( |
| 11981 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11982 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11983 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11984 | HadMultipleCandidates, |
| 11985 | OpLocInfo.getLoc(), |
| 11986 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11987 | if (NewFn.isInvalid()) |
| 11988 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11989 | |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11990 | // Build the full argument list for the method call (the implicit object |
| 11991 | // parameter is placed at the beginning of the list). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11992 | std::unique_ptr<Expr * []> MethodArgs(new Expr *[Args.size() + 1]); |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11993 | MethodArgs[0] = Object.get(); |
| 11994 | std::copy(Args.begin(), Args.end(), &MethodArgs[1]); |
| 11995 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11996 | // Once we've built TheCall, all of the expressions are properly |
| 11997 | // owned. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11998 | QualType ResultTy = Method->getReturnType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11999 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 12000 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 12001 | |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 12002 | CXXOperatorCallExpr *TheCall = new (Context) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12003 | CXXOperatorCallExpr(Context, OO_Call, NewFn.get(), |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 12004 | llvm::makeArrayRef(MethodArgs.get(), Args.size() + 1), |
| 12005 | ResultTy, VK, RParenLoc, false); |
| 12006 | MethodArgs.reset(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12007 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12008 | if (CheckCallReturnType(Method->getReturnType(), LParenLoc, TheCall, Method)) |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 12009 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12010 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 12011 | // We may have default arguments. If so, we need to allocate more |
| 12012 | // slots in the call for them. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12013 | if (Args.size() < NumParams) |
| 12014 | TheCall->setNumArgs(Context, NumParams + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 12015 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 12016 | bool IsError = false; |
| 12017 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12018 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12019 | ExprResult ObjRes = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12020 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/nullptr, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12021 | Best->FoundDecl, Method); |
| 12022 | if (ObjRes.isInvalid()) |
| 12023 | IsError = true; |
| 12024 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 12025 | Object = ObjRes; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12026 | TheCall->setArg(0, Object.get()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 12027 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12028 | // Check the argument types. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12029 | for (unsigned i = 0; i != NumParams; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12030 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 12031 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12032 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12033 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 12034 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 12035 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12036 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 12037 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 12038 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 12039 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12040 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12041 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 12042 | IsError |= InputInit.isInvalid(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12043 | Arg = InputInit.getAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 12044 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12045 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 12046 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 12047 | if (DefArg.isInvalid()) { |
| 12048 | IsError = true; |
| 12049 | break; |
| 12050 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12051 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12052 | Arg = DefArg.getAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 12053 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12054 | |
| 12055 | TheCall->setArg(i + 1, Arg); |
| 12056 | } |
| 12057 | |
| 12058 | // If this is a variadic call, handle args passed through "...". |
| 12059 | if (Proto->isVariadic()) { |
| 12060 | // Promote the arguments (C99 6.5.2.2p7). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12061 | for (unsigned i = NumParams, e = Args.size(); i < e; i++) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12062 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
| 12063 | nullptr); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12064 | IsError |= Arg.isInvalid(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12065 | TheCall->setArg(i + 1, Arg.get()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12066 | } |
| 12067 | } |
| 12068 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 12069 | if (IsError) return true; |
| 12070 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 12071 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 12072 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 12073 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 12074 | return true; |
| 12075 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 12076 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12077 | } |
| 12078 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12079 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12080 | /// (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] | 12081 | /// @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] | 12082 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 12083 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 12084 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12085 | assert(Base->getType()->isRecordType() && |
| 12086 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12087 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12088 | if (checkPlaceholderForOverload(*this, Base)) |
| 12089 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 12090 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 12091 | SourceLocation Loc = Base->getExprLoc(); |
| 12092 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12093 | // C++ [over.ref]p1: |
| 12094 | // |
| 12095 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 12096 | // for a class object x of type T if T::operator->() exists and if |
| 12097 | // the operator is selected as the best match function by the |
| 12098 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12099 | DeclarationName OpName = |
| 12100 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12101 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Operator); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 12102 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 12103 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 12104 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 12105 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 12106 | return ExprError(); |
| 12107 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 12108 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 12109 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 12110 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 12111 | |
| 12112 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 12113 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 12114 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 12115 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 12116 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12117 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12118 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 12119 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12120 | // Perform overload resolution. |
| 12121 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12122 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12123 | case OR_Success: |
| 12124 | // Overload resolution succeeded; we'll build the call below. |
| 12125 | break; |
| 12126 | |
| 12127 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 12128 | if (CandidateSet.empty()) { |
| 12129 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 12130 | if (NoArrowOperatorFound) { |
| 12131 | // Report this specific error to the caller instead of emitting a |
| 12132 | // diagnostic, as requested. |
| 12133 | *NoArrowOperatorFound = true; |
| 12134 | return ExprError(); |
| 12135 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 12136 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 12137 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 12138 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 12139 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 12140 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 12141 | } |
| 12142 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12143 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 12144 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12145 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 12146 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12147 | |
| 12148 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 12149 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 12150 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12151 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 12152 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 12153 | |
| 12154 | case OR_Deleted: |
| 12155 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 12156 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 12157 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 12158 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 12159 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12160 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 12161 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12162 | } |
| 12163 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12164 | CheckMemberOperatorAccess(OpLoc, Base, nullptr, Best->FoundDecl); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 12165 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12166 | // Convert the object parameter. |
| 12167 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12168 | ExprResult BaseResult = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12169 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/nullptr, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12170 | Best->FoundDecl, Method); |
| 12171 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 12172 | return ExprError(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12173 | Base = BaseResult.get(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 12174 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12175 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 12176 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 12177 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12178 | if (FnExpr.isInvalid()) |
| 12179 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12180 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12181 | QualType ResultTy = Method->getReturnType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12182 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 12183 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12184 | CXXOperatorCallExpr *TheCall = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12185 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.get(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 12186 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 12187 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12188 | if (CheckCallReturnType(Method->getReturnType(), OpLoc, TheCall, Method)) |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 12189 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 12190 | |
| 12191 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 12192 | } |
| 12193 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 12194 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 12195 | /// a literal operator described by the provided lookup results. |
| 12196 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 12197 | DeclarationNameInfo &SuffixInfo, |
| 12198 | ArrayRef<Expr*> Args, |
| 12199 | SourceLocation LitEndLoc, |
| 12200 | TemplateArgumentListInfo *TemplateArgs) { |
| 12201 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12202 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12203 | OverloadCandidateSet CandidateSet(UDSuffixLoc, |
| 12204 | OverloadCandidateSet::CSK_Normal); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 12205 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, TemplateArgs, |
| 12206 | /*SuppressUserConversions=*/true); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12207 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 12208 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 12209 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 12210 | // Perform overload resolution. This will usually be trivial, but might need |
| 12211 | // to perform substitutions for a literal operator template. |
| 12212 | OverloadCandidateSet::iterator Best; |
| 12213 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 12214 | case OR_Success: |
| 12215 | case OR_Deleted: |
| 12216 | break; |
| 12217 | |
| 12218 | case OR_No_Viable_Function: |
| 12219 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 12220 | << R.getLookupName(); |
| 12221 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 12222 | return ExprError(); |
| 12223 | |
| 12224 | case OR_Ambiguous: |
| 12225 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 12226 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 12227 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12228 | } |
| 12229 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 12230 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 12231 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 12232 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 12233 | SuffixInfo.getLoc(), |
| 12234 | SuffixInfo.getInfo()); |
| 12235 | if (Fn.isInvalid()) |
| 12236 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12237 | |
| 12238 | // Check the argument types. This should almost always be a no-op, except |
| 12239 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12240 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12241 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12242 | ExprResult InputInit = PerformCopyInitialization( |
| 12243 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 12244 | SourceLocation(), Args[ArgIdx]); |
| 12245 | if (InputInit.isInvalid()) |
| 12246 | return true; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12247 | ConvArgs[ArgIdx] = InputInit.get(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12248 | } |
| 12249 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12250 | QualType ResultTy = FD->getReturnType(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12251 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 12252 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 12253 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12254 | UserDefinedLiteral *UDL = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12255 | new (Context) UserDefinedLiteral(Context, Fn.get(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 12256 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12257 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 12258 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12259 | if (CheckCallReturnType(FD->getReturnType(), UDSuffixLoc, UDL, FD)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12260 | return ExprError(); |
| 12261 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12262 | if (CheckFunctionCall(FD, UDL, nullptr)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 12263 | return ExprError(); |
| 12264 | |
| 12265 | return MaybeBindToTemporary(UDL); |
| 12266 | } |
| 12267 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12268 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 12269 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 12270 | /// will be invoked. Otherwise, the function will be found via argument |
| 12271 | /// dependent lookup. |
| 12272 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 12273 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 12274 | /// is returned. |
| 12275 | Sema::ForRangeStatus |
| 12276 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 12277 | SourceLocation RangeLoc, VarDecl *Decl, |
| 12278 | BeginEndFunction BEF, |
| 12279 | const DeclarationNameInfo &NameInfo, |
| 12280 | LookupResult &MemberLookup, |
| 12281 | OverloadCandidateSet *CandidateSet, |
| 12282 | Expr *Range, ExprResult *CallExpr) { |
| 12283 | CandidateSet->clear(); |
| 12284 | if (!MemberLookup.empty()) { |
| 12285 | ExprResult MemberRef = |
| 12286 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 12287 | /*IsPtr=*/false, CXXScopeSpec(), |
| 12288 | /*TemplateKWLoc=*/SourceLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12289 | /*FirstQualifierInScope=*/nullptr, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12290 | MemberLookup, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12291 | /*TemplateArgs=*/nullptr); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12292 | if (MemberRef.isInvalid()) { |
| 12293 | *CallExpr = ExprError(); |
| 12294 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 12295 | << RangeLoc << BEF << Range->getType(); |
| 12296 | return FRS_DiagnosticIssued; |
| 12297 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12298 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, nullptr); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12299 | if (CallExpr->isInvalid()) { |
| 12300 | *CallExpr = ExprError(); |
| 12301 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 12302 | << RangeLoc << BEF << Range->getType(); |
| 12303 | return FRS_DiagnosticIssued; |
| 12304 | } |
| 12305 | } else { |
| 12306 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12307 | UnresolvedLookupExpr *Fn = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12308 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/nullptr, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12309 | NestedNameSpecifierLoc(), NameInfo, |
| 12310 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 12311 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12312 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12313 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12314 | CandidateSet, CallExpr); |
| 12315 | if (CandidateSet->empty() || CandidateSetError) { |
| 12316 | *CallExpr = ExprError(); |
| 12317 | return FRS_NoViableFunction; |
| 12318 | } |
| 12319 | OverloadCandidateSet::iterator Best; |
| 12320 | OverloadingResult OverloadResult = |
| 12321 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 12322 | |
| 12323 | if (OverloadResult == OR_No_Viable_Function) { |
| 12324 | *CallExpr = ExprError(); |
| 12325 | return FRS_NoViableFunction; |
| 12326 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12327 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12328 | Loc, nullptr, CandidateSet, &Best, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12329 | OverloadResult, |
| 12330 | /*AllowTypoCorrection=*/false); |
| 12331 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 12332 | *CallExpr = ExprError(); |
| 12333 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 12334 | << RangeLoc << BEF << Range->getType(); |
| 12335 | return FRS_DiagnosticIssued; |
| 12336 | } |
| 12337 | } |
| 12338 | return FRS_Success; |
| 12339 | } |
| 12340 | |
| 12341 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12342 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 12343 | /// a C++ overloaded function (possibly with some parentheses and |
| 12344 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 12345 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 12346 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 12347 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12348 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12349 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12350 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 12351 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12352 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12353 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12354 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12355 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12356 | } |
| 12357 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12358 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12359 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 12360 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12361 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12362 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 12363 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12364 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12365 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12366 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12367 | |
| 12368 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12369 | ICE->getCastKind(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12370 | SubExpr, nullptr, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 12371 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12372 | } |
| 12373 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12374 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12375 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12376 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 12377 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 12378 | if (Method->isStatic()) { |
| 12379 | // Do nothing: static member functions aren't any different |
| 12380 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12381 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12382 | // Fix the subexpression, which really has to be an |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12383 | // UnresolvedLookupExpr holding an overloaded member function |
| 12384 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12385 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 12386 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12387 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12388 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12389 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12390 | assert(isa<DeclRefExpr>(SubExpr) |
| 12391 | && "fixed to something other than a decl ref"); |
| 12392 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 12393 | && "fixed to a member ref with no nested name qualifier"); |
| 12394 | |
| 12395 | // We have taken the address of a pointer to member |
| 12396 | // function. Perform the computation here so that we get the |
| 12397 | // appropriate pointer to member type. |
| 12398 | QualType ClassType |
| 12399 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 12400 | QualType MemPtrType |
| 12401 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 12402 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12403 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 12404 | VK_RValue, OK_Ordinary, |
| 12405 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 12406 | } |
| 12407 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12408 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 12409 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12410 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12411 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12412 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12413 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12414 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12415 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12416 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12417 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12418 | |
| 12419 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12420 | // FIXME: avoid copy. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12421 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12422 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12423 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 12424 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12425 | } |
| 12426 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12427 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 12428 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 12429 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12430 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 12431 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12432 | ULE->getNameLoc(), |
| 12433 | Fn->getType(), |
| 12434 | VK_LValue, |
| 12435 | Found.getDecl(), |
| 12436 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12437 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12438 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 12439 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12440 | } |
| 12441 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12442 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 12443 | // FIXME: avoid copy. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12444 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12445 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 12446 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 12447 | TemplateArgs = &TemplateArgsBuffer; |
| 12448 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 12449 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12450 | Expr *Base; |
| 12451 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12452 | // If we're filling in a static method where we used to have an |
| 12453 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12454 | if (MemExpr->isImplicitAccess()) { |
| 12455 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12456 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 12457 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 12458 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12459 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 12460 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12461 | MemExpr->getMemberLoc(), |
| 12462 | Fn->getType(), |
| 12463 | VK_LValue, |
| 12464 | Found.getDecl(), |
| 12465 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12466 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12467 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 12468 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 12469 | } else { |
| 12470 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 12471 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 12472 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 12473 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 12474 | Base = new (Context) CXXThisExpr(Loc, |
| 12475 | MemExpr->getBaseType(), |
| 12476 | /*isImplicit=*/true); |
| 12477 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12478 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12479 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12480 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 12481 | ExprValueKind valueKind; |
| 12482 | QualType type; |
| 12483 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 12484 | valueKind = VK_LValue; |
| 12485 | type = Fn->getType(); |
| 12486 | } else { |
| 12487 | valueKind = VK_RValue; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 12488 | type = Context.BoundMemberTy;
|
| 12489 | }
|
| 12490 |
|
| 12491 | MemberExpr *ME = MemberExpr::Create(
|
| 12492 | Context, Base, MemExpr->isArrow(), MemExpr->getOperatorLoc(),
|
| 12493 | MemExpr->getQualifierLoc(), MemExpr->getTemplateKeywordLoc(), Fn, Found,
|
| 12494 | MemExpr->getMemberNameInfo(), TemplateArgs, type, valueKind,
|
| 12495 | OK_Ordinary);
|
| 12496 | ME->setHadMultipleCandidates(true);
|
| 12497 | MarkMemberReferenced(ME);
|
| 12498 | return ME;
|
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12499 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12500 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12501 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12502 | } |
| 12503 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12504 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12505 | DeclAccessPair Found, |
| 12506 | FunctionDecl *Fn) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12507 | return FixOverloadedFunctionReference(E.get(), Found, Fn); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 12508 | } |