| Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading -------------------------------===// |
| Douglas Gregor | 5251f1b | 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 | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Overload.h" |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
| Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 23 | #include "clang/Basic/DiagnosticOptions.h" |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
| Chandler Carruth | 3a02247 | 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 | 2bbc026 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Optional.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
| Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallPtrSet.h" |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallString.h" |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | #include <algorithm> |
| David Blaikie | 8ad22e6 | 2014-05-01 23:01:41 +0000 | [diff] [blame] | 37 | #include <cstdlib> |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 38 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 39 | using namespace clang; |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 40 | using namespace sema; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 41 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 42 | static bool functionHasPassObjectSizeParams(const FunctionDecl *FD) { |
| George Burgess IV | 2108136 | 2016-07-24 23:12:40 +0000 | [diff] [blame] | 43 | return llvm::any_of(FD->parameters(), [](const ParmVarDecl *P) { |
| 44 | return P->hasAttr<PassObjectSizeAttr>(); |
| 45 | }); |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 48 | /// A convenience routine for creating a decayed reference to a function. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 49 | static ExprResult |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 50 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 51 | const Expr *Base, bool HadMultipleCandidates, |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 52 | SourceLocation Loc = SourceLocation(), |
| Douglas Gregor | e9d6293 | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 53 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
| Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 54 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 55 | return ExprError(); |
| Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 56 | // If FoundDecl is different from Fn (such as if one is a template |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 57 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 58 | // called on both. |
| 59 | // FIXME: This would be more comprehensively addressed by modifying |
| 60 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 61 | // being used. |
| 62 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
| Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 63 | return ExprError(); |
| Richard Smith | 5f4b388 | 2016-10-19 00:14:23 +0000 | [diff] [blame] | 64 | if (auto *FPT = Fn->getType()->getAs<FunctionProtoType>()) |
| 65 | S.ResolveExceptionSpec(Loc, FPT); |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 66 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 67 | VK_LValue, Loc, LocInfo); |
| 68 | if (HadMultipleCandidates) |
| 69 | DRE->setHadMultipleCandidates(true); |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 70 | |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 71 | S.MarkDeclRefReferenced(DRE, Base); |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 72 | return S.ImpCastExprToType(DRE, S.Context.getPointerType(DRE->getType()), |
| 73 | CK_FunctionToPointerDecay); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 76 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 77 | bool InOverloadResolution, |
| Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 78 | StandardConversionSequence &SCS, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 79 | bool CStyle, |
| 80 | bool AllowObjCWritebackConversion); |
| Sam Panzer | 04390a6 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 81 | |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 82 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 83 | QualType &ToType, |
| 84 | bool InOverloadResolution, |
| 85 | StandardConversionSequence &SCS, |
| 86 | bool CStyle); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 87 | static OverloadingResult |
| 88 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 89 | UserDefinedConversionSequence& User, |
| 90 | OverloadCandidateSet& Conversions, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 91 | bool AllowExplicit, |
| 92 | bool AllowObjCConversionOnExplicit); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | static ImplicitConversionSequence::CompareKind |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 96 | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 97 | const StandardConversionSequence& SCS1, |
| 98 | const StandardConversionSequence& SCS2); |
| 99 | |
| 100 | static ImplicitConversionSequence::CompareKind |
| 101 | CompareQualificationConversions(Sema &S, |
| 102 | const StandardConversionSequence& SCS1, |
| 103 | const StandardConversionSequence& SCS2); |
| 104 | |
| 105 | static ImplicitConversionSequence::CompareKind |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 106 | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 107 | const StandardConversionSequence& SCS1, |
| 108 | const StandardConversionSequence& SCS2); |
| 109 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 110 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 111 | /// corresponding to the given implicit conversion kind. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 112 | ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 113 | static const ImplicitConversionRank |
| 114 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 115 | ICR_Exact_Match, |
| 116 | ICR_Exact_Match, |
| 117 | ICR_Exact_Match, |
| 118 | ICR_Exact_Match, |
| 119 | ICR_Exact_Match, |
| Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 120 | ICR_Exact_Match, |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 121 | ICR_Promotion, |
| 122 | ICR_Promotion, |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 123 | ICR_Promotion, |
| 124 | ICR_Conversion, |
| 125 | ICR_Conversion, |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 126 | ICR_Conversion, |
| 127 | ICR_Conversion, |
| 128 | ICR_Conversion, |
| 129 | ICR_Conversion, |
| 130 | ICR_Conversion, |
| Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 131 | ICR_Conversion, |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 132 | ICR_Conversion, |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 133 | ICR_Conversion, |
| Egor Churaev | c217f37 | 2017-03-21 12:55:55 +0000 | [diff] [blame] | 134 | ICR_OCL_Scalar_Widening, |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 135 | ICR_Complex_Real_Conversion, |
| 136 | ICR_Conversion, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 137 | ICR_Conversion, |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 138 | ICR_Writeback_Conversion, |
| 139 | ICR_Exact_Match, // NOTE(gbiv): This may not be completely right -- |
| 140 | // it was omitted by the patch that added |
| 141 | // ICK_Zero_Event_Conversion |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 142 | ICR_C_Conversion, |
| 143 | ICR_C_Conversion_Extension |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 144 | }; |
| 145 | return Rank[(int)Kind]; |
| 146 | } |
| 147 | |
| 148 | /// GetImplicitConversionName - Return the name of this kind of |
| 149 | /// implicit conversion. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 150 | static const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 151 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 152 | "No conversion", |
| 153 | "Lvalue-to-rvalue", |
| 154 | "Array-to-pointer", |
| 155 | "Function-to-pointer", |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 156 | "Function pointer conversion", |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 157 | "Qualification", |
| 158 | "Integral promotion", |
| 159 | "Floating point promotion", |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 160 | "Complex promotion", |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 161 | "Integral conversion", |
| 162 | "Floating conversion", |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 163 | "Complex conversion", |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 164 | "Floating-integral conversion", |
| 165 | "Pointer conversion", |
| 166 | "Pointer-to-member conversion", |
| Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 167 | "Boolean conversion", |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 168 | "Compatible-types conversion", |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 169 | "Derived-to-base conversion", |
| 170 | "Vector conversion", |
| 171 | "Vector splat", |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 172 | "Complex-real conversion", |
| 173 | "Block Pointer conversion", |
| Sylvestre Ledru | 55635ce | 2014-11-17 19:41:49 +0000 | [diff] [blame] | 174 | "Transparent Union Conversion", |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 175 | "Writeback conversion", |
| 176 | "OpenCL Zero Event Conversion", |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 177 | "C specific type conversion", |
| 178 | "Incompatible pointer conversion" |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 179 | }; |
| 180 | return Name[Kind]; |
| 181 | } |
| 182 | |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 183 | /// StandardConversionSequence - Set the standard conversion |
| 184 | /// sequence to the identity conversion. |
| 185 | void StandardConversionSequence::setAsIdentityConversion() { |
| 186 | First = ICK_Identity; |
| 187 | Second = ICK_Identity; |
| 188 | Third = ICK_Identity; |
| Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 189 | DeprecatedStringLiteralToCharPtr = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 190 | QualificationIncludesObjCLifetime = false; |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 191 | ReferenceBinding = false; |
| 192 | DirectBinding = false; |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 193 | IsLvalueReference = true; |
| 194 | BindsToFunctionLvalue = false; |
| 195 | BindsToRvalue = false; |
| Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 196 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | ObjCLifetimeConversionBinding = false; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 198 | CopyConstructor = nullptr; |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 201 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 202 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 203 | /// implicit conversions. |
| 204 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 205 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 206 | if (GetConversionRank(First) > Rank) |
| 207 | Rank = GetConversionRank(First); |
| 208 | if (GetConversionRank(Second) > Rank) |
| 209 | Rank = GetConversionRank(Second); |
| 210 | if (GetConversionRank(Third) > Rank) |
| 211 | Rank = GetConversionRank(Third); |
| 212 | return Rank; |
| 213 | } |
| 214 | |
| 215 | /// isPointerConversionToBool - Determines whether this conversion is |
| 216 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | /// used as part of the ranking of standard conversion sequences |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | /// (C++ 13.3.3.2p4). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | bool StandardConversionSequence::isPointerConversionToBool() const { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 220 | // Note that FromType has not necessarily been transformed by the |
| 221 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 222 | // check for their presence as well as checking whether FromType is |
| 223 | // a pointer. |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 224 | if (getToType(1)->isBooleanType() && |
| John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 225 | (getFromType()->isPointerType() || |
| Erich Keane | 2fcbe92 | 2018-06-12 13:59:32 +0000 | [diff] [blame] | 226 | getFromType()->isMemberPointerType() || |
| John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 227 | getFromType()->isObjCObjectPointerType() || |
| 228 | getFromType()->isBlockPointerType() || |
| Anders Carlsson | 7da7cc5 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 229 | getFromType()->isNullPtrType() || |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 230 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 231 | return true; |
| 232 | |
| 233 | return false; |
| 234 | } |
| 235 | |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 236 | /// isPointerConversionToVoidPointer - Determines whether this |
| 237 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 238 | /// used as part of the ranking of standard conversion sequences (C++ |
| 239 | /// 13.3.3.2p4). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | bool |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 241 | StandardConversionSequence:: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 243 | QualType FromType = getFromType(); |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 244 | QualType ToType = getToType(1); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 245 | |
| 246 | // Note that FromType has not necessarily been transformed by the |
| 247 | // array-to-pointer implicit conversion, so check for its presence |
| 248 | // and redo the conversion to get a pointer. |
| 249 | if (First == ICK_Array_To_Pointer) |
| 250 | FromType = Context.getArrayDecayedType(FromType); |
| 251 | |
| Douglas Gregor | 5d3d3fa | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 252 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 253 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 254 | return ToPtrType->getPointeeType()->isVoidType(); |
| 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 259 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 260 | /// or after one in an implicit conversion. |
| 261 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 262 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 263 | switch (ICE->getCastKind()) { |
| 264 | case CK_NoOp: |
| 265 | case CK_IntegralCast: |
| 266 | case CK_IntegralToBoolean: |
| 267 | case CK_IntegralToFloating: |
| George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 268 | case CK_BooleanToSignedIntegral: |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 269 | case CK_FloatingToIntegral: |
| 270 | case CK_FloatingToBoolean: |
| 271 | case CK_FloatingCast: |
| 272 | Converted = ICE->getSubExpr(); |
| 273 | continue; |
| 274 | |
| 275 | default: |
| 276 | return Converted; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return Converted; |
| 281 | } |
| 282 | |
| 283 | /// Check if this standard conversion sequence represents a narrowing |
| 284 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 285 | /// |
| 286 | /// \param Ctx The AST context. |
| 287 | /// \param Converted The result of applying this standard conversion sequence. |
| 288 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 289 | /// value of the expression prior to the narrowing conversion. |
| Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 290 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 291 | /// type of the expression prior to the narrowing conversion. |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 292 | /// \param IgnoreFloatToIntegralConversion If true type-narrowing conversions |
| 293 | /// from floating point types to integral types should be ignored. |
| 294 | NarrowingKind StandardConversionSequence::getNarrowingKind( |
| 295 | ASTContext &Ctx, const Expr *Converted, APValue &ConstantValue, |
| 296 | QualType &ConstantType, bool IgnoreFloatToIntegralConversion) const { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 297 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 298 | |
| 299 | // C++11 [dcl.init.list]p7: |
| 300 | // A narrowing conversion is an implicit conversion ... |
| 301 | QualType FromType = getToType(0); |
| 302 | QualType ToType = getToType(1); |
| Richard Smith | ed63886 | 2016-03-28 06:08:37 +0000 | [diff] [blame] | 303 | |
| 304 | // A conversion to an enumeration type is narrowing if the conversion to |
| 305 | // the underlying type is narrowing. This only arises for expressions of |
| 306 | // the form 'Enum{init}'. |
| 307 | if (auto *ET = ToType->getAs<EnumType>()) |
| 308 | ToType = ET->getDecl()->getIntegerType(); |
| 309 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 310 | switch (Second) { |
| Richard Smith | 64ecacf | 2015-02-19 00:39:05 +0000 | [diff] [blame] | 311 | // 'bool' is an integral type; dispatch to the right place to handle it. |
| 312 | case ICK_Boolean_Conversion: |
| 313 | if (FromType->isRealFloatingType()) |
| 314 | goto FloatingIntegralConversion; |
| 315 | if (FromType->isIntegralOrUnscopedEnumerationType()) |
| 316 | goto IntegralConversion; |
| 317 | // Boolean conversions can be from pointers and pointers to members |
| 318 | // [conv.bool], and those aren't considered narrowing conversions. |
| 319 | return NK_Not_Narrowing; |
| 320 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 321 | // -- from a floating-point type to an integer type, or |
| 322 | // |
| 323 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 324 | // type, except where the source is a constant expression and the actual |
| 325 | // value after conversion will fit into the target type and will produce |
| 326 | // the original value when converted back to the original type, or |
| 327 | case ICK_Floating_Integral: |
| Richard Smith | 64ecacf | 2015-02-19 00:39:05 +0000 | [diff] [blame] | 328 | FloatingIntegralConversion: |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 329 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 330 | return NK_Type_Narrowing; |
| Mikhail Maltsev | 7b1a950 | 2018-02-21 10:08:18 +0000 | [diff] [blame] | 331 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
| 332 | ToType->isRealFloatingType()) { |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 333 | if (IgnoreFloatToIntegralConversion) |
| 334 | return NK_Not_Narrowing; |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 335 | llvm::APSInt IntConstantValue; |
| 336 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| Simon Pilgrim | f940987 | 2017-06-01 18:13:02 +0000 | [diff] [blame] | 337 | assert(Initializer && "Unknown conversion expression"); |
| Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 338 | |
| 339 | // If it's value-dependent, we can't tell whether it's narrowing. |
| 340 | if (Initializer->isValueDependent()) |
| 341 | return NK_Dependent_Narrowing; |
| 342 | |
| Simon Pilgrim | f940987 | 2017-06-01 18:13:02 +0000 | [diff] [blame] | 343 | if (Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 344 | // Convert the integer to the floating type. |
| 345 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 346 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 347 | llvm::APFloat::rmNearestTiesToEven); |
| 348 | // And back. |
| 349 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 350 | bool ignored; |
| 351 | Result.convertToInteger(ConvertedValue, |
| 352 | llvm::APFloat::rmTowardZero, &ignored); |
| 353 | // If the resulting value is different, this was a narrowing conversion. |
| 354 | if (IntConstantValue != ConvertedValue) { |
| 355 | ConstantValue = APValue(IntConstantValue); |
| Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 356 | ConstantType = Initializer->getType(); |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 357 | return NK_Constant_Narrowing; |
| 358 | } |
| 359 | } else { |
| 360 | // Variables are always narrowings. |
| 361 | return NK_Variable_Narrowing; |
| 362 | } |
| 363 | } |
| 364 | return NK_Not_Narrowing; |
| 365 | |
| 366 | // -- from long double to double or float, or from double to float, except |
| 367 | // where the source is a constant expression and the actual value after |
| 368 | // conversion is within the range of values that can be represented (even |
| 369 | // if it cannot be represented exactly), or |
| 370 | case ICK_Floating_Conversion: |
| 371 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 372 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 373 | // FromType is larger than ToType. |
| 374 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 375 | |
| 376 | // If it's value-dependent, we can't tell whether it's narrowing. |
| 377 | if (Initializer->isValueDependent()) |
| 378 | return NK_Dependent_Narrowing; |
| 379 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 380 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 381 | // Constant! |
| 382 | assert(ConstantValue.isFloat()); |
| 383 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 384 | // Convert the source value into the target type. |
| 385 | bool ignored; |
| 386 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 387 | Ctx.getFloatTypeSemantics(ToType), |
| 388 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 389 | // If there was no overflow, the source value is within the range of |
| 390 | // values that can be represented. |
| Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 391 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 392 | ConstantType = Initializer->getType(); |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 393 | return NK_Constant_Narrowing; |
| Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 394 | } |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 395 | } else { |
| 396 | return NK_Variable_Narrowing; |
| 397 | } |
| 398 | } |
| 399 | return NK_Not_Narrowing; |
| 400 | |
| 401 | // -- from an integer type or unscoped enumeration type to an integer type |
| 402 | // that cannot represent all the values of the original type, except where |
| 403 | // the source is a constant expression and the actual value after |
| 404 | // conversion will fit into the target type and will produce the original |
| 405 | // value when converted back to the original type. |
| Richard Smith | 64ecacf | 2015-02-19 00:39:05 +0000 | [diff] [blame] | 406 | case ICK_Integral_Conversion: |
| 407 | IntegralConversion: { |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 408 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 409 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 410 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 411 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 412 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 413 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 414 | |
| 415 | if (FromWidth > ToWidth || |
| Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 416 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 417 | (FromSigned && !ToSigned)) { |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 418 | // Not all values of FromType can be represented in ToType. |
| 419 | llvm::APSInt InitializerValue; |
| 420 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 421 | |
| 422 | // If it's value-dependent, we can't tell whether it's narrowing. |
| 423 | if (Initializer->isValueDependent()) |
| 424 | return NK_Dependent_Narrowing; |
| 425 | |
| Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 426 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 427 | // Such conversions on variables are always narrowing. |
| 428 | return NK_Variable_Narrowing; |
| Richard Smith | 72cd8ea | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 429 | } |
| 430 | bool Narrowing = false; |
| 431 | if (FromWidth < ToWidth) { |
| Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 432 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 433 | // narrowing. |
| 434 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
| Richard Smith | 72cd8ea | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 435 | Narrowing = true; |
| Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 436 | } else { |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 437 | // Add a bit to the InitializerValue so we don't have to worry about |
| 438 | // signed vs. unsigned comparisons. |
| 439 | InitializerValue = InitializerValue.extend( |
| 440 | InitializerValue.getBitWidth() + 1); |
| 441 | // Convert the initializer to and from the target width and signed-ness. |
| 442 | llvm::APSInt ConvertedValue = InitializerValue; |
| 443 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 444 | ConvertedValue.setIsSigned(ToSigned); |
| 445 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 446 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 447 | // If the result is different, this was a narrowing conversion. |
| Richard Smith | 72cd8ea | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 448 | if (ConvertedValue != InitializerValue) |
| 449 | Narrowing = true; |
| 450 | } |
| 451 | if (Narrowing) { |
| 452 | ConstantType = Initializer->getType(); |
| 453 | ConstantValue = APValue(InitializerValue); |
| 454 | return NK_Constant_Narrowing; |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | return NK_Not_Narrowing; |
| 458 | } |
| 459 | |
| 460 | default: |
| 461 | // Other kinds of conversions are not narrowings. |
| 462 | return NK_Not_Narrowing; |
| 463 | } |
| 464 | } |
| 465 | |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 466 | /// dump - Print this standard conversion sequence to standard |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 467 | /// error. Useful for debugging overloading issues. |
| Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 468 | LLVM_DUMP_METHOD void StandardConversionSequence::dump() const { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 469 | raw_ostream &OS = llvm::errs(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 470 | bool PrintedSomething = false; |
| 471 | if (First != ICK_Identity) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 472 | OS << GetImplicitConversionName(First); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 473 | PrintedSomething = true; |
| 474 | } |
| 475 | |
| 476 | if (Second != ICK_Identity) { |
| 477 | if (PrintedSomething) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 478 | OS << " -> "; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 479 | } |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 480 | OS << GetImplicitConversionName(Second); |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 481 | |
| 482 | if (CopyConstructor) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 483 | OS << " (by copy constructor)"; |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 484 | } else if (DirectBinding) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 485 | OS << " (direct reference binding)"; |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 486 | } else if (ReferenceBinding) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 487 | OS << " (reference binding)"; |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 488 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 489 | PrintedSomething = true; |
| 490 | } |
| 491 | |
| 492 | if (Third != ICK_Identity) { |
| 493 | if (PrintedSomething) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 494 | OS << " -> "; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | } |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 496 | OS << GetImplicitConversionName(Third); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 497 | PrintedSomething = true; |
| 498 | } |
| 499 | |
| 500 | if (!PrintedSomething) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 501 | OS << "No conversions required"; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 505 | /// dump - Print this user-defined conversion sequence to standard |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | /// error. Useful for debugging overloading issues. |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 507 | void UserDefinedConversionSequence::dump() const { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 508 | raw_ostream &OS = llvm::errs(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 509 | if (Before.First || Before.Second || Before.Third) { |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 510 | Before.dump(); |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 511 | OS << " -> "; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 512 | } |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 513 | if (ConversionFunction) |
| 514 | OS << '\'' << *ConversionFunction << '\''; |
| 515 | else |
| 516 | OS << "aggregate initialization"; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 517 | if (After.First || After.Second || After.Third) { |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 518 | OS << " -> "; |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 519 | After.dump(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 523 | /// dump - Print this implicit conversion sequence to standard |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | /// error. Useful for debugging overloading issues. |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 525 | void ImplicitConversionSequence::dump() const { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 526 | raw_ostream &OS = llvm::errs(); |
| Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 527 | if (isStdInitializerListElement()) |
| 528 | OS << "Worst std::initializer_list element conversion: "; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 529 | switch (ConversionKind) { |
| 530 | case StandardConversion: |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 531 | OS << "Standard conversion: "; |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 532 | Standard.dump(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | break; |
| 534 | case UserDefinedConversion: |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 535 | OS << "User-defined conversion: "; |
| Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 536 | UserDefined.dump(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 537 | break; |
| 538 | case EllipsisConversion: |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 539 | OS << "Ellipsis conversion"; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 540 | break; |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 541 | case AmbiguousConversion: |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 542 | OS << "Ambiguous conversion"; |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 543 | break; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 544 | case BadConversion: |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 545 | OS << "Bad conversion"; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | |
| Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 549 | OS << "\n"; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 552 | void AmbiguousConversionSequence::construct() { |
| 553 | new (&conversions()) ConversionSet(); |
| 554 | } |
| 555 | |
| 556 | void AmbiguousConversionSequence::destruct() { |
| 557 | conversions().~ConversionSet(); |
| 558 | } |
| 559 | |
| 560 | void |
| 561 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 562 | FromTypePtr = O.FromTypePtr; |
| 563 | ToTypePtr = O.ToTypePtr; |
| 564 | new (&conversions()) ConversionSet(O.conversions()); |
| 565 | } |
| 566 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 567 | namespace { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 568 | // Structure used by DeductionFailureInfo to store |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 569 | // template argument information. |
| 570 | struct DFIArguments { |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 571 | TemplateArgument FirstArg; |
| 572 | TemplateArgument SecondArg; |
| 573 | }; |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 574 | // Structure used by DeductionFailureInfo to store |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 575 | // template parameter and template argument information. |
| 576 | struct DFIParamWithArguments : DFIArguments { |
| 577 | TemplateParameter Param; |
| 578 | }; |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 579 | // Structure used by DeductionFailureInfo to store template argument |
| 580 | // information and the index of the problematic call argument. |
| 581 | struct DFIDeducedMismatchArgs : DFIArguments { |
| 582 | TemplateArgumentList *TemplateArgs; |
| 583 | unsigned CallArgIndex; |
| 584 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 585 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 586 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 587 | /// Convert from Sema's representation of template deduction information |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 588 | /// to the form used in overload-candidate information. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 589 | DeductionFailureInfo |
| 590 | clang::MakeDeductionFailureInfo(ASTContext &Context, |
| 591 | Sema::TemplateDeductionResult TDK, |
| 592 | TemplateDeductionInfo &Info) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 593 | DeductionFailureInfo Result; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 594 | Result.Result = static_cast<unsigned>(TDK); |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 595 | Result.HasDiagnostic = false; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 596 | switch (TDK) { |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 597 | case Sema::TDK_Invalid: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 598 | case Sema::TDK_InstantiationDepth: |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 599 | case Sema::TDK_TooManyArguments: |
| 600 | case Sema::TDK_TooFewArguments: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 601 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 602 | case Sema::TDK_CUDATargetMismatch: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 603 | Result.Data = nullptr; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 604 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 606 | case Sema::TDK_Incomplete: |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 607 | case Sema::TDK_InvalidExplicitArguments: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 608 | Result.Data = Info.Param.getOpaqueValue(); |
| 609 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 611 | case Sema::TDK_DeducedMismatch: |
| 612 | case Sema::TDK_DeducedMismatchNested: { |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 613 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 614 | auto *Saved = new (Context) DFIDeducedMismatchArgs; |
| 615 | Saved->FirstArg = Info.FirstArg; |
| 616 | Saved->SecondArg = Info.SecondArg; |
| 617 | Saved->TemplateArgs = Info.take(); |
| 618 | Saved->CallArgIndex = Info.CallArgIndex; |
| 619 | Result.Data = Saved; |
| 620 | break; |
| 621 | } |
| 622 | |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 623 | case Sema::TDK_NonDeducedMismatch: { |
| 624 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 625 | DFIArguments *Saved = new (Context) DFIArguments; |
| 626 | Saved->FirstArg = Info.FirstArg; |
| 627 | Saved->SecondArg = Info.SecondArg; |
| 628 | Result.Data = Saved; |
| 629 | break; |
| 630 | } |
| 631 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 632 | case Sema::TDK_Inconsistent: |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 633 | case Sema::TDK_Underqualified: { |
| Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 634 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 635 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 636 | Saved->Param = Info.Param; |
| 637 | Saved->FirstArg = Info.FirstArg; |
| 638 | Saved->SecondArg = Info.SecondArg; |
| 639 | Result.Data = Saved; |
| 640 | break; |
| 641 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 643 | case Sema::TDK_SubstitutionFailure: |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 644 | Result.Data = Info.take(); |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 645 | if (Info.hasSFINAEDiagnostic()) { |
| 646 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 647 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 648 | Info.takeSFINAEDiagnostic(*Diag); |
| 649 | Result.HasDiagnostic = true; |
| 650 | } |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 651 | break; |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 652 | |
| 653 | case Sema::TDK_Success: |
| 654 | case Sema::TDK_NonDependentConversionFailure: |
| 655 | llvm_unreachable("not a deduction failure"); |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 656 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 658 | return Result; |
| 659 | } |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 660 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 661 | void DeductionFailureInfo::Destroy() { |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 662 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 663 | case Sema::TDK_Success: |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 664 | case Sema::TDK_Invalid: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 665 | case Sema::TDK_InstantiationDepth: |
| 666 | case Sema::TDK_Incomplete: |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 667 | case Sema::TDK_TooManyArguments: |
| 668 | case Sema::TDK_TooFewArguments: |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 669 | case Sema::TDK_InvalidExplicitArguments: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 670 | case Sema::TDK_CUDATargetMismatch: |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 671 | case Sema::TDK_NonDependentConversionFailure: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 672 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 673 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 674 | case Sema::TDK_Inconsistent: |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 675 | case Sema::TDK_Underqualified: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 676 | case Sema::TDK_DeducedMismatch: |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 677 | case Sema::TDK_DeducedMismatchNested: |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 678 | case Sema::TDK_NonDeducedMismatch: |
| Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 679 | // FIXME: Destroy the data? |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 680 | Data = nullptr; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 681 | break; |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 682 | |
| 683 | case Sema::TDK_SubstitutionFailure: |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 684 | // FIXME: Destroy the template argument list? |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 685 | Data = nullptr; |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 686 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 687 | Diag->~PartialDiagnosticAt(); |
| 688 | HasDiagnostic = false; |
| 689 | } |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 690 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 691 | |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 692 | // Unhandled |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 693 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 694 | break; |
| 695 | } |
| 696 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 698 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 699 | if (HasDiagnostic) |
| 700 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 701 | return nullptr; |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 704 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 705 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 706 | case Sema::TDK_Success: |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 707 | case Sema::TDK_Invalid: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 708 | case Sema::TDK_InstantiationDepth: |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 709 | case Sema::TDK_TooManyArguments: |
| 710 | case Sema::TDK_TooFewArguments: |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 711 | case Sema::TDK_SubstitutionFailure: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 712 | case Sema::TDK_DeducedMismatch: |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 713 | case Sema::TDK_DeducedMismatchNested: |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 714 | case Sema::TDK_NonDeducedMismatch: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 715 | case Sema::TDK_CUDATargetMismatch: |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 716 | case Sema::TDK_NonDependentConversionFailure: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 717 | return TemplateParameter(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 718 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 719 | case Sema::TDK_Incomplete: |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 720 | case Sema::TDK_InvalidExplicitArguments: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 721 | return TemplateParameter::getFromOpaqueValue(Data); |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 722 | |
| 723 | case Sema::TDK_Inconsistent: |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 724 | case Sema::TDK_Underqualified: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 725 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 726 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 727 | // Unhandled |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 728 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 729 | break; |
| 730 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 731 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 732 | return TemplateParameter(); |
| 733 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 734 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 735 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 736 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 737 | case Sema::TDK_Success: |
| 738 | case Sema::TDK_Invalid: |
| 739 | case Sema::TDK_InstantiationDepth: |
| 740 | case Sema::TDK_TooManyArguments: |
| 741 | case Sema::TDK_TooFewArguments: |
| 742 | case Sema::TDK_Incomplete: |
| 743 | case Sema::TDK_InvalidExplicitArguments: |
| 744 | case Sema::TDK_Inconsistent: |
| 745 | case Sema::TDK_Underqualified: |
| 746 | case Sema::TDK_NonDeducedMismatch: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 747 | case Sema::TDK_CUDATargetMismatch: |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 748 | case Sema::TDK_NonDependentConversionFailure: |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 749 | return nullptr; |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 750 | |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 751 | case Sema::TDK_DeducedMismatch: |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 752 | case Sema::TDK_DeducedMismatchNested: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 753 | return static_cast<DFIDeducedMismatchArgs*>(Data)->TemplateArgs; |
| 754 | |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 755 | case Sema::TDK_SubstitutionFailure: |
| 756 | return static_cast<TemplateArgumentList*>(Data); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 758 | // Unhandled |
| 759 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 760 | break; |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 763 | return nullptr; |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 766 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 767 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 768 | case Sema::TDK_Success: |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 769 | case Sema::TDK_Invalid: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 770 | case Sema::TDK_InstantiationDepth: |
| 771 | case Sema::TDK_Incomplete: |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 772 | case Sema::TDK_TooManyArguments: |
| 773 | case Sema::TDK_TooFewArguments: |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 774 | case Sema::TDK_InvalidExplicitArguments: |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 775 | case Sema::TDK_SubstitutionFailure: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 776 | case Sema::TDK_CUDATargetMismatch: |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 777 | case Sema::TDK_NonDependentConversionFailure: |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 778 | return nullptr; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 779 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 780 | case Sema::TDK_Inconsistent: |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 781 | case Sema::TDK_Underqualified: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 782 | case Sema::TDK_DeducedMismatch: |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 783 | case Sema::TDK_DeducedMismatchNested: |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 784 | case Sema::TDK_NonDeducedMismatch: |
| 785 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 786 | |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 787 | // Unhandled |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 788 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 789 | break; |
| 790 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 791 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 792 | return nullptr; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 | } |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 794 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 795 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 796 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 797 | case Sema::TDK_Success: |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 798 | case Sema::TDK_Invalid: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 799 | case Sema::TDK_InstantiationDepth: |
| 800 | case Sema::TDK_Incomplete: |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 801 | case Sema::TDK_TooManyArguments: |
| 802 | case Sema::TDK_TooFewArguments: |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 803 | case Sema::TDK_InvalidExplicitArguments: |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 804 | case Sema::TDK_SubstitutionFailure: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 805 | case Sema::TDK_CUDATargetMismatch: |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 806 | case Sema::TDK_NonDependentConversionFailure: |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 807 | return nullptr; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 808 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 809 | case Sema::TDK_Inconsistent: |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 810 | case Sema::TDK_Underqualified: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 811 | case Sema::TDK_DeducedMismatch: |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 812 | case Sema::TDK_DeducedMismatchNested: |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 813 | case Sema::TDK_NonDeducedMismatch: |
| 814 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 815 | |
| Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 816 | // Unhandled |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 817 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 820 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 821 | return nullptr; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 824 | llvm::Optional<unsigned> DeductionFailureInfo::getCallArgIndex() { |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 825 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 826 | case Sema::TDK_DeducedMismatch: |
| 827 | case Sema::TDK_DeducedMismatchNested: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 828 | return static_cast<DFIDeducedMismatchArgs*>(Data)->CallArgIndex; |
| 829 | |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 830 | default: |
| 831 | return llvm::None; |
| 832 | } |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| Benjamin Kramer | 97e5949 | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 835 | void OverloadCandidateSet::destroyCandidates() { |
| Richard Smith | 0bf93aa | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 836 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 837 | for (auto &C : i->Conversions) |
| 838 | C.~ImplicitConversionSequence(); |
| Richard Smith | 0bf93aa | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 839 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 840 | i->DeductionFailure.Destroy(); |
| 841 | } |
| Benjamin Kramer | 97e5949 | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 844 | void OverloadCandidateSet::clear(CandidateSetKind CSK) { |
| Benjamin Kramer | 97e5949 | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 845 | destroyCandidates(); |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 846 | SlabAllocator.Reset(); |
| 847 | NumInlineBytesUsed = 0; |
| Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 848 | Candidates.clear(); |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 849 | Functions.clear(); |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 850 | Kind = CSK; |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 851 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 852 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 853 | namespace { |
| 854 | class UnbridgedCastsSet { |
| 855 | struct Entry { |
| 856 | Expr **Addr; |
| 857 | Expr *Saved; |
| 858 | }; |
| 859 | SmallVector<Entry, 2> Entries; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 860 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 861 | public: |
| 862 | void save(Sema &S, Expr *&E) { |
| 863 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 864 | Entry entry = { &E, E }; |
| 865 | Entries.push_back(entry); |
| 866 | E = S.stripARCUnbridgedCast(E); |
| 867 | } |
| 868 | |
| 869 | void restore() { |
| 870 | for (SmallVectorImpl<Entry>::iterator |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 871 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 872 | *i->Addr = i->Saved; |
| 873 | } |
| 874 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 875 | } |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 876 | |
| 877 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 878 | /// preprocessing on the given expression. |
| 879 | /// |
| 880 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 881 | /// without this, they will be immediately diagnosed as errors |
| 882 | /// |
| 883 | /// Return true on unrecoverable error. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 884 | static bool |
| 885 | checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 886 | UnbridgedCastsSet *unbridgedCasts = nullptr) { |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 887 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 888 | // We can't handle overloaded expressions here because overload |
| 889 | // resolution might reasonably tweak them. |
| 890 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 891 | |
| 892 | // If the context potentially accepts unbridged ARC casts, strip |
| 893 | // the unbridged cast and add it to the collection for later restoration. |
| 894 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 895 | unbridgedCasts) { |
| 896 | unbridgedCasts->save(S, E); |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | // Go ahead and check everything else. |
| 901 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 902 | if (result.isInvalid()) |
| 903 | return true; |
| 904 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 905 | E = result.get(); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 906 | return false; |
| 907 | } |
| 908 | |
| 909 | // Nothing to do. |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 914 | /// placeholders. |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 915 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 916 | MultiExprArg Args, |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 917 | UnbridgedCastsSet &unbridged) { |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 918 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 919 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 920 | return true; |
| 921 | |
| 922 | return false; |
| 923 | } |
| 924 | |
| George Burgess IV | 2d82b09 | 2017-04-06 00:23:31 +0000 | [diff] [blame] | 925 | /// Determine whether the given New declaration is an overload of the |
| 926 | /// declarations in Old. This routine returns Ovl_Match or Ovl_NonFunction if |
| 927 | /// New and Old cannot be overloaded, e.g., if New has the same signature as |
| 928 | /// some function in Old (C++ 1.3.10) or if the Old declarations aren't |
| 929 | /// functions (or function templates) at all. When it does return Ovl_Match or |
| 930 | /// Ovl_NonFunction, MatchedDecl will point to the decl that New cannot be |
| 931 | /// overloaded with. This decl may be a UsingShadowDecl on top of the underlying |
| 932 | /// declaration. |
| 933 | /// |
| 934 | /// Example: Given the following input: |
| 935 | /// |
| 936 | /// void f(int, float); // #1 |
| 937 | /// void f(int, int); // #2 |
| 938 | /// int f(int, int); // #3 |
| 939 | /// |
| 940 | /// When we process #1, there is no previous declaration of "f", so IsOverload |
| 941 | /// will not be used. |
| 942 | /// |
| 943 | /// When we process #2, Old contains only the FunctionDecl for #1. By comparing |
| 944 | /// the parameter types, we see that #1 and #2 are overloaded (since they have |
| 945 | /// different signatures), so this routine returns Ovl_Overload; MatchedDecl is |
| 946 | /// unchanged. |
| 947 | /// |
| 948 | /// When we process #3, Old is an overload set containing #1 and #2. We compare |
| 949 | /// the signatures of #3 to #1 (they're overloaded, so we do nothing) and then |
| 950 | /// #3 to #2. Since the signatures of #3 and #2 are identical (return types of |
| 951 | /// functions are not part of the signature), IsOverload returns Ovl_Match and |
| 952 | /// MatchedDecl will be set to point to the FunctionDecl for #2. |
| 953 | /// |
| 954 | /// 'NewIsUsingShadowDecl' indicates that 'New' is being introduced into a class |
| 955 | /// by a using declaration. The rules for whether to hide shadow declarations |
| 956 | /// ignore some properties which otherwise figure into a function template's |
| 957 | /// signature. |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 958 | Sema::OverloadKind |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 959 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 960 | NamedDecl *&Match, bool NewIsUsingDecl) { |
| John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 961 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 962 | I != E; ++I) { |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 963 | NamedDecl *OldD = *I; |
| 964 | |
| 965 | bool OldIsUsingDecl = false; |
| 966 | if (isa<UsingShadowDecl>(OldD)) { |
| 967 | OldIsUsingDecl = true; |
| 968 | |
| 969 | // We can always introduce two using declarations into the same |
| 970 | // context, even if they have identical signatures. |
| 971 | if (NewIsUsingDecl) continue; |
| 972 | |
| 973 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 974 | } |
| 975 | |
| Richard Smith | f091e12 | 2015-09-15 01:28:55 +0000 | [diff] [blame] | 976 | // A using-declaration does not conflict with another declaration |
| 977 | // if one of them is hidden. |
| 978 | if ((OldIsUsingDecl || NewIsUsingDecl) && !isVisible(*I)) |
| 979 | continue; |
| 980 | |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 981 | // If either declaration was introduced by a using declaration, |
| 982 | // we'll need to use slightly different rules for matching. |
| 983 | // Essentially, these rules are the normal rules, except that |
| 984 | // function templates hide function templates with different |
| 985 | // return types or template parameter lists. |
| 986 | bool UseMemberUsingDeclRules = |
| John McCall | c70fca6 | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 987 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 988 | !New->getFriendObjectKind(); |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 989 | |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 990 | if (FunctionDecl *OldF = OldD->getAsFunction()) { |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 991 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 992 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 993 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 994 | continue; |
| 995 | } |
| 996 | |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 997 | if (!isa<FunctionTemplateDecl>(OldD) && |
| 998 | !shouldLinkPossiblyHiddenDecl(*I, New)) |
| Rafael Espindola | 5bddd6a | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 999 | continue; |
| 1000 | |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 1001 | Match = *I; |
| 1002 | return Ovl_Match; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1003 | } |
| Erich Keane | 41af971 | 2018-04-16 21:30:08 +0000 | [diff] [blame] | 1004 | |
| 1005 | // Builtins that have custom typechecking or have a reference should |
| 1006 | // not be overloadable or redeclarable. |
| 1007 | if (!getASTContext().canBuiltinBeRedeclared(OldF)) { |
| 1008 | Match = *I; |
| 1009 | return Ovl_NonFunction; |
| 1010 | } |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 1011 | } else if (isa<UsingDecl>(OldD) || isa<UsingPackDecl>(OldD)) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1012 | // We can overload with these, which can show up when doing |
| 1013 | // redeclaration checks for UsingDecls. |
| 1014 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 1015 | } else if (isa<TagDecl>(OldD)) { |
| 1016 | // We can always overload with tags by hiding them. |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1017 | } else if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(OldD)) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1018 | // Optimistically assume that an unresolved using decl will |
| 1019 | // overload; if it doesn't, we'll have to diagnose during |
| 1020 | // template instantiation. |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1021 | // |
| 1022 | // Exception: if the scope is dependent and this is not a class |
| 1023 | // member, the using declaration can only introduce an enumerator. |
| 1024 | if (UUD->getQualifier()->isDependent() && !UUD->isCXXClassMember()) { |
| 1025 | Match = *I; |
| 1026 | return Ovl_NonFunction; |
| 1027 | } |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1028 | } else { |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1029 | // (C++ 13p1): |
| 1030 | // Only function declarations can be overloaded; object and type |
| 1031 | // declarations cannot be overloaded. |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 1032 | Match = *I; |
| 1033 | return Ovl_NonFunction; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1034 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1035 | } |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1036 | |
| John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 1037 | return Ovl_Overload; |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1040 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 1041 | bool UseMemberUsingDeclRules, bool ConsiderCudaAttrs) { |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1042 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 1043 | if (New->isMain()) |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1044 | return false; |
| Rafael Espindola | 7cf35ef | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 1045 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 1046 | // MSVCRT user defined entry points cannot be overloaded. |
| 1047 | if (New->isMSVCRTEntryPoint()) |
| 1048 | return false; |
| 1049 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1050 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 1051 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 1052 | |
| 1053 | // C++ [temp.fct]p2: |
| 1054 | // A function template can be overloaded with other function templates |
| 1055 | // and with normal (non-template) functions. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1056 | if ((OldTemplate == nullptr) != (NewTemplate == nullptr)) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1057 | return true; |
| 1058 | |
| 1059 | // Is the function New an overload of the function Old? |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1060 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 1061 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 1064 | // determine whether they are overloads. If we find any mismatch |
| 1065 | // in the signature, they are overloads. |
| 1066 | |
| 1067 | // If either of these functions is a K&R-style function (no |
| 1068 | // prototype), then we consider them to have matching signatures. |
| 1069 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1070 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1071 | return false; |
| 1072 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1073 | const FunctionProtoType *OldType = cast<FunctionProtoType>(OldQType); |
| 1074 | const FunctionProtoType *NewType = cast<FunctionProtoType>(NewQType); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1075 | |
| 1076 | // The signature of a function includes the types of its |
| 1077 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1078 | // of the ellipsis; see C++ DR 357). |
| 1079 | if (OldQType != NewQType && |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1080 | (OldType->getNumParams() != NewType->getNumParams() || |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1081 | OldType->isVariadic() != NewType->isVariadic() || |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1082 | !FunctionParamTypesAreEqual(OldType, NewType))) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1083 | return true; |
| 1084 | |
| 1085 | // C++ [temp.over.link]p4: |
| 1086 | // The signature of a function template consists of its function |
| 1087 | // signature, its return type and its template parameter list. The names |
| 1088 | // of the template parameters are significant only for establishing the |
| 1089 | // relationship between the template parameters and the rest of the |
| 1090 | // signature. |
| 1091 | // |
| 1092 | // We check the return type and template parameter lists for function |
| 1093 | // templates first; the remaining checks follow. |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1094 | // |
| 1095 | // However, we don't consider either of these when deciding whether |
| 1096 | // a member introduced by a shadow declaration is hidden. |
| Justin Lebar | 39fd529 | 2016-03-30 20:41:05 +0000 | [diff] [blame] | 1097 | if (!UseMemberUsingDeclRules && NewTemplate && |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1098 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1099 | OldTemplate->getTemplateParameters(), |
| 1100 | false, TPL_TemplateMatch) || |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1101 | OldType->getReturnType() != NewType->getReturnType())) |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1102 | return true; |
| 1103 | |
| 1104 | // If the function is a class member, its signature includes the |
| Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1105 | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1106 | // |
| 1107 | // As part of this, also check whether one of the member functions |
| 1108 | // is static, in which case they are not overloads (C++ |
| 1109 | // 13.1p2). While not part of the definition of the signature, |
| 1110 | // this check is important to determine whether these functions |
| 1111 | // can be overloaded. |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1112 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1113 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1114 | if (OldMethod && NewMethod && |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1115 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1116 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| Justin Lebar | 39fd529 | 2016-03-30 20:41:05 +0000 | [diff] [blame] | 1117 | if (!UseMemberUsingDeclRules && |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1118 | (OldMethod->getRefQualifier() == RQ_None || |
| 1119 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1120 | // C++0x [over.load]p2: |
| 1121 | // - Member function declarations with the same name and the same |
| 1122 | // parameter-type-list as well as member function template |
| 1123 | // declarations with the same name, the same parameter-type-list, and |
| 1124 | // the same template parameter lists cannot be overloaded if any of |
| 1125 | // them, but not all, have a ref-qualifier (8.3.5). |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1126 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1127 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1128 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1129 | } |
| 1130 | return true; |
| Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1131 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 | |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1133 | // We may not have applied the implicit const for a constexpr member |
| 1134 | // function yet (because we haven't yet resolved whether this is a static |
| 1135 | // or non-static member function). Add it now, on the assumption that this |
| 1136 | // is a redeclaration of OldMethod. |
| David Majnemer | 42350df | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1137 | unsigned OldQuals = OldMethod->getTypeQualifiers(); |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1138 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1139 | if (!getLangOpts().CPlusPlus14 && NewMethod->isConstexpr() && |
| Richard Smith | e83b1d3 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1140 | !isa<CXXConstructorDecl>(NewMethod)) |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1141 | NewQuals |= Qualifiers::Const; |
| David Majnemer | 42350df | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1142 | |
| 1143 | // We do not allow overloading based off of '__restrict'. |
| 1144 | OldQuals &= ~Qualifiers::Restrict; |
| 1145 | NewQuals &= ~Qualifiers::Restrict; |
| 1146 | if (OldQuals != NewQuals) |
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1147 | return true; |
| Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1148 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1149 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1150 | // Though pass_object_size is placed on parameters and takes an argument, we |
| 1151 | // consider it to be a function-level modifier for the sake of function |
| 1152 | // identity. Either the function has one or more parameters with |
| 1153 | // pass_object_size or it doesn't. |
| 1154 | if (functionHasPassObjectSizeParams(New) != |
| 1155 | functionHasPassObjectSizeParams(Old)) |
| 1156 | return true; |
| 1157 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1158 | // enable_if attributes are an order-sensitive part of the signature. |
| 1159 | for (specific_attr_iterator<EnableIfAttr> |
| 1160 | NewI = New->specific_attr_begin<EnableIfAttr>(), |
| 1161 | NewE = New->specific_attr_end<EnableIfAttr>(), |
| 1162 | OldI = Old->specific_attr_begin<EnableIfAttr>(), |
| 1163 | OldE = Old->specific_attr_end<EnableIfAttr>(); |
| 1164 | NewI != NewE || OldI != OldE; ++NewI, ++OldI) { |
| 1165 | if (NewI == NewE || OldI == OldE) |
| 1166 | return true; |
| 1167 | llvm::FoldingSetNodeID NewID, OldID; |
| 1168 | NewI->getCond()->Profile(NewID, Context, true); |
| 1169 | OldI->getCond()->Profile(OldID, Context, true); |
| Nick Lewycky | d950ae7 | 2014-01-21 01:30:30 +0000 | [diff] [blame] | 1170 | if (NewID != OldID) |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1171 | return true; |
| 1172 | } |
| 1173 | |
| Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 1174 | if (getLangOpts().CUDA && ConsiderCudaAttrs) { |
| Justin Lebar | e060feb | 2016-10-03 16:48:23 +0000 | [diff] [blame] | 1175 | // Don't allow overloading of destructors. (In theory we could, but it |
| 1176 | // would be a giant change to clang.) |
| 1177 | if (isa<CXXDestructorDecl>(New)) |
| 1178 | return false; |
| 1179 | |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 1180 | CUDAFunctionTarget NewTarget = IdentifyCUDATarget(New), |
| 1181 | OldTarget = IdentifyCUDATarget(Old); |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 1182 | if (NewTarget == CFT_InvalidTarget) |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 1183 | return false; |
| 1184 | |
| 1185 | assert((OldTarget != CFT_InvalidTarget) && "Unexpected invalid target."); |
| 1186 | |
| Justin Lebar | 53b000af | 2016-10-03 16:48:27 +0000 | [diff] [blame] | 1187 | // Allow overloading of functions with same signature and different CUDA |
| 1188 | // target attributes. |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 1189 | return NewTarget != OldTarget; |
| 1190 | } |
| 1191 | |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1192 | // The signatures match; this is not an overload. |
| 1193 | return false; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1196 | /// Checks availability of the function depending on the current |
| Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1197 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1198 | /// |
| 1199 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1200 | /// an available function, false otherwise. |
| 1201 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| Duncan P. N. Exon Smith | 8536392 | 2016-03-08 10:28:52 +0000 | [diff] [blame] | 1202 | if (!FD->isUnavailable()) |
| 1203 | return false; |
| 1204 | |
| 1205 | // Walk up the context of the caller. |
| 1206 | Decl *C = cast<Decl>(CurContext); |
| 1207 | do { |
| 1208 | if (C->isUnavailable()) |
| 1209 | return false; |
| 1210 | } while ((C = cast_or_null<Decl>(C->getDeclContext()))); |
| 1211 | return true; |
| Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1214 | /// Tries a user-defined conversion from From to ToType. |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1215 | /// |
| 1216 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1217 | /// is not an option. See TryImplicitConversion for more information. |
| 1218 | static ImplicitConversionSequence |
| 1219 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1220 | bool SuppressUserConversions, |
| 1221 | bool AllowExplicit, |
| 1222 | bool InOverloadResolution, |
| 1223 | bool CStyle, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1224 | bool AllowObjCWritebackConversion, |
| 1225 | bool AllowObjCConversionOnExplicit) { |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1226 | ImplicitConversionSequence ICS; |
| 1227 | |
| 1228 | if (SuppressUserConversions) { |
| 1229 | // We're not in the case above, so there is no conversion that |
| 1230 | // we can perform. |
| 1231 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1232 | return ICS; |
| 1233 | } |
| 1234 | |
| 1235 | // Attempt user-defined conversion. |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 1236 | OverloadCandidateSet Conversions(From->getExprLoc(), |
| 1237 | OverloadCandidateSet::CSK_Normal); |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 1238 | switch (IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, |
| 1239 | Conversions, AllowExplicit, |
| 1240 | AllowObjCConversionOnExplicit)) { |
| 1241 | case OR_Success: |
| 1242 | case OR_Deleted: |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1243 | ICS.setUserDefined(); |
| 1244 | // C++ [over.ics.user]p4: |
| 1245 | // A conversion of an expression of class type to the same class |
| 1246 | // type is given Exact Match rank, and a conversion of an |
| 1247 | // expression of class type to a base class of that type is |
| 1248 | // given Conversion rank, in spite of the fact that a copy |
| 1249 | // constructor (i.e., a user-defined conversion function) is |
| 1250 | // called for those cases. |
| 1251 | if (CXXConstructorDecl *Constructor |
| 1252 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1253 | QualType FromCanon |
| 1254 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1255 | QualType ToCanon |
| 1256 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1257 | if (Constructor->isCopyConstructor() && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 1258 | (FromCanon == ToCanon || |
| 1259 | S.IsDerivedFrom(From->getLocStart(), FromCanon, ToCanon))) { |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1260 | // Turn this into a "standard" conversion sequence, so that it |
| 1261 | // gets ranked with standard conversion sequences. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 1262 | DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction; |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1263 | ICS.setStandard(); |
| 1264 | ICS.Standard.setAsIdentityConversion(); |
| 1265 | ICS.Standard.setFromType(From->getType()); |
| 1266 | ICS.Standard.setAllToTypes(ToType); |
| 1267 | ICS.Standard.CopyConstructor = Constructor; |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 1268 | ICS.Standard.FoundCopyConstructor = Found; |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1269 | if (ToCanon != FromCanon) |
| 1270 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1271 | } |
| 1272 | } |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 1273 | break; |
| 1274 | |
| 1275 | case OR_Ambiguous: |
| Richard Smith | 1bbaba8 | 2015-01-27 23:23:39 +0000 | [diff] [blame] | 1276 | ICS.setAmbiguous(); |
| 1277 | ICS.Ambiguous.setFromType(From->getType()); |
| 1278 | ICS.Ambiguous.setToType(ToType); |
| 1279 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1280 | Cand != Conversions.end(); ++Cand) |
| 1281 | if (Cand->Viable) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 1282 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
| Richard Smith | 1bbaba8 | 2015-01-27 23:23:39 +0000 | [diff] [blame] | 1283 | break; |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Fall through. |
| 1286 | case OR_No_Viable_Function: |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1287 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 1288 | break; |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | return ICS; |
| 1292 | } |
| 1293 | |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1294 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1295 | /// from the given expression (Expr) to the given type (ToType). This |
| 1296 | /// function returns an implicit conversion sequence that can be used |
| 1297 | /// to perform the initialization. Given |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1298 | /// |
| 1299 | /// void f(float f); |
| 1300 | /// void g(int i) { f(i); } |
| 1301 | /// |
| 1302 | /// this routine would produce an implicit conversion sequence to |
| 1303 | /// describe the initialization of f from i, which will be a standard |
| 1304 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1305 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1306 | // |
| 1307 | /// Note that this routine only determines how the conversion can be |
| 1308 | /// performed; it does not actually perform the conversion. As such, |
| 1309 | /// it will not produce any diagnostics if no conversion is available, |
| 1310 | /// but will instead return an implicit conversion sequence of kind |
| 1311 | /// "BadConversion". |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1312 | /// |
| 1313 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1314 | /// not permitted. |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1315 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1316 | /// permitted. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1317 | /// |
| 1318 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1319 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1320 | /// be initialized with __strong id* or __weak id* arguments. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1321 | static ImplicitConversionSequence |
| 1322 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1323 | bool SuppressUserConversions, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1324 | bool AllowExplicit, |
| Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1325 | bool InOverloadResolution, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1326 | bool CStyle, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1327 | bool AllowObjCWritebackConversion, |
| 1328 | bool AllowObjCConversionOnExplicit) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1329 | ImplicitConversionSequence ICS; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1330 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1331 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1332 | ICS.setStandard(); |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1333 | return ICS; |
| 1334 | } |
| 1335 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1336 | if (!S.getLangOpts().CPlusPlus) { |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1337 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1338 | return ICS; |
| 1339 | } |
| 1340 | |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1341 | // C++ [over.ics.user]p4: |
| 1342 | // A conversion of an expression of class type to the same class |
| 1343 | // type is given Exact Match rank, and a conversion of an |
| 1344 | // expression of class type to a base class of that type is |
| 1345 | // given Conversion rank, in spite of the fact that a copy/move |
| 1346 | // constructor (i.e., a user-defined conversion function) is |
| 1347 | // called for those cases. |
| 1348 | QualType FromType = From->getType(); |
| 1349 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1350 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 1351 | S.IsDerivedFrom(From->getLocStart(), FromType, ToType))) { |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1352 | ICS.setStandard(); |
| 1353 | ICS.Standard.setAsIdentityConversion(); |
| 1354 | ICS.Standard.setFromType(FromType); |
| 1355 | ICS.Standard.setAllToTypes(ToType); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1357 | // We don't actually check at this point whether there is a valid |
| 1358 | // copy/move constructor, since overloading just assumes that it |
| 1359 | // exists. When we actually perform initialization, we'll find the |
| 1360 | // appropriate constructor to copy the returned object, if needed. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1361 | ICS.Standard.CopyConstructor = nullptr; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1362 | |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1363 | // Determine whether this is considered a derived-to-base conversion. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1364 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1365 | ICS.Standard.Second = ICK_Derived_To_Base; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1366 | |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1367 | return ICS; |
| 1368 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1369 | |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1370 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1371 | AllowExplicit, InOverloadResolution, CStyle, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1372 | AllowObjCWritebackConversion, |
| 1373 | AllowObjCConversionOnExplicit); |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1376 | ImplicitConversionSequence |
| 1377 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1378 | bool SuppressUserConversions, |
| 1379 | bool AllowExplicit, |
| 1380 | bool InOverloadResolution, |
| 1381 | bool CStyle, |
| 1382 | bool AllowObjCWritebackConversion) { |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1383 | return ::TryImplicitConversion(*this, From, ToType, |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 1384 | SuppressUserConversions, AllowExplicit, |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1385 | InOverloadResolution, CStyle, |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 1386 | AllowObjCWritebackConversion, |
| 1387 | /*AllowObjCConversionOnExplicit=*/false); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1390 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1391 | /// expression From to the type ToType. Returns the |
| Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1392 | /// converted expression. Flavor is the kind of conversion we're |
| 1393 | /// performing, used in the error message. If @p AllowExplicit, |
| 1394 | /// explicit user-defined conversions are permitted. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1395 | ExprResult |
| 1396 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
| Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1397 | AssignmentAction Action, bool AllowExplicit) { |
| Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1398 | ImplicitConversionSequence ICS; |
| Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1399 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1402 | ExprResult |
| 1403 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
| Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1404 | AssignmentAction Action, bool AllowExplicit, |
| Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1405 | ImplicitConversionSequence& ICS) { |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1406 | if (checkPlaceholderForOverload(*this, From)) |
| 1407 | return ExprError(); |
| 1408 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1409 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1410 | bool AllowObjCWritebackConversion |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1411 | = getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1412 | (Action == AA_Passing || Action == AA_Sending); |
| Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 1413 | if (getLangOpts().ObjC1) |
| 1414 | CheckObjCBridgeRelatedConversions(From->getLocStart(), |
| 1415 | ToType, From->getType(), From); |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 1416 | ICS = ::TryImplicitConversion(*this, From, ToType, |
| 1417 | /*SuppressUserConversions=*/false, |
| 1418 | AllowExplicit, |
| 1419 | /*InOverloadResolution=*/false, |
| 1420 | /*CStyle=*/false, |
| 1421 | AllowObjCWritebackConversion, |
| 1422 | /*AllowObjCConversionOnExplicit=*/false); |
| Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1423 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1424 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1425 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1426 | /// Determine whether the conversion from FromType to ToType is a valid |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1427 | /// conversion that strips "noexcept" or "noreturn" off the nested function |
| 1428 | /// type. |
| 1429 | bool Sema::IsFunctionConversion(QualType FromType, QualType ToType, |
| Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1430 | QualType &ResultTy) { |
| Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1431 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1432 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1433 | |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1434 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1435 | // or F(t noexcept) -> F(t) |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1436 | // where F adds one of the following at most once: |
| 1437 | // - a pointer |
| 1438 | // - a member pointer |
| 1439 | // - a block pointer |
| Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 1440 | // Changes here need matching changes in FindCompositePointerType. |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1441 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1442 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1443 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1444 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1445 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1446 | if (TyClass == Type::Pointer) { |
| 1447 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1448 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1449 | } else if (TyClass == Type::BlockPointer) { |
| 1450 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1451 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1452 | } else if (TyClass == Type::MemberPointer) { |
| Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 1453 | auto ToMPT = CanTo.getAs<MemberPointerType>(); |
| 1454 | auto FromMPT = CanFrom.getAs<MemberPointerType>(); |
| 1455 | // A function pointer conversion cannot change the class of the function. |
| 1456 | if (ToMPT->getClass() != FromMPT->getClass()) |
| 1457 | return false; |
| 1458 | CanTo = ToMPT->getPointeeType(); |
| 1459 | CanFrom = FromMPT->getPointeeType(); |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1460 | } else { |
| 1461 | return false; |
| 1462 | } |
| Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1463 | |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1464 | TyClass = CanTo->getTypeClass(); |
| 1465 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1466 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1467 | return false; |
| 1468 | } |
| 1469 | |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1470 | const auto *FromFn = cast<FunctionType>(CanFrom); |
| 1471 | FunctionType::ExtInfo FromEInfo = FromFn->getExtInfo(); |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1472 | |
| Richard Smith | 6f42740 | 2016-10-20 00:01:36 +0000 | [diff] [blame] | 1473 | const auto *ToFn = cast<FunctionType>(CanTo); |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1474 | FunctionType::ExtInfo ToEInfo = ToFn->getExtInfo(); |
| 1475 | |
| 1476 | bool Changed = false; |
| 1477 | |
| 1478 | // Drop 'noreturn' if not present in target type. |
| 1479 | if (FromEInfo.getNoReturn() && !ToEInfo.getNoReturn()) { |
| 1480 | FromFn = Context.adjustFunctionType(FromFn, FromEInfo.withNoReturn(false)); |
| 1481 | Changed = true; |
| 1482 | } |
| 1483 | |
| 1484 | // Drop 'noexcept' if not present in target type. |
| 1485 | if (const auto *FromFPT = dyn_cast<FunctionProtoType>(FromFn)) { |
| Richard Smith | 6f42740 | 2016-10-20 00:01:36 +0000 | [diff] [blame] | 1486 | const auto *ToFPT = cast<FunctionProtoType>(ToFn); |
| Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 1487 | if (FromFPT->isNothrow() && !ToFPT->isNothrow()) { |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1488 | FromFn = cast<FunctionType>( |
| Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 1489 | Context.getFunctionTypeWithExceptionSpec(QualType(FromFPT, 0), |
| 1490 | EST_None) |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1491 | .getTypePtr()); |
| 1492 | Changed = true; |
| 1493 | } |
| Artem Belevich | 55ebd6c | 2018-04-03 18:29:31 +0000 | [diff] [blame] | 1494 | |
| Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 1495 | // Convert FromFPT's ExtParameterInfo if necessary. The conversion is valid |
| 1496 | // only if the ExtParameterInfo lists of the two function prototypes can be |
| 1497 | // merged and the merged list is identical to ToFPT's ExtParameterInfo list. |
| 1498 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
| 1499 | bool CanUseToFPT, CanUseFromFPT; |
| 1500 | if (Context.mergeExtParameterInfo(ToFPT, FromFPT, CanUseToFPT, |
| 1501 | CanUseFromFPT, NewParamInfos) && |
| 1502 | CanUseToFPT && !CanUseFromFPT) { |
| 1503 | FunctionProtoType::ExtProtoInfo ExtInfo = FromFPT->getExtProtoInfo(); |
| 1504 | ExtInfo.ExtParameterInfos = |
| 1505 | NewParamInfos.empty() ? nullptr : NewParamInfos.data(); |
| 1506 | QualType QT = Context.getFunctionType(FromFPT->getReturnType(), |
| 1507 | FromFPT->getParamTypes(), ExtInfo); |
| 1508 | FromFn = QT->getAs<FunctionType>(); |
| 1509 | Changed = true; |
| 1510 | } |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | if (!Changed) |
| 1514 | return false; |
| 1515 | |
| John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1516 | assert(QualType(FromFn, 0).isCanonical()); |
| 1517 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1518 | |
| 1519 | ResultTy = ToType; |
| Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1520 | return true; |
| 1521 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1522 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1523 | /// Determine whether the conversion from FromType to ToType is a valid |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1524 | /// vector conversion. |
| 1525 | /// |
| 1526 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1527 | /// conversion. |
| John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1528 | static bool IsVectorConversion(Sema &S, QualType FromType, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1529 | QualType ToType, ImplicitConversionKind &ICK) { |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1530 | // We need at least one of these types to be a vector type to have a vector |
| 1531 | // conversion. |
| 1532 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1533 | return false; |
| 1534 | |
| 1535 | // Identical types require no conversions. |
| John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1536 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1537 | return false; |
| 1538 | |
| 1539 | // There are no conversions between extended vector types, only identity. |
| 1540 | if (ToType->isExtVectorType()) { |
| 1541 | // There are no conversions between extended vector types other than the |
| 1542 | // identity conversion. |
| 1543 | if (FromType->isExtVectorType()) |
| 1544 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1545 | |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1546 | // Vector splat from any arithmetic type to a vector. |
| Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1547 | if (FromType->isArithmeticType()) { |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1548 | ICK = ICK_Vector_Splat; |
| 1549 | return true; |
| 1550 | } |
| 1551 | } |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1552 | |
| 1553 | // We can perform the conversion between vector types in the following cases: |
| 1554 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1555 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1556 | // same size |
| 1557 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1558 | if (S.Context.areCompatibleVectorTypes(FromType, ToType) || |
| 1559 | S.isLaxVectorConversion(FromType, ToType)) { |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1560 | ICK = ICK_Vector_Conversion; |
| 1561 | return true; |
| 1562 | } |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1563 | } |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1564 | |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1565 | return false; |
| 1566 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1567 | |
| Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1568 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1569 | bool InOverloadResolution, |
| 1570 | StandardConversionSequence &SCS, |
| 1571 | bool CStyle); |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1572 | |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1573 | /// IsStandardConversion - Determines whether there is a standard |
| 1574 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1575 | /// expression From to the type ToType. Standard conversion sequences |
| 1576 | /// only consider non-class types; for conversions that involve class |
| 1577 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1578 | /// contain the standard conversion sequence required to perform this |
| 1579 | /// conversion and this routine will return true. Otherwise, this |
| 1580 | /// routine will return false and the value of SCS is unspecified. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1581 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1582 | bool InOverloadResolution, |
| Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1583 | StandardConversionSequence &SCS, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1584 | bool CStyle, |
| 1585 | bool AllowObjCWritebackConversion) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1586 | QualType FromType = From->getType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1587 | |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1588 | // Standard conversions (C++ [conv]) |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1589 | SCS.setAsIdentityConversion(); |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1590 | SCS.IncompatibleObjC = false; |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1591 | SCS.setFromType(FromType); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1592 | SCS.CopyConstructor = nullptr; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1593 | |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1594 | // There are no standard conversions for class types in C++, so |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1595 | // abort early. When overloading in C, however, we do permit them. |
| 1596 | if (S.getLangOpts().CPlusPlus && |
| 1597 | (FromType->isRecordType() || ToType->isRecordType())) |
| 1598 | return false; |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1599 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1600 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1601 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1602 | // (C++ 4p1). |
| 1603 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1604 | if (FromType == S.Context.OverloadTy) { |
| Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1605 | DeclAccessPair AccessPair; |
| 1606 | if (FunctionDecl *Fn |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1607 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1608 | AccessPair)) { |
| Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1609 | // We were able to resolve the address of the overloaded function, |
| 1610 | // so we can convert to the type of that function. |
| 1611 | FromType = Fn->getType(); |
| Ehsan Akhgari | c3ad3ba | 2014-07-22 20:20:14 +0000 | [diff] [blame] | 1612 | SCS.setFromType(FromType); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1613 | |
| 1614 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1615 | // if the type matches (identity) or we are converting to bool |
| 1616 | if (!S.Context.hasSameUnqualifiedType( |
| 1617 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1618 | QualType resultTy; |
| 1619 | // if the function type matches except for [[noreturn]], it's ok |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1620 | if (!S.IsFunctionConversion(FromType, |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1621 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1622 | // otherwise, only a boolean conversion is standard |
| 1623 | if (!ToType->isBooleanType()) |
| 1624 | return false; |
| Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1625 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1626 | |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1627 | // Check if the "from" expression is taking the address of an overloaded |
| 1628 | // function and recompute the FromType accordingly. Take advantage of the |
| 1629 | // fact that non-static member functions *must* have such an address-of |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1630 | // expression. |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1631 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1632 | if (Method && !Method->isStatic()) { |
| 1633 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1634 | "Non-unary operator on non-static member address"); |
| 1635 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1636 | == UO_AddrOf && |
| 1637 | "Non-address-of operator on non-static member address"); |
| 1638 | const Type *ClassType |
| 1639 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1640 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
| Chandler Carruth | 7750f76 | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1641 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1642 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1643 | UO_AddrOf && |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1644 | "Non-address-of operator for overloaded function expression"); |
| 1645 | FromType = S.Context.getPointerType(FromType); |
| 1646 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1647 | |
| Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1648 | // Check that we've computed the proper type after overload resolution. |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 1649 | // FIXME: FixOverloadedFunctionReference has side-effects; we shouldn't |
| 1650 | // be calling it from within an NDEBUG block. |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1651 | assert(S.Context.hasSameType( |
| 1652 | FromType, |
| 1653 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
| Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1654 | } else { |
| 1655 | return false; |
| 1656 | } |
| Anders Carlsson | ba37e1e | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1657 | } |
| John McCall | 154a2fd | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1658 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1659 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1660 | // be converted to a prvalue. |
| 1661 | bool argIsLValue = From->isGLValue(); |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1662 | if (argIsLValue && |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1663 | !FromType->isFunctionType() && !FromType->isArrayType() && |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1664 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1665 | SCS.First = ICK_Lvalue_To_Rvalue; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1666 | |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1667 | // C11 6.3.2.1p2: |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1668 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1669 | // of the type of the lvalue ... |
| 1670 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1671 | FromType = Atomic->getValueType(); |
| 1672 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1673 | // If T is a non-class type, the type of the rvalue is the |
| 1674 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1675 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1676 | // just strip the qualifiers because they don't matter. |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1677 | FromType = FromType.getUnqualifiedType(); |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1678 | } else if (FromType->isArrayType()) { |
| 1679 | // Array-to-pointer conversion (C++ 4.2) |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1680 | SCS.First = ICK_Array_To_Pointer; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1681 | |
| 1682 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1683 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1684 | // T" (C++ 4.2p1). |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1685 | FromType = S.Context.getArrayDecayedType(FromType); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1686 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1687 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 1688 | // This conversion is deprecated in C++03 (D.4) |
| Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1689 | SCS.DeprecatedStringLiteralToCharPtr = true; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1690 | |
| 1691 | // For the purpose of ranking in overload resolution |
| 1692 | // (13.3.3.1.1), this conversion is considered an |
| 1693 | // array-to-pointer conversion followed by a qualification |
| 1694 | // conversion (4.4). (C++ 4.2p2) |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1695 | SCS.Second = ICK_Identity; |
| 1696 | SCS.Third = ICK_Qualification; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1697 | SCS.QualificationIncludesObjCLifetime = false; |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1698 | SCS.setAllToTypes(FromType); |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1699 | return true; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1700 | } |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1701 | } else if (FromType->isFunctionType() && argIsLValue) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1702 | // Function-to-pointer conversion (C++ 4.3). |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1703 | SCS.First = ICK_Function_To_Pointer; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1704 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1705 | if (auto *DRE = dyn_cast<DeclRefExpr>(From->IgnoreParenCasts())) |
| 1706 | if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) |
| 1707 | if (!S.checkAddressOfFunctionIsAvailable(FD)) |
| 1708 | return false; |
| 1709 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1710 | // An lvalue of function type T can be converted to an rvalue of |
| 1711 | // type "pointer to T." The result is a pointer to the |
| 1712 | // function. (C++ 4.3p1). |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1713 | FromType = S.Context.getPointerType(FromType); |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1714 | } else { |
| 1715 | // We don't require any conversions for the first step. |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1716 | SCS.First = ICK_Identity; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1717 | } |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1718 | SCS.setToType(0, FromType); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1719 | |
| 1720 | // The second conversion can be an integral promotion, floating |
| 1721 | // point promotion, integral conversion, floating point conversion, |
| 1722 | // floating-integral conversion, pointer conversion, |
| 1723 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1724 | // For overloading in C, this can also be a "compatible-type" |
| 1725 | // conversion. |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1726 | bool IncompatibleObjC = false; |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1727 | ImplicitConversionKind SecondICK = ICK_Identity; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1728 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1729 | // The unqualified versions of the types are the same: there's no |
| 1730 | // conversion to do. |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1731 | SCS.Second = ICK_Identity; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1732 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | // Integral promotion (C++ 4.5). |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1734 | SCS.Second = ICK_Integral_Promotion; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1735 | FromType = ToType.getUnqualifiedType(); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1736 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1737 | // Floating point promotion (C++ 4.6). |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1738 | SCS.Second = ICK_Floating_Promotion; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1739 | FromType = ToType.getUnqualifiedType(); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1740 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1741 | // Complex promotion (Clang extension) |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1742 | SCS.Second = ICK_Complex_Promotion; |
| 1743 | FromType = ToType.getUnqualifiedType(); |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1744 | } else if (ToType->isBooleanType() && |
| 1745 | (FromType->isArithmeticType() || |
| 1746 | FromType->isAnyPointerType() || |
| 1747 | FromType->isBlockPointerType() || |
| 1748 | FromType->isMemberPointerType() || |
| 1749 | FromType->isNullPtrType())) { |
| 1750 | // Boolean conversions (C++ 4.12). |
| 1751 | SCS.Second = ICK_Boolean_Conversion; |
| 1752 | FromType = S.Context.BoolTy; |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1753 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1754 | ToType->isIntegralType(S.Context)) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1755 | // Integral conversions (C++ 4.7). |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1756 | SCS.Second = ICK_Integral_Conversion; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1757 | FromType = ToType.getUnqualifiedType(); |
| Richard Smith | b8a9824 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1758 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1759 | // Complex conversions (C99 6.3.1.6) |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1760 | SCS.Second = ICK_Complex_Conversion; |
| 1761 | FromType = ToType.getUnqualifiedType(); |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1762 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1763 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
| Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1764 | // Complex-real conversions (C99 6.3.1.7) |
| 1765 | SCS.Second = ICK_Complex_Real; |
| 1766 | FromType = ToType.getUnqualifiedType(); |
| Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1767 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
| Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1768 | // FIXME: disable conversions between long double and __float128 if |
| 1769 | // their representation is different until there is back end support |
| 1770 | // We of course allow this conversion if long double is really double. |
| 1771 | if (&S.Context.getFloatTypeSemantics(FromType) != |
| 1772 | &S.Context.getFloatTypeSemantics(ToType)) { |
| 1773 | bool Float128AndLongDouble = ((FromType == S.Context.Float128Ty && |
| 1774 | ToType == S.Context.LongDoubleTy) || |
| 1775 | (FromType == S.Context.LongDoubleTy && |
| 1776 | ToType == S.Context.Float128Ty)); |
| 1777 | if (Float128AndLongDouble && |
| Benjamin Kramer | 9805795 | 2018-01-17 22:56:57 +0000 | [diff] [blame] | 1778 | (&S.Context.getFloatTypeSemantics(S.Context.LongDoubleTy) == |
| 1779 | &llvm::APFloat::PPCDoubleDouble())) |
| Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1780 | return false; |
| 1781 | } |
| Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1782 | // Floating point conversions (C++ 4.8). |
| 1783 | SCS.Second = ICK_Floating_Conversion; |
| 1784 | FromType = ToType.getUnqualifiedType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1785 | } else if ((FromType->isRealFloatingType() && |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1786 | ToType->isIntegralType(S.Context)) || |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1787 | (FromType->isIntegralOrUnscopedEnumerationType() && |
| Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1788 | ToType->isRealFloatingType())) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1789 | // Floating-integral conversions (C++ 4.9). |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1790 | SCS.Second = ICK_Floating_Integral; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1791 | FromType = ToType.getUnqualifiedType(); |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1792 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1793 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1794 | } else if (AllowObjCWritebackConversion && |
| 1795 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1796 | SCS.Second = ICK_Writeback_Conversion; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1797 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1798 | FromType, IncompatibleObjC)) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1799 | // Pointer conversions (C++ 4.10). |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1800 | SCS.Second = ICK_Pointer_Conversion; |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1801 | SCS.IncompatibleObjC = IncompatibleObjC; |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1802 | FromType = FromType.getUnqualifiedType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1803 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1804 | InOverloadResolution, FromType)) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1805 | // Pointer to member conversions (4.11). |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1806 | SCS.Second = ICK_Pointer_Member; |
| John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1807 | } else if (IsVectorConversion(S, FromType, ToType, SecondICK)) { |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1808 | SCS.Second = SecondICK; |
| 1809 | FromType = ToType.getUnqualifiedType(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1810 | } else if (!S.getLangOpts().CPlusPlus && |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1811 | S.Context.typesAreCompatible(ToType, FromType)) { |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1812 | // Compatible conversions (Clang extension for C function overloading) |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1813 | SCS.Second = ICK_Compatible_Conversion; |
| Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1814 | FromType = ToType.getUnqualifiedType(); |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1815 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1816 | InOverloadResolution, |
| 1817 | SCS, CStyle)) { |
| 1818 | SCS.Second = ICK_TransparentUnionConversion; |
| 1819 | FromType = ToType; |
| Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1820 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1821 | CStyle)) { |
| 1822 | // tryAtomicConversion has updated the standard conversion sequence |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1823 | // appropriately. |
| 1824 | return true; |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1825 | } else if (ToType->isEventT() && |
| Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1826 | From->isIntegerConstantExpr(S.getASTContext()) && |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1827 | From->EvaluateKnownConstInt(S.getASTContext()) == 0) { |
| Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1828 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1829 | FromType = ToType; |
| Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 1830 | } else if (ToType->isQueueT() && |
| 1831 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1832 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1833 | SCS.Second = ICK_Zero_Queue_Conversion; |
| 1834 | FromType = ToType; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1835 | } else { |
| 1836 | // No second conversion required. |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1837 | SCS.Second = ICK_Identity; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1838 | } |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1839 | SCS.setToType(1, FromType); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1840 | |
| Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 1841 | // The third conversion can be a function pointer conversion or a |
| 1842 | // qualification conversion (C++ [conv.fctptr], [conv.qual]). |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1843 | bool ObjCLifetimeConversion; |
| Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 1844 | if (S.IsFunctionConversion(FromType, ToType, FromType)) { |
| 1845 | // Function pointer conversions (removing 'noexcept') including removal of |
| 1846 | // 'noreturn' (Clang extension). |
| 1847 | SCS.Third = ICK_Function_Conversion; |
| 1848 | } else if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1849 | ObjCLifetimeConversion)) { |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1850 | SCS.Third = ICK_Qualification; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1851 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1852 | FromType = ToType; |
| 1853 | } else { |
| 1854 | // No conversion required |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1855 | SCS.Third = ICK_Identity; |
| Richard Smith | 9c37e66 | 2016-10-20 17:57:33 +0000 | [diff] [blame] | 1856 | } |
| Richard Smith | eb7ef2e | 2016-10-20 21:53:09 +0000 | [diff] [blame] | 1857 | |
| 1858 | // C++ [over.best.ics]p6: |
| 1859 | // [...] Any difference in top-level cv-qualification is |
| 1860 | // subsumed by the initialization itself and does not constitute |
| 1861 | // a conversion. [...] |
| 1862 | QualType CanonFrom = S.Context.getCanonicalType(FromType); |
| 1863 | QualType CanonTo = S.Context.getCanonicalType(ToType); |
| 1864 | if (CanonFrom.getLocalUnqualifiedType() |
| 1865 | == CanonTo.getLocalUnqualifiedType() && |
| 1866 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
| 1867 | FromType = ToType; |
| 1868 | CanonFrom = CanonTo; |
| 1869 | } |
| 1870 | |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1871 | SCS.setToType(2, FromType); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1872 | |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1873 | if (CanonFrom == CanonTo) |
| 1874 | return true; |
| 1875 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1876 | // If we have not converted the argument type to the parameter type, |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1877 | // this is a bad conversion sequence, unless we're resolving an overload in C. |
| 1878 | if (S.getLangOpts().CPlusPlus || !InOverloadResolution) |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1879 | return false; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1880 | |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1881 | ExprResult ER = ExprResult{From}; |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 1882 | Sema::AssignConvertType Conv = |
| 1883 | S.CheckSingleAssignmentConstraints(ToType, ER, |
| 1884 | /*Diagnose=*/false, |
| 1885 | /*DiagnoseCFAudited=*/false, |
| 1886 | /*ConvertRHS=*/false); |
| George Burgess IV | 6098fd1 | 2016-09-03 00:28:25 +0000 | [diff] [blame] | 1887 | ImplicitConversionKind SecondConv; |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 1888 | switch (Conv) { |
| 1889 | case Sema::Compatible: |
| George Burgess IV | 6098fd1 | 2016-09-03 00:28:25 +0000 | [diff] [blame] | 1890 | SecondConv = ICK_C_Only_Conversion; |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 1891 | break; |
| 1892 | // For our purposes, discarding qualifiers is just as bad as using an |
| 1893 | // incompatible pointer. Note that an IncompatiblePointer conversion can drop |
| 1894 | // qualifiers, as well. |
| 1895 | case Sema::CompatiblePointerDiscardsQualifiers: |
| 1896 | case Sema::IncompatiblePointer: |
| 1897 | case Sema::IncompatiblePointerSign: |
| George Burgess IV | 6098fd1 | 2016-09-03 00:28:25 +0000 | [diff] [blame] | 1898 | SecondConv = ICK_Incompatible_Pointer_Conversion; |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | default: |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1901 | return false; |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 1902 | } |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 1903 | |
| George Burgess IV | 6098fd1 | 2016-09-03 00:28:25 +0000 | [diff] [blame] | 1904 | // First can only be an lvalue conversion, so we pretend that this was the |
| 1905 | // second conversion. First should already be valid from earlier in the |
| 1906 | // function. |
| 1907 | SCS.Second = SecondConv; |
| 1908 | SCS.setToType(1, ToType); |
| 1909 | |
| 1910 | // Third is Identity, because Second should rank us worse than any other |
| 1911 | // conversion. This could also be ICK_Qualification, but it's simpler to just |
| 1912 | // lump everything in with the second conversion, and we don't gain anything |
| 1913 | // from making this ICK_Qualification. |
| 1914 | SCS.Third = ICK_Identity; |
| 1915 | SCS.setToType(2, ToType); |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1916 | return true; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1917 | } |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 1918 | |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1919 | static bool |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1920 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1921 | QualType &ToType, |
| 1922 | bool InOverloadResolution, |
| 1923 | StandardConversionSequence &SCS, |
| 1924 | bool CStyle) { |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 1925 | |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1926 | const RecordType *UT = ToType->getAsUnionType(); |
| 1927 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1928 | return false; |
| 1929 | // The field to initialize within the transparent union. |
| 1930 | RecordDecl *UD = UT->getDecl(); |
| 1931 | // It's compatible if the expression matches any of the fields. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1932 | for (const auto *it : UD->fields()) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1933 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1934 | CStyle, /*ObjCWritebackConversion=*/false)) { |
| Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1935 | ToType = it->getType(); |
| 1936 | return true; |
| 1937 | } |
| 1938 | } |
| 1939 | return false; |
| 1940 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1941 | |
| 1942 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1943 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1944 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1945 | /// sets PromotedType to the promoted type. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1947 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
| Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1948 | // All integers are built-in. |
| Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1949 | if (!To) { |
| 1950 | return false; |
| 1951 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1952 | |
| 1953 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1954 | // unsigned short int can be converted to an rvalue of type int if |
| 1955 | // int can represent all the values of the source type; otherwise, |
| 1956 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1957 | // int (C++ 4.5p1). |
| Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1958 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1959 | !FromType->isEnumeralType()) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1960 | if (// We can promote any signed, promotable integer type to an int |
| 1961 | (FromType->isSignedIntegerType() || |
| 1962 | // We can promote any unsigned integer type whose size is |
| 1963 | // less than int to an int. |
| Benjamin Kramer | 5ff6747 | 2016-04-11 08:26:13 +0000 | [diff] [blame] | 1964 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType))) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1965 | return To->getKind() == BuiltinType::Int; |
| Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1968 | return To->getKind() == BuiltinType::UInt; |
| 1969 | } |
| 1970 | |
| Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1971 | // C++11 [conv.prom]p3: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1972 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1973 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1974 | // following types that can represent all the values of the enumeration |
| 1975 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1976 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1977 | // long long int. If none of the types in that list can represent all the |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1978 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1979 | // type can be converted to an rvalue a prvalue of the extended integer type |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1981 | // long in which all the values of the enumeration can be represented. If |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1982 | // there are two such extended types, the signed one is chosen. |
| Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1983 | // C++11 [conv.prom]p4: |
| 1984 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1985 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1986 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1987 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1988 | // converted to a prvalue of the promoted underlying type. |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1989 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1990 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1991 | // provided for a scoped enumeration. |
| 1992 | if (FromEnumType->getDecl()->isScoped()) |
| 1993 | return false; |
| 1994 | |
| Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1995 | // We can perform an integral promotion to the underlying type of the enum, |
| Richard Smith | ac8c175 | 2015-03-28 00:31:40 +0000 | [diff] [blame] | 1996 | // even if that's not the promoted type. Note that the check for promoting |
| 1997 | // the underlying type is based on the type alone, and does not consider |
| 1998 | // the bitfield-ness of the actual source expression. |
| Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1999 | if (FromEnumType->getDecl()->isFixed()) { |
| 2000 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 2001 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| Richard Smith | ac8c175 | 2015-03-28 00:31:40 +0000 | [diff] [blame] | 2002 | IsIntegralPromotion(nullptr, Underlying, ToType); |
| Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 2005 | // We have already pre-calculated the promotion type, so this is trivial. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | if (ToType->isIntegerType() && |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 2007 | isCompleteType(From->getLocStart(), FromType)) |
| Richard Smith | 88f4bba | 2015-03-26 00:16:07 +0000 | [diff] [blame] | 2008 | return Context.hasSameUnqualifiedType( |
| 2009 | ToType, FromEnumType->getDecl()->getPromotionType()); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2010 | } |
| John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2011 | |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 2012 | // C++0x [conv.prom]p2: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2013 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 2014 | // to an rvalue a prvalue of the first of the following types that can |
| 2015 | // represent all the values of its underlying type: int, unsigned int, |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 2016 | // long int, unsigned long int, long long int, or unsigned long long int. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2017 | // If none of the types in that list can represent all the values of its |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 2018 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2019 | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 2020 | // type. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2021 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
| Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 2022 | ToType->isIntegerType()) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2023 | // Determine whether the type we're converting from is signed or |
| 2024 | // unsigned. |
| David Majnemer | fa01a58 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 2025 | bool FromIsSigned = FromType->isSignedIntegerType(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2026 | uint64_t FromSize = Context.getTypeSize(FromType); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2027 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2028 | // The types we'll try to promote to, in the appropriate |
| 2029 | // order. Try each of these types. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | QualType PromoteTypes[6] = { |
| 2031 | Context.IntTy, Context.UnsignedIntTy, |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2032 | Context.LongTy, Context.UnsignedLongTy , |
| 2033 | Context.LongLongTy, Context.UnsignedLongLongTy |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2034 | }; |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2035 | for (int Idx = 0; Idx < 6; ++Idx) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2036 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 2037 | if (FromSize < ToSize || |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | (FromSize == ToSize && |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2039 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 2040 | // We found the type that we can promote to. If this is the |
| 2041 | // type we wanted, we have a promotion. Otherwise, no |
| 2042 | // promotion. |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2043 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 2049 | // rvalue of type int if int can represent all the values of the |
| 2050 | // bit-field; otherwise, it can be converted to unsigned int if |
| 2051 | // unsigned int can represent all the values of the bit-field. If |
| 2052 | // the bit-field is larger yet, no integral promotion applies to |
| 2053 | // it. If the bit-field has an enumerated type, it is treated as any |
| 2054 | // other value of that type for promotion purposes (C++ 4.5p3). |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2055 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 2056 | // conversion. |
| Richard Smith | 88f4bba | 2015-03-26 00:16:07 +0000 | [diff] [blame] | 2057 | if (From) { |
| John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 2058 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
| Richard Smith | 88f4bba | 2015-03-26 00:16:07 +0000 | [diff] [blame] | 2059 | llvm::APSInt BitWidth; |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2060 | if (FromType->isIntegralType(Context) && |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2061 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| Richard Smith | 88f4bba | 2015-03-26 00:16:07 +0000 | [diff] [blame] | 2062 | llvm::APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2063 | ToSize = Context.getTypeSize(ToType); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | |
| Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2065 | // Are we promoting to an int from a bitfield that fits in an int? |
| 2066 | if (BitWidth < ToSize || |
| 2067 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 2068 | return To->getKind() == BuiltinType::Int; |
| 2069 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
| Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2071 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 2072 | // that fits into an unsigned int? |
| 2073 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 2074 | return To->getKind() == BuiltinType::UInt; |
| 2075 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2076 | |
| Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2077 | return false; |
| Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2078 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2079 | } |
| Richard Smith | 88f4bba | 2015-03-26 00:16:07 +0000 | [diff] [blame] | 2080 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2082 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 2083 | // with false becoming zero and true becoming one (C++ 4.5p4). |
| Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2084 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2085 | return true; |
| Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2086 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2087 | |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 2092 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 2093 | /// returns true and sets PromotedType to the promoted type. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2095 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 2096 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
| Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2097 | /// An rvalue of type float can be converted to an rvalue of type |
| 2098 | /// double. (C++ 4.6p1). |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2099 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 2100 | ToBuiltin->getKind() == BuiltinType::Double) |
| 2101 | return true; |
| 2102 | |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2103 | // C99 6.3.1.5p1: |
| 2104 | // When a float is promoted to double or long double, or a |
| 2105 | // double is promoted to long double [...]. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2106 | if (!getLangOpts().CPlusPlus && |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2107 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 2108 | FromBuiltin->getKind() == BuiltinType::Double) && |
| Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 2109 | (ToBuiltin->getKind() == BuiltinType::LongDouble || |
| 2110 | ToBuiltin->getKind() == BuiltinType::Float128)) |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2111 | return true; |
| Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2112 | |
| 2113 | // Half can be promoted to float. |
| Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 2114 | if (!getLangOpts().NativeHalfType && |
| 2115 | FromBuiltin->getKind() == BuiltinType::Half && |
| Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2116 | ToBuiltin->getKind() == BuiltinType::Float) |
| 2117 | return true; |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2120 | return false; |
| 2121 | } |
| 2122 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2123 | /// Determine if a conversion is a complex promotion. |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2124 | /// |
| 2125 | /// A complex promotion is defined as a complex -> complex conversion |
| 2126 | /// where the conversion between the underlying real types is a |
| Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 2127 | /// floating-point or integral promotion. |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2128 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2129 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2130 | if (!FromComplex) |
| 2131 | return false; |
| 2132 | |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2133 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2134 | if (!ToComplex) |
| 2135 | return false; |
| 2136 | |
| 2137 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
| Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 2138 | ToComplex->getElementType()) || |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2139 | IsIntegralPromotion(nullptr, FromComplex->getElementType(), |
| Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 2140 | ToComplex->getElementType()); |
| Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2143 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 2144 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 2145 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 2146 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 2147 | /// the right set of qualifiers on its pointee. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2148 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | static QualType |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2150 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2151 | QualType ToPointee, QualType ToType, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2152 | ASTContext &Context, |
| 2153 | bool StripObjCLifetime = false) { |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2154 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 2155 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 2156 | "Invalid similarly-qualified pointer type"); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2157 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2158 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2159 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
| Douglas Gregor | c6bd1d3 | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 2160 | return ToType.getUnqualifiedType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2161 | |
| 2162 | QualType CanonFromPointee |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2163 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2164 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2165 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2167 | if (StripObjCLifetime) |
| 2168 | Quals.removeObjCLifetime(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2169 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | // Exact qualifier match -> return the pointer type we're converting to. |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2171 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2172 | // ToType is exactly what we need. Return it. |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2173 | if (!ToType.isNull()) |
| Douglas Gregor | b9f907b | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 2174 | return ToType.getUnqualifiedType(); |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2175 | |
| 2176 | // Build a pointer to ToPointee. It has the right qualifiers |
| 2177 | // already. |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2178 | if (isa<ObjCObjectPointerType>(ToType)) |
| 2179 | return Context.getObjCObjectPointerType(ToPointee); |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2180 | return Context.getPointerType(ToPointee); |
| 2181 | } |
| 2182 | |
| 2183 | // Just build a canonical type that has the right qualifiers. |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2184 | QualType QualifiedCanonToPointee |
| 2185 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2186 | |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2187 | if (isa<ObjCObjectPointerType>(ToType)) |
| 2188 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 2189 | return Context.getPointerType(QualifiedCanonToPointee); |
| Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2190 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2191 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | static bool isNullPointerConstantForConversion(Expr *Expr, |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2193 | bool InOverloadResolution, |
| 2194 | ASTContext &Context) { |
| 2195 | // Handle value-dependent integral null pointer constants correctly. |
| 2196 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 2197 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2198 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2199 | return !InOverloadResolution; |
| 2200 | |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2201 | return Expr->isNullPointerConstant(Context, |
| 2202 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2203 | : Expr::NPC_ValueDependentIsNull); |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2204 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2206 | /// IsPointerConversion - Determines whether the conversion of the |
| 2207 | /// expression From, which has the (possibly adjusted) type FromType, |
| 2208 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 2209 | /// 4.10). If so, returns true and places the converted type (that |
| 2210 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 2211 | /// ConvertedType. |
| Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2212 | /// |
| Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2213 | /// This routine also supports conversions to and from block pointers |
| 2214 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 2215 | /// pointers to interfaces. FIXME: Once we've determined the |
| 2216 | /// appropriate overloading rules for Objective-C, we may want to |
| 2217 | /// split the Objective-C checks into a different routine; however, |
| 2218 | /// GCC seems to consider all of these conversions to be pointer |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2219 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 2220 | /// set if the conversion is an allowed Objective-C conversion that |
| 2221 | /// should result in a warning. |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2222 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
| Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2223 | bool InOverloadResolution, |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2224 | QualType& ConvertedType, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | bool &IncompatibleObjC) { |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2226 | IncompatibleObjC = false; |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2227 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 2228 | IncompatibleObjC)) |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2229 | return true; |
| Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2230 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 2232 | if (ToType->isObjCObjectPointerType() && |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2233 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 2234 | ConvertedType = ToType; |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
| Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2238 | // Blocks: Block pointers can be converted to void*. |
| 2239 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2240 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
| Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2241 | ConvertedType = ToType; |
| 2242 | return true; |
| 2243 | } |
| 2244 | // Blocks: A null pointer constant can be converted to a block |
| 2245 | // pointer type. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | if (ToType->isBlockPointerType() && |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2247 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2248 | ConvertedType = ToType; |
| 2249 | return true; |
| 2250 | } |
| 2251 | |
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2252 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2253 | // pointer constant. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | if (ToType->isNullPtrType() && |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2255 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2256 | ConvertedType = ToType; |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2260 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2261 | if (!ToTypePtr) |
| 2262 | return false; |
| 2263 | |
| 2264 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2265 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2266 | ConvertedType = ToType; |
| 2267 | return true; |
| 2268 | } |
| Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2269 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2270 | // Beyond this point, both types need to be pointers |
| Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2271 | // , including objective-c pointers. |
| 2272 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2273 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2274 | !getLangOpts().ObjCAutoRefCount) { |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2275 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2276 | FromType->getAs<ObjCObjectPointerType>(), |
| 2277 | ToPointeeType, |
| Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2278 | ToType, Context); |
| 2279 | return true; |
| Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2280 | } |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2281 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2282 | if (!FromTypePtr) |
| 2283 | return false; |
| 2284 | |
| 2285 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2286 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2287 | // If the unqualified pointee types are the same, this can't be a |
| Douglas Gregor | fb64086 | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 2288 | // pointer conversion, so don't do all of the work below. |
| 2289 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2290 | return false; |
| 2291 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2292 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2293 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2294 | // 4.10p2). |
| Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2295 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2296 | ToPointeeType->isVoidType()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2298 | ToPointeeType, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2299 | ToType, Context, |
| 2300 | /*StripObjCLifetime=*/true); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2301 | return true; |
| 2302 | } |
| 2303 | |
| Francois Pichet | bc6ebb5 | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2304 | // MSVC allows implicit function to void* type conversion. |
| David Majnemer | 6bf0282 | 2015-10-31 08:42:14 +0000 | [diff] [blame] | 2305 | if (getLangOpts().MSVCCompat && FromPointeeType->isFunctionType() && |
| Francois Pichet | bc6ebb5 | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2306 | ToPointeeType->isVoidType()) { |
| 2307 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2308 | ToPointeeType, |
| 2309 | ToType, Context); |
| 2310 | return true; |
| 2311 | } |
| 2312 | |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2313 | // When we're overloading in C, we allow a special kind of pointer |
| 2314 | // conversion for compatible-but-not-identical pointee types. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2315 | if (!getLangOpts().CPlusPlus && |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2316 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2318 | ToPointeeType, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | ToType, Context); |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2320 | return true; |
| 2321 | } |
| 2322 | |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2323 | // C++ [conv.ptr]p3: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | // |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2325 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2326 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2327 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2328 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2329 | // necessitates this conversion is ill-formed. The result of the |
| 2330 | // conversion is a pointer to the base class sub-object of the |
| 2331 | // derived class object. The null pointer value is converted to |
| 2332 | // the null pointer value of the destination type. |
| 2333 | // |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2334 | // Note that we do not check for ambiguity or inaccessibility |
| 2335 | // here. That is handled by CheckPointerConversion. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2336 | if (getLangOpts().CPlusPlus && |
| Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2337 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2338 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2339 | IsDerivedFrom(From->getLocStart(), FromPointeeType, ToPointeeType)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2341 | ToPointeeType, |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2342 | ToType, Context); |
| 2343 | return true; |
| 2344 | } |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2345 | |
| Fariborz Jahanian | bc2ee93 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2346 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2347 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2348 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2349 | ToPointeeType, |
| 2350 | ToType, Context); |
| 2351 | return true; |
| 2352 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2353 | |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2354 | return false; |
| 2355 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2356 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2357 | /// Adopt the given qualifiers for the given type. |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2358 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2359 | Qualifiers TQs = T.getQualifiers(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2360 | |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2361 | // Check whether qualifiers already match. |
| 2362 | if (TQs == Qs) |
| 2363 | return T; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2364 | |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2365 | if (Qs.compatiblyIncludes(TQs)) |
| 2366 | return Context.getQualifiedType(T, Qs); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2367 | |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2368 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2369 | } |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2370 | |
| 2371 | /// isObjCPointerConversion - Determines whether this is an |
| 2372 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2373 | /// with the same arguments and return values. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2374 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2375 | QualType& ConvertedType, |
| 2376 | bool &IncompatibleObjC) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2377 | if (!getLangOpts().ObjC1) |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2378 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2380 | // The set of qualifiers on the type we're converting from. |
| 2381 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2382 | |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2383 | // First, we handle all conversions on ObjC object pointer types. |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2384 | const ObjCObjectPointerType* ToObjCPtr = |
| 2385 | ToType->getAs<ObjCObjectPointerType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | const ObjCObjectPointerType *FromObjCPtr = |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2387 | FromType->getAs<ObjCObjectPointerType>(); |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2388 | |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2389 | if (ToObjCPtr && FromObjCPtr) { |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2390 | // If the pointee types are the same (ignoring qualifications), |
| 2391 | // then this is not a pointer conversion. |
| 2392 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2393 | FromObjCPtr->getPointeeType())) |
| 2394 | return false; |
| 2395 | |
| Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2396 | // Conversion between Objective-C pointers. |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2397 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2398 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2399 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2400 | if (getLangOpts().CPlusPlus && LHS && RHS && |
| Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2401 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2402 | FromObjCPtr->getPointeeType())) |
| 2403 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2404 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2405 | ToObjCPtr->getPointeeType(), |
| 2406 | ToType, Context); |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2407 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2408 | return true; |
| 2409 | } |
| 2410 | |
| 2411 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2412 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2413 | // interfaces, which is permitted. However, we're going to |
| 2414 | // complain about it. |
| 2415 | IncompatibleObjC = true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2416 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2417 | ToObjCPtr->getPointeeType(), |
| 2418 | ToType, Context); |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2419 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2420 | return true; |
| 2421 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2422 | } |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2423 | // Beyond this point, both types need to be C pointers or block pointers. |
| Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2424 | QualType ToPointeeType; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2425 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2426 | ToPointeeType = ToCPtr->getPointeeType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2427 | else if (const BlockPointerType *ToBlockPtr = |
| Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2428 | ToType->getAs<BlockPointerType>()) { |
| Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2429 | // Objective C++: We're able to convert from a pointer to any object |
| Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2430 | // to a block pointer type. |
| 2431 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2432 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
| Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2433 | return true; |
| 2434 | } |
| Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2435 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2436 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2437 | else if (FromType->getAs<BlockPointerType>() && |
| Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2438 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2439 | // Objective C++: We're able to convert from a block pointer type to a |
| Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2440 | // pointer to any object. |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2441 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
| Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2442 | return true; |
| 2443 | } |
| Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2444 | else |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2445 | return false; |
| 2446 | |
| Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2447 | QualType FromPointeeType; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2448 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2449 | FromPointeeType = FromCPtr->getPointeeType(); |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2450 | else if (const BlockPointerType *FromBlockPtr = |
| 2451 | FromType->getAs<BlockPointerType>()) |
| Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2452 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2453 | else |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2454 | return false; |
| 2455 | |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2456 | // If we have pointers to pointers, recursively check whether this |
| 2457 | // is an Objective-C conversion. |
| 2458 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2459 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2460 | IncompatibleObjC)) { |
| 2461 | // We always complain about this conversion. |
| 2462 | IncompatibleObjC = true; |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2463 | ConvertedType = Context.getPointerType(ConvertedType); |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2464 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2465 | return true; |
| 2466 | } |
| Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2467 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2468 | // as in I* to id. |
| 2469 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2470 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2471 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2472 | IncompatibleObjC)) { |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2473 | |
| Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2474 | ConvertedType = Context.getPointerType(ConvertedType); |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2475 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
| Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2476 | return true; |
| 2477 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2478 | |
| Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2479 | // If we have pointers to functions or blocks, check whether the only |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2480 | // differences in the argument and result types are in Objective-C |
| 2481 | // pointer conversions. If so, we permit the conversion (but |
| 2482 | // complain about it). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | const FunctionProtoType *FromFunctionType |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2484 | = FromPointeeType->getAs<FunctionProtoType>(); |
| Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2485 | const FunctionProtoType *ToFunctionType |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2486 | = ToPointeeType->getAs<FunctionProtoType>(); |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2487 | if (FromFunctionType && ToFunctionType) { |
| 2488 | // If the function types are exactly the same, this isn't an |
| 2489 | // Objective-C pointer conversion. |
| 2490 | if (Context.getCanonicalType(FromPointeeType) |
| 2491 | == Context.getCanonicalType(ToPointeeType)) |
| 2492 | return false; |
| 2493 | |
| 2494 | // Perform the quick checks that will tell us whether these |
| 2495 | // function types are obviously different. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2496 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2497 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2498 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2499 | return false; |
| 2500 | |
| 2501 | bool HasObjCConversion = false; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2502 | if (Context.getCanonicalType(FromFunctionType->getReturnType()) == |
| 2503 | Context.getCanonicalType(ToFunctionType->getReturnType())) { |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2504 | // Okay, the types match exactly. Nothing to do. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2505 | } else if (isObjCPointerConversion(FromFunctionType->getReturnType(), |
| 2506 | ToFunctionType->getReturnType(), |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2507 | ConvertedType, IncompatibleObjC)) { |
| 2508 | // Okay, we have an Objective-C pointer conversion. |
| 2509 | HasObjCConversion = true; |
| 2510 | } else { |
| 2511 | // Function types are too different. Abort. |
| 2512 | return false; |
| 2513 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2515 | // Check argument types. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2516 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2517 | ArgIdx != NumArgs; ++ArgIdx) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2518 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
| 2519 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2520 | if (Context.getCanonicalType(FromArgType) |
| 2521 | == Context.getCanonicalType(ToArgType)) { |
| 2522 | // Okay, the types match exactly. Nothing to do. |
| 2523 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2524 | ConvertedType, IncompatibleObjC)) { |
| 2525 | // Okay, we have an Objective-C pointer conversion. |
| 2526 | HasObjCConversion = true; |
| 2527 | } else { |
| 2528 | // Argument types are too different. Abort. |
| 2529 | return false; |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | if (HasObjCConversion) { |
| 2534 | // We had an Objective-C conversion. Allow this pointer |
| 2535 | // conversion, but complain about it. |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2536 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
| Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2537 | IncompatibleObjC = true; |
| 2538 | return true; |
| 2539 | } |
| 2540 | } |
| 2541 | |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2542 | return false; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2543 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2545 | /// Determine whether this is an Objective-C writeback conversion, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2546 | /// used for parameter passing when performing automatic reference counting. |
| 2547 | /// |
| 2548 | /// \param FromType The type we're converting form. |
| 2549 | /// |
| 2550 | /// \param ToType The type we're converting to. |
| 2551 | /// |
| 2552 | /// \param ConvertedType The type that will be produced after applying |
| 2553 | /// this conversion. |
| 2554 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2555 | QualType &ConvertedType) { |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2556 | if (!getLangOpts().ObjCAutoRefCount || |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2557 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2558 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2559 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2560 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2561 | QualType ToPointee; |
| 2562 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2563 | ToPointee = ToPointer->getPointeeType(); |
| 2564 | else |
| 2565 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2566 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2567 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2568 | if (!ToPointee->isObjCLifetimeType() || |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2569 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
| John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2570 | !ToQuals.withoutObjCLifetime().empty()) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2571 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2572 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2573 | // Argument must be a pointer to __strong to __weak. |
| 2574 | QualType FromPointee; |
| 2575 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2576 | FromPointee = FromPointer->getPointeeType(); |
| 2577 | else |
| 2578 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2579 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2580 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2581 | if (!FromPointee->isObjCLifetimeType() || |
| 2582 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2583 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2584 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2585 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2586 | // Make sure that we have compatible qualifiers. |
| 2587 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2588 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2589 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2590 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2591 | // Remove qualifiers from the pointee type we're converting from; they |
| 2592 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2593 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2594 | FromPointee = FromPointee.getUnqualifiedType(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2595 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2596 | // The unqualified form of the pointee types must be compatible. |
| 2597 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2598 | bool IncompatibleObjC; |
| 2599 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2600 | FromPointee = ToPointee; |
| 2601 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2602 | IncompatibleObjC)) |
| 2603 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2604 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2605 | /// Construct the type we're converting to, which is a pointer to |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2606 | /// __autoreleasing pointee. |
| 2607 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2608 | ConvertedType = Context.getPointerType(FromPointee); |
| 2609 | return true; |
| 2610 | } |
| 2611 | |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2612 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2613 | QualType& ConvertedType) { |
| 2614 | QualType ToPointeeType; |
| 2615 | if (const BlockPointerType *ToBlockPtr = |
| 2616 | ToType->getAs<BlockPointerType>()) |
| 2617 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2618 | else |
| 2619 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2620 | |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2621 | QualType FromPointeeType; |
| 2622 | if (const BlockPointerType *FromBlockPtr = |
| 2623 | FromType->getAs<BlockPointerType>()) |
| 2624 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2625 | else |
| 2626 | return false; |
| 2627 | // We have pointer to blocks, check whether the only |
| 2628 | // differences in the argument and result types are in Objective-C |
| 2629 | // pointer conversions. If so, we permit the conversion. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2630 | |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2631 | const FunctionProtoType *FromFunctionType |
| 2632 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2633 | const FunctionProtoType *ToFunctionType |
| 2634 | = ToPointeeType->getAs<FunctionProtoType>(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2635 | |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2636 | if (!FromFunctionType || !ToFunctionType) |
| 2637 | return false; |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2638 | |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2639 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2640 | return true; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2641 | |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2642 | // Perform the quick checks that will tell us whether these |
| 2643 | // function types are obviously different. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2644 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2645 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2646 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2647 | |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2648 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2649 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2650 | if (FromEInfo != ToEInfo) |
| 2651 | return false; |
| 2652 | |
| 2653 | bool IncompatibleObjC = false; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2654 | if (Context.hasSameType(FromFunctionType->getReturnType(), |
| 2655 | ToFunctionType->getReturnType())) { |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2656 | // Okay, the types match exactly. Nothing to do. |
| 2657 | } else { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2658 | QualType RHS = FromFunctionType->getReturnType(); |
| 2659 | QualType LHS = ToFunctionType->getReturnType(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2660 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2661 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2662 | LHS = LHS.getUnqualifiedType(); |
| 2663 | |
| 2664 | if (Context.hasSameType(RHS,LHS)) { |
| 2665 | // OK exact match. |
| 2666 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2667 | ConvertedType, IncompatibleObjC)) { |
| 2668 | if (IncompatibleObjC) |
| 2669 | return false; |
| 2670 | // Okay, we have an Objective-C pointer conversion. |
| 2671 | } |
| 2672 | else |
| 2673 | return false; |
| 2674 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2675 | |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2676 | // Check argument types. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2677 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2678 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2679 | IncompatibleObjC = false; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2680 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
| 2681 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2682 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2683 | // Okay, the types match exactly. Nothing to do. |
| 2684 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2685 | ConvertedType, IncompatibleObjC)) { |
| 2686 | if (IncompatibleObjC) |
| 2687 | return false; |
| 2688 | // Okay, we have an Objective-C pointer conversion. |
| 2689 | } else |
| 2690 | // Argument types are too different. Abort. |
| 2691 | return false; |
| 2692 | } |
| Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 2693 | |
| 2694 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> NewParamInfos; |
| 2695 | bool CanUseToFPT, CanUseFromFPT; |
| 2696 | if (!Context.mergeExtParameterInfo(ToFunctionType, FromFunctionType, |
| 2697 | CanUseToFPT, CanUseFromFPT, |
| 2698 | NewParamInfos)) |
| Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2699 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 2700 | |
| Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2701 | ConvertedType = ToType; |
| 2702 | return true; |
| Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2705 | enum { |
| 2706 | ft_default, |
| 2707 | ft_different_class, |
| 2708 | ft_parameter_arity, |
| 2709 | ft_parameter_mismatch, |
| 2710 | ft_return_type, |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2711 | ft_qualifer_mismatch, |
| 2712 | ft_noexcept |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2713 | }; |
| 2714 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2715 | /// Attempts to get the FunctionProtoType from a Type. Handles |
| 2716 | /// MemberFunctionPointers properly. |
| 2717 | static const FunctionProtoType *tryGetFunctionProtoType(QualType FromType) { |
| 2718 | if (auto *FPT = FromType->getAs<FunctionProtoType>()) |
| 2719 | return FPT; |
| 2720 | |
| 2721 | if (auto *MPT = FromType->getAs<MemberPointerType>()) |
| 2722 | return MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 2723 | |
| 2724 | return nullptr; |
| 2725 | } |
| 2726 | |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2727 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2728 | /// function types. Catches different number of parameter, mismatch in |
| 2729 | /// parameter types, and different return types. |
| 2730 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2731 | QualType FromType, QualType ToType) { |
| Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2732 | // If either type is not valid, include no extra info. |
| 2733 | if (FromType.isNull() || ToType.isNull()) { |
| 2734 | PDiag << ft_default; |
| 2735 | return; |
| 2736 | } |
| 2737 | |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2738 | // Get the function type from the pointers. |
| 2739 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2740 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2741 | *ToMember = ToType->getAs<MemberPointerType>(); |
| Richard Trieu | 9098c9f | 2014-05-22 01:39:16 +0000 | [diff] [blame] | 2742 | if (!Context.hasSameType(FromMember->getClass(), ToMember->getClass())) { |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2743 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2744 | << QualType(FromMember->getClass(), 0); |
| 2745 | return; |
| 2746 | } |
| 2747 | FromType = FromMember->getPointeeType(); |
| 2748 | ToType = ToMember->getPointeeType(); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2751 | if (FromType->isPointerType()) |
| 2752 | FromType = FromType->getPointeeType(); |
| 2753 | if (ToType->isPointerType()) |
| 2754 | ToType = ToType->getPointeeType(); |
| 2755 | |
| 2756 | // Remove references. |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2757 | FromType = FromType.getNonReferenceType(); |
| 2758 | ToType = ToType.getNonReferenceType(); |
| 2759 | |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2760 | // Don't print extra info for non-specialized template functions. |
| 2761 | if (FromType->isInstantiationDependentType() && |
| 2762 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2763 | PDiag << ft_default; |
| 2764 | return; |
| 2765 | } |
| 2766 | |
| Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2767 | // No extra info for same types. |
| 2768 | if (Context.hasSameType(FromType, ToType)) { |
| 2769 | PDiag << ft_default; |
| 2770 | return; |
| 2771 | } |
| 2772 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2773 | const FunctionProtoType *FromFunction = tryGetFunctionProtoType(FromType), |
| 2774 | *ToFunction = tryGetFunctionProtoType(ToType); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2775 | |
| 2776 | // Both types need to be function types. |
| 2777 | if (!FromFunction || !ToFunction) { |
| 2778 | PDiag << ft_default; |
| 2779 | return; |
| 2780 | } |
| 2781 | |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2782 | if (FromFunction->getNumParams() != ToFunction->getNumParams()) { |
| 2783 | PDiag << ft_parameter_arity << ToFunction->getNumParams() |
| 2784 | << FromFunction->getNumParams(); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2785 | return; |
| 2786 | } |
| 2787 | |
| 2788 | // Handle different parameter types. |
| 2789 | unsigned ArgPos; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2790 | if (!FunctionParamTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2791 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2792 | << ToFunction->getParamType(ArgPos) |
| 2793 | << FromFunction->getParamType(ArgPos); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2794 | return; |
| 2795 | } |
| 2796 | |
| 2797 | // Handle different return type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2798 | if (!Context.hasSameType(FromFunction->getReturnType(), |
| 2799 | ToFunction->getReturnType())) { |
| 2800 | PDiag << ft_return_type << ToFunction->getReturnType() |
| 2801 | << FromFunction->getReturnType(); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | } |
| 2804 | |
| 2805 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2806 | ToQuals = ToFunction->getTypeQuals(); |
| 2807 | if (FromQuals != ToQuals) { |
| 2808 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2809 | return; |
| 2810 | } |
| 2811 | |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2812 | // Handle exception specification differences on canonical type (in C++17 |
| 2813 | // onwards). |
| 2814 | if (cast<FunctionProtoType>(FromFunction->getCanonicalTypeUnqualified()) |
| Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 2815 | ->isNothrow() != |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2816 | cast<FunctionProtoType>(ToFunction->getCanonicalTypeUnqualified()) |
| Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 2817 | ->isNothrow()) { |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2818 | PDiag << ft_noexcept; |
| 2819 | return; |
| 2820 | } |
| 2821 | |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2822 | // Unable to find a difference, so add no extra info. |
| 2823 | PDiag << ft_default; |
| 2824 | } |
| 2825 | |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2826 | /// FunctionParamTypesAreEqual - This routine checks two function proto types |
| Douglas Gregor | 2039ca0 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2827 | /// for equality of their argument types. Caller has already checked that |
| Eli Friedman | 5f50895 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2828 | /// they have same number of arguments. If the parameters are different, |
| 2829 | /// ArgPos will have the parameter index of the first different parameter. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2830 | bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, |
| 2831 | const FunctionProtoType *NewType, |
| 2832 | unsigned *ArgPos) { |
| 2833 | for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), |
| 2834 | N = NewType->param_type_begin(), |
| 2835 | E = OldType->param_type_end(); |
| 2836 | O && (O != E); ++O, ++N) { |
| Richard Trieu | 4b03d98 | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2837 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2838 | N->getUnqualifiedType())) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2839 | if (ArgPos) |
| 2840 | *ArgPos = O - OldType->param_type_begin(); |
| Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2841 | return false; |
| Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | return true; |
| 2845 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2846 | |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2847 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2848 | /// expression From to the type ToType. This routine checks for |
| Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2849 | /// ambiguous or inaccessible derived-to-base pointer |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2850 | /// conversions for which IsPointerConversion has already returned |
| 2851 | /// true. It returns true and produces a diagnostic if there was an |
| 2852 | /// error, or returns false otherwise. |
| Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2853 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2854 | CastKind &Kind, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2855 | CXXCastPath& BasePath, |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 2856 | bool IgnoreBaseAccess, |
| 2857 | bool Diagnose) { |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2858 | QualType FromType = From->getType(); |
| Argyrios Kyrtzidis | d6ea6bd | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2859 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2860 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2861 | Kind = CK_BitCast; |
| 2862 | |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 2863 | if (Diagnose && !IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| Argyrios Kyrtzidis | 3e3305d | 2014-02-02 05:26:43 +0000 | [diff] [blame] | 2864 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 2865 | Expr::NPCK_ZeroExpression) { |
| David Blaikie | 1c7c8f7 | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2866 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2867 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2868 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2869 | << ToType << From->getSourceRange()); |
| 2870 | else if (!isUnevaluatedContext()) |
| 2871 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2872 | << ToType << From->getSourceRange(); |
| 2873 | } |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2874 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2875 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2876 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2877 | ToPointeeType = ToPtrType->getPointeeType(); |
| Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2878 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2879 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2880 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2881 | // We must have a derived-to-base conversion. Check an |
| 2882 | // ambiguous or inaccessible conversion. |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 2883 | unsigned InaccessibleID = 0; |
| 2884 | unsigned AmbigiousID = 0; |
| 2885 | if (Diagnose) { |
| 2886 | InaccessibleID = diag::err_upcast_to_inaccessible_base; |
| 2887 | AmbigiousID = diag::err_ambiguous_derived_to_base_conv; |
| 2888 | } |
| 2889 | if (CheckDerivedToBaseConversion( |
| 2890 | FromPointeeType, ToPointeeType, InaccessibleID, AmbigiousID, |
| 2891 | From->getExprLoc(), From->getSourceRange(), DeclarationName(), |
| 2892 | &BasePath, IgnoreBaseAccess)) |
| Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2893 | return true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | |
| Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2895 | // The conversion was successful. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2896 | Kind = CK_DerivedToBase; |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2897 | } |
| David Majnemer | 6bf0282 | 2015-10-31 08:42:14 +0000 | [diff] [blame] | 2898 | |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 2899 | if (Diagnose && !IsCStyleOrFunctionalCast && |
| 2900 | FromPointeeType->isFunctionType() && ToPointeeType->isVoidType()) { |
| David Majnemer | 6bf0282 | 2015-10-31 08:42:14 +0000 | [diff] [blame] | 2901 | assert(getLangOpts().MSVCCompat && |
| 2902 | "this should only be possible with MSVCCompat!"); |
| 2903 | Diag(From->getExprLoc(), diag::ext_ms_impcast_fn_obj) |
| 2904 | << From->getSourceRange(); |
| 2905 | } |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2906 | } |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2907 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2908 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2909 | if (const ObjCObjectPointerType *FromPtrType = |
| 2910 | FromType->getAs<ObjCObjectPointerType>()) { |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2911 | // Objective-C++ conversions are always okay. |
| 2912 | // FIXME: We should have a different class of conversions for the |
| 2913 | // Objective-C++ implicit conversions. |
| Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2914 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2915 | return false; |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2916 | } else if (FromType->isBlockPointerType()) { |
| 2917 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2918 | } else { |
| 2919 | Kind = CK_CPointerToObjCPointerCast; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2920 | } |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2921 | } else if (ToType->isBlockPointerType()) { |
| 2922 | if (!FromType->isBlockPointerType()) |
| 2923 | Kind = CK_AnyPointerToBlockPointerCast; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2924 | } |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2925 | |
| 2926 | // We shouldn't fall into this case unless it's valid for other |
| 2927 | // reasons. |
| 2928 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2929 | Kind = CK_NullToPointer; |
| 2930 | |
| Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2931 | return false; |
| 2932 | } |
| 2933 | |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2934 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2935 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2936 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2937 | /// If so, returns true and places the converted type (that might differ from |
| 2938 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2939 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | QualType ToType, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2941 | bool InOverloadResolution, |
| 2942 | QualType &ConvertedType) { |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2943 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2944 | if (!ToTypePtr) |
| 2945 | return false; |
| 2946 | |
| 2947 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2948 | if (From->isNullPointerConstant(Context, |
| 2949 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2950 | : Expr::NPC_ValueDependentIsNull)) { |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2951 | ConvertedType = ToType; |
| 2952 | return true; |
| 2953 | } |
| 2954 | |
| 2955 | // Otherwise, both types have to be member pointers. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2956 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2957 | if (!FromTypePtr) |
| 2958 | return false; |
| 2959 | |
| 2960 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2961 | // where D is derived from B (C++ 4.11p2). |
| 2962 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2963 | QualType ToClass(ToTypePtr->getClass(), 0); |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2964 | |
| Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2965 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2966 | IsDerivedFrom(From->getLocStart(), ToClass, FromClass)) { |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2967 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2968 | ToClass.getTypePtr()); |
| 2969 | return true; |
| 2970 | } |
| 2971 | |
| 2972 | return false; |
| 2973 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2975 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2976 | /// expression From to the type ToType. This routine checks for ambiguous or |
| John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2977 | /// virtual or inaccessible base-to-derived member pointer conversions |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2978 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2979 | /// true and produces a diagnostic if there was an error, or returns false |
| 2980 | /// otherwise. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2982 | CastKind &Kind, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2983 | CXXCastPath &BasePath, |
| Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2984 | bool IgnoreBaseAccess) { |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2985 | QualType FromType = From->getType(); |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2986 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
| Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2987 | if (!FromPtrType) { |
| 2988 | // This must be a null pointer to member pointer conversion |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2989 | assert(From->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2990 | Expr::NPC_ValueDependentIsNull) && |
| Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2991 | "Expr must be null pointer constant!"); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2992 | Kind = CK_NullToMemberPointer; |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2993 | return false; |
| Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2994 | } |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2995 | |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2996 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2997 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2998 | "that is not a member pointer."); |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2999 | |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3000 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 3001 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 3002 | |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3003 | // FIXME: What about dependent types? |
| 3004 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 3005 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 3006 | |
| Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 3007 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 3008 | /*DetectVirtual=*/true); |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3009 | bool DerivationOkay = |
| 3010 | IsDerivedFrom(From->getLocStart(), ToClass, FromClass, Paths); |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3011 | assert(DerivationOkay && |
| 3012 | "Should not have been called if derivation isn't OK."); |
| 3013 | (void)DerivationOkay; |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 3014 | |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3015 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 3016 | getUnqualifiedType())) { |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3017 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 3018 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 3019 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 3020 | return true; |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 3021 | } |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3022 | |
| Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3023 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3024 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 3025 | << FromClass << ToClass << QualType(VBase, 0) |
| 3026 | << From->getSourceRange(); |
| 3027 | return true; |
| 3028 | } |
| 3029 | |
| John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 3030 | if (!IgnoreBaseAccess) |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3031 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 3032 | Paths.front(), |
| 3033 | diag::err_downcast_from_inaccessible_base); |
| John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 3034 | |
| Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 3035 | // Must be a base to derived member conversion. |
| Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 3036 | BuildBasePathArray(Paths, BasePath); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3037 | Kind = CK_BaseToDerivedMemberPointer; |
| Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 3038 | return false; |
| 3039 | } |
| 3040 | |
| Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3041 | /// Determine whether the lifetime conversion between the two given |
| 3042 | /// qualifiers sets is nontrivial. |
| 3043 | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
| 3044 | Qualifiers ToQuals) { |
| 3045 | // Converting anything to const __unsafe_unretained is trivial. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3046 | if (ToQuals.hasConst() && |
| Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3047 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 3048 | return false; |
| 3049 | |
| 3050 | return true; |
| 3051 | } |
| 3052 | |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3053 | /// IsQualificationConversion - Determines whether the conversion from |
| 3054 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 3055 | /// (C++ 4.4). |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3056 | /// |
| 3057 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 3058 | /// when the qualification conversion involves a change in the Objective-C |
| 3059 | /// object lifetime. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | bool |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3062 | bool CStyle, bool &ObjCLifetimeConversion) { |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3063 | FromType = Context.getCanonicalType(FromType); |
| 3064 | ToType = Context.getCanonicalType(ToType); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3065 | ObjCLifetimeConversion = false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3066 | |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3067 | // If FromType and ToType are the same type, this is not a |
| 3068 | // qualification conversion. |
| Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 3069 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3070 | return false; |
| Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 3071 | |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3072 | // (C++ 4.4p4): |
| 3073 | // A conversion can add cv-qualifiers at levels other than the first |
| 3074 | // in multi-level pointers, subject to the following rules: [...] |
| 3075 | bool PreviousToQualsIncludeConst = true; |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3076 | bool UnwrappedAnyPointer = false; |
| Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3077 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3078 | // Within each iteration of the loop, we check the qualifiers to |
| 3079 | // determine if this still looks like a qualification |
| 3080 | // conversion. Then, if all is well, we unwrap one more level of |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3081 | // pointers or pointers-to-members and do it all again |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3082 | // until there are no more pointers or pointers-to-members left to |
| 3083 | // unwrap. |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3084 | UnwrappedAnyPointer = true; |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3085 | |
| Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 3086 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 3087 | Qualifiers ToQuals = ToType.getQualifiers(); |
| Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 3088 | |
| 3089 | // Ignore __unaligned qualifier if this type is void. |
| 3090 | if (ToType.getUnqualifiedType()->isVoidType()) |
| 3091 | FromQuals.removeUnaligned(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3092 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3093 | // Objective-C ARC: |
| 3094 | // Check Objective-C lifetime conversions. |
| 3095 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 3096 | UnwrappedAnyPointer) { |
| 3097 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3098 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
| 3099 | ObjCLifetimeConversion = true; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3100 | FromQuals.removeObjCLifetime(); |
| 3101 | ToQuals.removeObjCLifetime(); |
| 3102 | } else { |
| 3103 | // Qualification conversions cannot cast between different |
| 3104 | // Objective-C lifetime qualifiers. |
| 3105 | return false; |
| 3106 | } |
| 3107 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3108 | |
| Douglas Gregor | f30053d | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 3109 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 3110 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 3111 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 3112 | FromQuals.removeObjCGCAttr(); |
| 3113 | ToQuals.removeObjCGCAttr(); |
| 3114 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3115 | |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3116 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 3117 | // 2,j, and similarly for volatile. |
| Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 3118 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3119 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3121 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 3122 | // every cv for 0 < k < j. |
| Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 3123 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3124 | && !PreviousToQualsIncludeConst) |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3125 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3127 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 3128 | // include const. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | PreviousToQualsIncludeConst |
| Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 3130 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3131 | } |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3132 | |
| 3133 | // We are left with FromType and ToType being the pointee types |
| 3134 | // after unwrapping the original FromType and ToType the same number |
| 3135 | // of types. If we unwrapped any pointers, and if FromType and |
| 3136 | // ToType have the same unqualified type (since we checked |
| 3137 | // qualifiers above), then this is a qualification conversion. |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3138 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3141 | /// - Determine whether this is a conversion from a scalar type to an |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 3142 | /// atomic type. |
| 3143 | /// |
| 3144 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 3145 | /// sequence to finish the conversion. |
| Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 3146 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 3147 | bool InOverloadResolution, |
| 3148 | StandardConversionSequence &SCS, |
| 3149 | bool CStyle) { |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 3150 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 3151 | if (!ToAtomic) |
| 3152 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3153 | |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 3154 | StandardConversionSequence InnerSCS; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3155 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 3156 | InOverloadResolution, InnerSCS, |
| 3157 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 3158 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3159 | |
| Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 3160 | SCS.Second = InnerSCS.Second; |
| 3161 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 3162 | SCS.Third = InnerSCS.Third; |
| 3163 | SCS.QualificationIncludesObjCLifetime |
| 3164 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 3165 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 3166 | return true; |
| 3167 | } |
| 3168 | |
| Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3169 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 3170 | CXXConstructorDecl *Constructor, |
| 3171 | QualType Type) { |
| 3172 | const FunctionProtoType *CtorType = |
| 3173 | Constructor->getType()->getAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3174 | if (CtorType->getNumParams() > 0) { |
| 3175 | QualType FirstArg = CtorType->getParamType(0); |
| Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3176 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 3177 | return true; |
| 3178 | } |
| 3179 | return false; |
| 3180 | } |
| 3181 | |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3182 | static OverloadingResult |
| 3183 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 3184 | CXXRecordDecl *To, |
| 3185 | UserDefinedConversionSequence &User, |
| 3186 | OverloadCandidateSet &CandidateSet, |
| 3187 | bool AllowExplicit) { |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3188 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3189 | for (auto *D : S.LookupConstructors(To)) { |
| 3190 | auto Info = getConstructorInfo(D); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 3191 | if (!Info) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3192 | continue; |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3193 | |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3194 | bool Usable = !Info.Constructor->isInvalidDecl() && |
| 3195 | S.isInitListConstructor(Info.Constructor) && |
| 3196 | (AllowExplicit || !Info.Constructor->isExplicit()); |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3197 | if (Usable) { |
| Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3198 | // If the first argument is (a reference to) the target type, |
| 3199 | // suppress conversions. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3200 | bool SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3201 | S.Context, Info.Constructor, ToType); |
| 3202 | if (Info.ConstructorTmpl) |
| 3203 | S.AddTemplateOverloadCandidate(Info.ConstructorTmpl, Info.FoundDecl, |
| 3204 | /*ExplicitArgs*/ nullptr, From, |
| 3205 | CandidateSet, SuppressUserConversions); |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3206 | else |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3207 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, From, |
| 3208 | CandidateSet, SuppressUserConversions); |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3213 | |
| 3214 | OverloadCandidateSet::iterator Best; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3215 | switch (auto Result = |
| 3216 | CandidateSet.BestViableFunction(S, From->getLocStart(), |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3217 | Best)) { |
| Fariborz Jahanian | dcf06f4 | 2015-04-14 17:21:58 +0000 | [diff] [blame] | 3218 | case OR_Deleted: |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3219 | case OR_Success: { |
| 3220 | // Record the standard conversion we used and the conversion function. |
| 3221 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3222 | QualType ThisType = Constructor->getThisType(S.Context); |
| 3223 | // Initializer lists don't have conversions as such. |
| 3224 | User.Before.setAsIdentityConversion(); |
| 3225 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 3226 | User.ConversionFunction = Constructor; |
| 3227 | User.FoundConversionFunction = Best->FoundDecl; |
| 3228 | User.After.setAsIdentityConversion(); |
| 3229 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3230 | User.After.setAllToTypes(ToType); |
| Fariborz Jahanian | dcf06f4 | 2015-04-14 17:21:58 +0000 | [diff] [blame] | 3231 | return Result; |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | case OR_No_Viable_Function: |
| 3235 | return OR_No_Viable_Function; |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3236 | case OR_Ambiguous: |
| 3237 | return OR_Ambiguous; |
| 3238 | } |
| 3239 | |
| 3240 | llvm_unreachable("Invalid OverloadResult!"); |
| 3241 | } |
| 3242 | |
| Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3243 | /// Determines whether there is a user-defined conversion sequence |
| 3244 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 3245 | /// ToType. If such a conversion exists, User will contain the |
| 3246 | /// user-defined conversion sequence that performs such a conversion |
| 3247 | /// and this routine will return true. Otherwise, this routine returns |
| 3248 | /// false and User is unspecified. |
| 3249 | /// |
| Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3250 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 3251 | /// "explicit" conversion functions as well as non-explicit conversion |
| 3252 | /// functions (C++0x [class.conv.fct]p2). |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3253 | /// |
| 3254 | /// \param AllowObjCConversionOnExplicit true if the conversion should |
| 3255 | /// allow an extra Objective-C pointer conversion on uses of explicit |
| 3256 | /// constructors. Requires \c AllowExplicit to also be set. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3257 | static OverloadingResult |
| 3258 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3259 | UserDefinedConversionSequence &User, |
| 3260 | OverloadCandidateSet &CandidateSet, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3261 | bool AllowExplicit, |
| 3262 | bool AllowObjCConversionOnExplicit) { |
| Douglas Gregor | 2ee1d99 | 2013-11-08 01:20:25 +0000 | [diff] [blame] | 3263 | assert(AllowExplicit || !AllowObjCConversionOnExplicit); |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3264 | CandidateSet.clear(OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3265 | |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3266 | // Whether we will only visit constructors. |
| 3267 | bool ConstructorsOnly = false; |
| 3268 | |
| 3269 | // If the type we are conversion to is a class type, enumerate its |
| 3270 | // constructors. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3271 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3272 | // C++ [over.match.ctor]p1: |
| 3273 | // When objects of class type are direct-initialized (8.5), or |
| 3274 | // copy-initialized from an expression of the same or a |
| 3275 | // derived class type (8.5), overload resolution selects the |
| 3276 | // constructor. [...] For copy-initialization, the candidate |
| 3277 | // functions are all the converting constructors (12.3.1) of |
| 3278 | // that class. The argument list is the expression-list within |
| 3279 | // the parentheses of the initializer. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3280 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3281 | (From->getType()->getAs<RecordType>() && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3282 | S.IsDerivedFrom(From->getLocStart(), From->getType(), ToType))) |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3283 | ConstructorsOnly = true; |
| 3284 | |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3285 | if (!S.isCompleteType(From->getExprLoc(), ToType)) { |
| Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3286 | // We're not going to find any constructors. |
| 3287 | } else if (CXXRecordDecl *ToRecordDecl |
| 3288 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3289 | |
| 3290 | Expr **Args = &From; |
| 3291 | unsigned NumArgs = 1; |
| 3292 | bool ListInitializing = false; |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3293 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
| Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3294 | // But first, see if there is an init-list-constructor that will work. |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3295 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3296 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3297 | if (Result != OR_No_Viable_Function) |
| 3298 | return Result; |
| 3299 | // Never mind. |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3300 | CandidateSet.clear( |
| 3301 | OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
| Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3302 | |
| 3303 | // If we're list-initializing, we pass the individual elements as |
| 3304 | // arguments, not the entire list. |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3305 | Args = InitList->getInits(); |
| 3306 | NumArgs = InitList->getNumInits(); |
| 3307 | ListInitializing = true; |
| 3308 | } |
| 3309 | |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3310 | for (auto *D : S.LookupConstructors(ToRecordDecl)) { |
| 3311 | auto Info = getConstructorInfo(D); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 3312 | if (!Info) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3313 | continue; |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3314 | |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3315 | bool Usable = !Info.Constructor->isInvalidDecl(); |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3316 | if (ListInitializing) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3317 | Usable = Usable && (AllowExplicit || !Info.Constructor->isExplicit()); |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3318 | else |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3319 | Usable = Usable && |
| 3320 | Info.Constructor->isConvertingConstructor(AllowExplicit); |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3321 | if (Usable) { |
| Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3322 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3323 | if (SuppressUserConversions && ListInitializing) { |
| 3324 | SuppressUserConversions = false; |
| 3325 | if (NumArgs == 1) { |
| 3326 | // If the first argument is (a reference to) the target type, |
| 3327 | // suppress conversions. |
| Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3328 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3329 | S.Context, Info.Constructor, ToType); |
| Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3330 | } |
| 3331 | } |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3332 | if (Info.ConstructorTmpl) |
| 3333 | S.AddTemplateOverloadCandidate( |
| 3334 | Info.ConstructorTmpl, Info.FoundDecl, |
| 3335 | /*ExplicitArgs*/ nullptr, llvm::makeArrayRef(Args, NumArgs), |
| 3336 | CandidateSet, SuppressUserConversions); |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3337 | else |
| Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3338 | // Allow one user-defined conversion when user specifies a |
| 3339 | // From->ToType conversion via an static cast (c-style, etc). |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 3340 | S.AddOverloadCandidate(Info.Constructor, Info.FoundDecl, |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3341 | llvm::makeArrayRef(Args, NumArgs), |
| Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3342 | CandidateSet, SuppressUserConversions); |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3343 | } |
| Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3344 | } |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3348 | // Enumerate conversion functions, if we're allowed to. |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3349 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3350 | } else if (!S.isCompleteType(From->getLocStart(), From->getType())) { |
| Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3351 | // No conversion functions from incomplete types. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | } else if (const RecordType *FromRecordType |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3353 | = From->getType()->getAs<RecordType>()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | if (CXXRecordDecl *FromRecordDecl |
| Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3355 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3356 | // Add all of the conversion functions as candidates. |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 3357 | const auto &Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3358 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3359 | DeclAccessPair FoundDecl = I.getPair(); |
| 3360 | NamedDecl *D = FoundDecl.getDecl(); |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3361 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3362 | if (isa<UsingShadowDecl>(D)) |
| 3363 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3364 | |
| Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3365 | CXXConversionDecl *Conv; |
| 3366 | FunctionTemplateDecl *ConvTemplate; |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3367 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3368 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3369 | else |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3370 | Conv = cast<CXXConversionDecl>(D); |
| Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3371 | |
| 3372 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3373 | if (ConvTemplate) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3374 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3375 | ActingContext, From, ToType, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3376 | CandidateSet, |
| 3377 | AllowObjCConversionOnExplicit); |
| Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3378 | else |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3379 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3380 | From, ToType, CandidateSet, |
| 3381 | AllowObjCConversionOnExplicit); |
| Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3382 | } |
| 3383 | } |
| 3384 | } |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3385 | } |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3386 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3387 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3388 | |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3389 | OverloadCandidateSet::iterator Best; |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 3390 | switch (auto Result = CandidateSet.BestViableFunction(S, From->getLocStart(), |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 3391 | Best)) { |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3392 | case OR_Success: |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 3393 | case OR_Deleted: |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3394 | // Record the standard conversion we used and the conversion function. |
| 3395 | if (CXXConstructorDecl *Constructor |
| 3396 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3397 | // C++ [over.ics.user]p1: |
| 3398 | // If the user-defined conversion is specified by a |
| 3399 | // constructor (12.3.1), the initial standard conversion |
| 3400 | // sequence converts the source type to the type required by |
| 3401 | // the argument of the constructor. |
| 3402 | // |
| 3403 | QualType ThisType = Constructor->getThisType(S.Context); |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3404 | if (isa<InitListExpr>(From)) { |
| 3405 | // Initializer lists don't have conversions as such. |
| 3406 | User.Before.setAsIdentityConversion(); |
| 3407 | } else { |
| 3408 | if (Best->Conversions[0].isEllipsis()) |
| 3409 | User.EllipsisConversion = true; |
| 3410 | else { |
| 3411 | User.Before = Best->Conversions[0].Standard; |
| 3412 | User.EllipsisConversion = false; |
| 3413 | } |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3414 | } |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3415 | User.HadMultipleCandidates = HadMultipleCandidates; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3416 | User.ConversionFunction = Constructor; |
| John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3417 | User.FoundConversionFunction = Best->FoundDecl; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3418 | User.After.setAsIdentityConversion(); |
| 3419 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3420 | User.After.setAllToTypes(ToType); |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 3421 | return Result; |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3422 | } |
| 3423 | if (CXXConversionDecl *Conversion |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3424 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3425 | // C++ [over.ics.user]p1: |
| 3426 | // |
| 3427 | // [...] If the user-defined conversion is specified by a |
| 3428 | // conversion function (12.3.2), the initial standard |
| 3429 | // conversion sequence converts the source type to the |
| 3430 | // implicit object parameter of the conversion function. |
| 3431 | User.Before = Best->Conversions[0].Standard; |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3432 | User.HadMultipleCandidates = HadMultipleCandidates; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3433 | User.ConversionFunction = Conversion; |
| John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3434 | User.FoundConversionFunction = Best->FoundDecl; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3435 | User.EllipsisConversion = false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3437 | // C++ [over.ics.user]p2: |
| 3438 | // The second standard conversion sequence converts the |
| 3439 | // result of the user-defined conversion to the target type |
| 3440 | // for the sequence. Since an implicit conversion sequence |
| 3441 | // is an initialization, the special rules for |
| 3442 | // initialization by user-defined conversion apply when |
| 3443 | // selecting the best user-defined conversion for a |
| 3444 | // user-defined conversion sequence (see 13.3.3 and |
| 3445 | // 13.3.3.1). |
| 3446 | User.After = Best->FinalConversion; |
| Richard Smith | 48372b6 | 2015-01-27 03:30:40 +0000 | [diff] [blame] | 3447 | return Result; |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3448 | } |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3449 | llvm_unreachable("Not a constructor or conversion function?"); |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3450 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3451 | case OR_No_Viable_Function: |
| 3452 | return OR_No_Viable_Function; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3453 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3454 | case OR_Ambiguous: |
| 3455 | return OR_Ambiguous; |
| 3456 | } |
| 3457 | |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3458 | llvm_unreachable("Invalid OverloadResult!"); |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3459 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3460 | |
| Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3461 | bool |
| Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3462 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
| Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3463 | ImplicitConversionSequence ICS; |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 3464 | OverloadCandidateSet CandidateSet(From->getExprLoc(), |
| 3465 | OverloadCandidateSet::CSK_Normal); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | OverloadingResult OvResult = |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3467 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3468 | CandidateSet, false, false); |
| Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3469 | if (OvResult == OR_Ambiguous) |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3470 | Diag(From->getLocStart(), diag::err_typecheck_ambiguous_condition) |
| 3471 | << From->getType() << ToType << From->getSourceRange(); |
| Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3472 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
| Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3473 | if (!RequireCompleteType(From->getLocStart(), ToType, |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3474 | diag::err_typecheck_nonviable_condition_incomplete, |
| Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3475 | From->getType(), From->getSourceRange())) |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3476 | Diag(From->getLocStart(), diag::err_typecheck_nonviable_condition) |
| Nick Lewycky | 08426e2 | 2015-08-25 22:18:46 +0000 | [diff] [blame] | 3477 | << false << From->getType() << From->getSourceRange() << ToType; |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3478 | } else |
| Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3479 | return false; |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3480 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3481 | return true; |
| Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3482 | } |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3483 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3484 | /// Compare the user-defined conversion functions or constructors |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3485 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3486 | /// is possible. |
| 3487 | static ImplicitConversionSequence::CompareKind |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3488 | compareConversionFunctions(Sema &S, FunctionDecl *Function1, |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3489 | FunctionDecl *Function2) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3490 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3491 | return ImplicitConversionSequence::Indistinguishable; |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3492 | |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3493 | // Objective-C++: |
| 3494 | // If both conversion functions are implicitly-declared conversions from |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3495 | // a lambda closure type to a function pointer and a block pointer, |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3496 | // respectively, always prefer the conversion to a function pointer, |
| 3497 | // because the function pointer is more lightweight and is more likely |
| 3498 | // to keep code working. |
| Ted Kremenek | 8d265c2 | 2014-04-01 07:23:18 +0000 | [diff] [blame] | 3499 | CXXConversionDecl *Conv1 = dyn_cast_or_null<CXXConversionDecl>(Function1); |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3500 | if (!Conv1) |
| 3501 | return ImplicitConversionSequence::Indistinguishable; |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3502 | |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3503 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3504 | if (!Conv2) |
| 3505 | return ImplicitConversionSequence::Indistinguishable; |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3506 | |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3507 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3508 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3509 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3510 | if (Block1 != Block2) |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3511 | return Block1 ? ImplicitConversionSequence::Worse |
| 3512 | : ImplicitConversionSequence::Better; |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | return ImplicitConversionSequence::Indistinguishable; |
| 3516 | } |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3517 | |
| 3518 | static bool hasDeprecatedStringLiteralToCharPtrConversion( |
| 3519 | const ImplicitConversionSequence &ICS) { |
| 3520 | return (ICS.isStandard() && ICS.Standard.DeprecatedStringLiteralToCharPtr) || |
| 3521 | (ICS.isUserDefined() && |
| 3522 | ICS.UserDefined.Before.DeprecatedStringLiteralToCharPtr); |
| 3523 | } |
| 3524 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3525 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3526 | /// conversion sequences to determine whether one is better than the |
| 3527 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3528 | static ImplicitConversionSequence::CompareKind |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3529 | CompareImplicitConversionSequences(Sema &S, SourceLocation Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3530 | const ImplicitConversionSequence& ICS1, |
| 3531 | const ImplicitConversionSequence& ICS2) |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3532 | { |
| 3533 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3534 | // conversion sequences (as defined in 13.3.3.1) |
| 3535 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3536 | // conversion sequence than a user-defined conversion sequence or |
| 3537 | // an ellipsis conversion sequence, and |
| 3538 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3539 | // conversion sequence than an ellipsis conversion sequence |
| 3540 | // (13.3.3.1.3). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | // |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3542 | // C++0x [over.best.ics]p10: |
| 3543 | // For the purpose of ranking implicit conversion sequences as |
| 3544 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3545 | // treated as a user-defined sequence that is indistinguishable |
| 3546 | // from any other user-defined conversion sequence. |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3547 | |
| 3548 | // String literal to 'char *' conversion has been deprecated in C++03. It has |
| 3549 | // been removed from C++11. We still accept this conversion, if it happens at |
| 3550 | // the best viable function. Otherwise, this conversion is considered worse |
| 3551 | // than ellipsis conversion. Consider this as an extension; this is not in the |
| 3552 | // standard. For example: |
| 3553 | // |
| 3554 | // int &f(...); // #1 |
| 3555 | // void f(char*); // #2 |
| 3556 | // void g() { int &r = f("foo"); } |
| 3557 | // |
| 3558 | // In C++03, we pick #2 as the best viable function. |
| 3559 | // In C++11, we pick #1 as the best viable function, because ellipsis |
| 3560 | // conversion is better than string-literal to char* conversion (since there |
| 3561 | // is no such conversion in C++11). If there was no #1 at all or #1 couldn't |
| 3562 | // convert arguments, #2 would be the best viable function in C++11. |
| 3563 | // If the best viable function has this conversion, a warning will be issued |
| 3564 | // in C++03, or an ExtWarn (+SFINAE failure) will be issued in C++11. |
| 3565 | |
| 3566 | if (S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
| 3567 | hasDeprecatedStringLiteralToCharPtrConversion(ICS1) != |
| 3568 | hasDeprecatedStringLiteralToCharPtrConversion(ICS2)) |
| 3569 | return hasDeprecatedStringLiteralToCharPtrConversion(ICS1) |
| 3570 | ? ImplicitConversionSequence::Worse |
| 3571 | : ImplicitConversionSequence::Better; |
| 3572 | |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3573 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3574 | return ImplicitConversionSequence::Better; |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3575 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3576 | return ImplicitConversionSequence::Worse; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3577 | |
| Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3578 | // The following checks require both conversion sequences to be of |
| 3579 | // the same kind. |
| 3580 | if (ICS1.getKind() != ICS2.getKind()) |
| 3581 | return ImplicitConversionSequence::Indistinguishable; |
| 3582 | |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3583 | ImplicitConversionSequence::CompareKind Result = |
| 3584 | ImplicitConversionSequence::Indistinguishable; |
| 3585 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3586 | // Two implicit conversion sequences of the same form are |
| 3587 | // indistinguishable conversion sequences unless one of the |
| 3588 | // following rules apply: (C++ 13.3.3.2p3): |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3589 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3590 | // List-initialization sequence L1 is a better conversion sequence than |
| 3591 | // list-initialization sequence L2 if: |
| 3592 | // - L1 converts to std::initializer_list<X> for some X and L2 does not, or, |
| 3593 | // if not that, |
| NAKAMURA Takumi | b01d86b | 2015-02-25 11:02:00 +0000 | [diff] [blame] | 3594 | // - L1 converts to type "array of N1 T", L2 converts to type "array of N2 T", |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3595 | // and N1 is smaller than N2., |
| 3596 | // even if one of the other rules in this paragraph would otherwise apply. |
| 3597 | if (!ICS1.isBad()) { |
| 3598 | if (ICS1.isStdInitializerListElement() && |
| 3599 | !ICS2.isStdInitializerListElement()) |
| 3600 | return ImplicitConversionSequence::Better; |
| 3601 | if (!ICS1.isStdInitializerListElement() && |
| 3602 | ICS2.isStdInitializerListElement()) |
| 3603 | return ImplicitConversionSequence::Worse; |
| 3604 | } |
| 3605 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3606 | if (ICS1.isStandard()) |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3607 | // Standard conversion sequence S1 is a better conversion sequence than |
| 3608 | // standard conversion sequence S2 if [...] |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3609 | Result = CompareStandardConversionSequences(S, Loc, |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3610 | ICS1.Standard, ICS2.Standard); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3611 | else if (ICS1.isUserDefined()) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3612 | // User-defined conversion sequence U1 is a better conversion |
| 3613 | // sequence than another user-defined conversion sequence U2 if |
| 3614 | // they contain the same user-defined conversion function or |
| 3615 | // constructor and if the second standard conversion sequence of |
| 3616 | // U1 is better than the second standard conversion sequence of |
| 3617 | // U2 (C++ 13.3.3.2p3). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | if (ICS1.UserDefined.ConversionFunction == |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3619 | ICS2.UserDefined.ConversionFunction) |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3620 | Result = CompareStandardConversionSequences(S, Loc, |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3621 | ICS1.UserDefined.After, |
| 3622 | ICS2.UserDefined.After); |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3623 | else |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3624 | Result = compareConversionFunctions(S, |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3625 | ICS1.UserDefined.ConversionFunction, |
| 3626 | ICS2.UserDefined.ConversionFunction); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3629 | return Result; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3632 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3633 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3634 | Qualifiers Quals; |
| 3635 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3636 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
| Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3637 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | |
| Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3639 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3640 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3641 | |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3642 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3643 | // determine if one is a proper subset of the other. |
| 3644 | static ImplicitConversionSequence::CompareKind |
| 3645 | compareStandardConversionSubsets(ASTContext &Context, |
| 3646 | const StandardConversionSequence& SCS1, |
| 3647 | const StandardConversionSequence& SCS2) { |
| 3648 | ImplicitConversionSequence::CompareKind Result |
| 3649 | = ImplicitConversionSequence::Indistinguishable; |
| 3650 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3651 | // the identity conversion sequence is considered to be a subsequence of |
| Douglas Gregor | e87561a | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3652 | // any non-identity conversion sequence |
| Douglas Gregor | 377c109 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3653 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3654 | return ImplicitConversionSequence::Better; |
| 3655 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3656 | return ImplicitConversionSequence::Worse; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3658 | if (SCS1.Second != SCS2.Second) { |
| 3659 | if (SCS1.Second == ICK_Identity) |
| 3660 | Result = ImplicitConversionSequence::Better; |
| 3661 | else if (SCS2.Second == ICK_Identity) |
| 3662 | Result = ImplicitConversionSequence::Worse; |
| 3663 | else |
| 3664 | return ImplicitConversionSequence::Indistinguishable; |
| Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3665 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3666 | return ImplicitConversionSequence::Indistinguishable; |
| 3667 | |
| 3668 | if (SCS1.Third == SCS2.Third) { |
| 3669 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3670 | : ImplicitConversionSequence::Indistinguishable; |
| 3671 | } |
| 3672 | |
| 3673 | if (SCS1.Third == ICK_Identity) |
| 3674 | return Result == ImplicitConversionSequence::Worse |
| 3675 | ? ImplicitConversionSequence::Indistinguishable |
| 3676 | : ImplicitConversionSequence::Better; |
| 3677 | |
| 3678 | if (SCS2.Third == ICK_Identity) |
| 3679 | return Result == ImplicitConversionSequence::Better |
| 3680 | ? ImplicitConversionSequence::Indistinguishable |
| 3681 | : ImplicitConversionSequence::Worse; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3682 | |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3683 | return ImplicitConversionSequence::Indistinguishable; |
| 3684 | } |
| 3685 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3686 | /// Determine whether one of the given reference bindings is better |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3687 | /// than the other based on what kind of bindings they are. |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 3688 | static bool |
| 3689 | isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3690 | const StandardConversionSequence &SCS2) { |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3691 | // C++0x [over.ics.rank]p3b4: |
| 3692 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3693 | // implicit object parameter of a non-static member function declared |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3694 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3695 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3696 | // lvalue reference to a function lvalue and S2 binds an rvalue |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3697 | // reference*. |
| 3698 | // |
| 3699 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3700 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3701 | // time of this writing) break the standard definition of std::forward |
| 3702 | // and std::reference_wrapper when dealing with references to functions. |
| 3703 | // Proposed wording changes submitted to CWG for consideration. |
| Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3704 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3705 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3706 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3707 | |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3708 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3709 | SCS2.IsLvalueReference) || |
| 3710 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 3711 | !SCS2.IsLvalueReference && SCS2.BindsToFunctionLvalue); |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3712 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3714 | /// CompareStandardConversionSequences - Compare two standard |
| 3715 | /// conversion sequences to determine whether one is better than the |
| 3716 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3717 | static ImplicitConversionSequence::CompareKind |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3718 | CompareStandardConversionSequences(Sema &S, SourceLocation Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3719 | const StandardConversionSequence& SCS1, |
| 3720 | const StandardConversionSequence& SCS2) |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3721 | { |
| 3722 | // Standard conversion sequence S1 is a better conversion sequence |
| 3723 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3724 | |
| 3725 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3726 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3727 | // excluding any Lvalue Transformation; the identity conversion |
| 3728 | // sequence is considered to be a subsequence of any |
| 3729 | // non-identity conversion sequence) or, if not that, |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3730 | if (ImplicitConversionSequence::CompareKind CK |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3731 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3732 | return CK; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3733 | |
| 3734 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3735 | // defined below), or, if not that, |
| 3736 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3737 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3738 | if (Rank1 < Rank2) |
| 3739 | return ImplicitConversionSequence::Better; |
| 3740 | else if (Rank2 < Rank1) |
| 3741 | return ImplicitConversionSequence::Worse; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3742 | |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3743 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3744 | // are indistinguishable unless one of the following rules |
| 3745 | // applies: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3746 | |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3747 | // A conversion that is not a conversion of a pointer, or |
| 3748 | // pointer to member, to bool is better than another conversion |
| 3749 | // that is such a conversion. |
| 3750 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3751 | return SCS2.isPointerConversionToBool() |
| 3752 | ? ImplicitConversionSequence::Better |
| 3753 | : ImplicitConversionSequence::Worse; |
| 3754 | |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3755 | // C++ [over.ics.rank]p4b2: |
| 3756 | // |
| 3757 | // If class B is derived directly or indirectly from class A, |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3758 | // conversion of B* to A* is better than conversion of B* to |
| 3759 | // void*, and conversion of A* to void* is better than conversion |
| 3760 | // of B* to void*. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | bool SCS1ConvertsToVoid |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3762 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3763 | bool SCS2ConvertsToVoid |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3764 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3765 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3766 | // Exactly one of the conversion sequences is a conversion to |
| 3767 | // a void pointer; it's the worse conversion. |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3768 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3769 | : ImplicitConversionSequence::Worse; |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3770 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3771 | // Neither conversion sequence converts to a void pointer; compare |
| 3772 | // their derived-to-base conversions. |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3773 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3774 | = CompareDerivedToBaseConversions(S, Loc, SCS1, SCS2)) |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3775 | return DerivedCK; |
| Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3776 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3777 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3778 | // Both conversion sequences are conversions to void |
| 3779 | // pointers. Compare the source types to determine if there's an |
| 3780 | // inheritance relationship in their sources. |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3781 | QualType FromType1 = SCS1.getFromType(); |
| 3782 | QualType FromType2 = SCS2.getFromType(); |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3783 | |
| 3784 | // Adjust the types we're converting from via the array-to-pointer |
| 3785 | // conversion, if we need to. |
| 3786 | if (SCS1.First == ICK_Array_To_Pointer) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3787 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3788 | if (SCS2.First == ICK_Array_To_Pointer) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3789 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3790 | |
| Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3791 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3792 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3793 | |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3794 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
| Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3795 | return ImplicitConversionSequence::Better; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 3796 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
| Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3797 | return ImplicitConversionSequence::Worse; |
| 3798 | |
| 3799 | // Objective-C++: If one interface is more specific than the |
| 3800 | // other, it is the better one. |
| Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3801 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3802 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3803 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3804 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3805 | if (FromObjCPtr1 && FromObjCPtr2) { |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3806 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3807 | FromObjCPtr2); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3808 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3809 | FromObjCPtr1); |
| 3810 | if (AssignLeft != AssignRight) { |
| 3811 | return AssignLeft? ImplicitConversionSequence::Better |
| 3812 | : ImplicitConversionSequence::Worse; |
| 3813 | } |
| Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3814 | } |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3815 | } |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3816 | |
| 3817 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3818 | // bullet 3). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | if (ImplicitConversionSequence::CompareKind QualCK |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3820 | = CompareQualificationConversions(S, SCS1, SCS2)) |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3821 | return QualCK; |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3822 | |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3823 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3824 | // Check for a better reference binding based on the kind of bindings. |
| 3825 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3826 | return ImplicitConversionSequence::Better; |
| 3827 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3828 | return ImplicitConversionSequence::Worse; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3829 | |
| Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3830 | // C++ [over.ics.rank]p3b4: |
| 3831 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3832 | // which the references refer are the same type except for |
| 3833 | // top-level cv-qualifiers, and the type to which the reference |
| 3834 | // initialized by S2 refers is more cv-qualified than the type |
| 3835 | // to which the reference initialized by S1 refers. |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3836 | QualType T1 = SCS1.getToType(2); |
| 3837 | QualType T2 = SCS2.getToType(2); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3838 | T1 = S.Context.getCanonicalType(T1); |
| 3839 | T2 = S.Context.getCanonicalType(T2); |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3840 | Qualifiers T1Quals, T2Quals; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3841 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3842 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3843 | if (UnqualT1 == UnqualT2) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3844 | // Objective-C++ ARC: If the references refer to objects with different |
| 3845 | // lifetimes, prefer bindings that don't change lifetime. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3846 | if (SCS1.ObjCLifetimeConversionBinding != |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3847 | SCS2.ObjCLifetimeConversionBinding) { |
| 3848 | return SCS1.ObjCLifetimeConversionBinding |
| 3849 | ? ImplicitConversionSequence::Worse |
| 3850 | : ImplicitConversionSequence::Better; |
| 3851 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3852 | |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3853 | // If the type is an array type, promote the element qualifiers to the |
| 3854 | // type for comparison. |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3855 | if (isa<ArrayType>(T1) && T1Quals) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3856 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3857 | if (isa<ArrayType>(T2) && T2Quals) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3858 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3859 | if (T2.isMoreQualifiedThan(T1)) |
| 3860 | return ImplicitConversionSequence::Better; |
| 3861 | else if (T1.isMoreQualifiedThan(T2)) |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3862 | return ImplicitConversionSequence::Worse; |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3863 | } |
| 3864 | } |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3865 | |
| Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3866 | // In Microsoft mode, prefer an integral conversion to a |
| 3867 | // floating-to-integral conversion if the integral conversion |
| 3868 | // is between types of the same size. |
| 3869 | // For example: |
| 3870 | // void f(float); |
| 3871 | // void f(int); |
| 3872 | // int main { |
| 3873 | // long a; |
| 3874 | // f(a); |
| 3875 | // } |
| 3876 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3877 | // as clang will do in standard mode. |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 3878 | if (S.getLangOpts().MSVCCompat && SCS1.Second == ICK_Integral_Conversion && |
| 3879 | SCS2.Second == ICK_Floating_Integral && |
| Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3880 | S.Context.getTypeSize(SCS1.getFromType()) == |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 3881 | S.Context.getTypeSize(SCS1.getToType(2))) |
| Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3882 | return ImplicitConversionSequence::Better; |
| 3883 | |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3884 | return ImplicitConversionSequence::Indistinguishable; |
| 3885 | } |
| 3886 | |
| 3887 | /// CompareQualificationConversions - Compares two standard conversion |
| 3888 | /// sequences to determine whether they can be ranked based on their |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 3890 | static ImplicitConversionSequence::CompareKind |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3891 | CompareQualificationConversions(Sema &S, |
| 3892 | const StandardConversionSequence& SCS1, |
| 3893 | const StandardConversionSequence& SCS2) { |
| Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3894 | // C++ 13.3.3.2p3: |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3895 | // -- S1 and S2 differ only in their qualification conversion and |
| 3896 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3897 | // cv-qualification signature of type T1 is a proper subset of |
| 3898 | // the cv-qualification signature of type T2, and S1 is not the |
| 3899 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3900 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3901 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3902 | return ImplicitConversionSequence::Indistinguishable; |
| 3903 | |
| 3904 | // FIXME: the example in the standard doesn't use a qualification |
| 3905 | // conversion (!) |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3906 | QualType T1 = SCS1.getToType(2); |
| 3907 | QualType T2 = SCS2.getToType(2); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3908 | T1 = S.Context.getCanonicalType(T1); |
| 3909 | T2 = S.Context.getCanonicalType(T2); |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3910 | Qualifiers T1Quals, T2Quals; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3911 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3912 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3913 | |
| 3914 | // If the types are the same, we won't learn anything by unwrapped |
| 3915 | // them. |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3916 | if (UnqualT1 == UnqualT2) |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3917 | return ImplicitConversionSequence::Indistinguishable; |
| 3918 | |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3919 | // If the type is an array type, promote the element qualifiers to the type |
| 3920 | // for comparison. |
| 3921 | if (isa<ArrayType>(T1) && T1Quals) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3922 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3923 | if (isa<ArrayType>(T2) && T2Quals) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3924 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
| Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3925 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | ImplicitConversionSequence::CompareKind Result |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3927 | = ImplicitConversionSequence::Indistinguishable; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3928 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3929 | // Objective-C++ ARC: |
| 3930 | // Prefer qualification conversions not involving a change in lifetime |
| 3931 | // to qualification conversions that do not change lifetime. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3932 | if (SCS1.QualificationIncludesObjCLifetime != |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3933 | SCS2.QualificationIncludesObjCLifetime) { |
| 3934 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3935 | ? ImplicitConversionSequence::Worse |
| 3936 | : ImplicitConversionSequence::Better; |
| 3937 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 3938 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3939 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3940 | // Within each iteration of the loop, we check the qualifiers to |
| 3941 | // determine if this still looks like a qualification |
| 3942 | // conversion. Then, if all is well, we unwrap one more level of |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3943 | // pointers or pointers-to-members and do it all again |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3944 | // until there are no more pointers or pointers-to-members left |
| 3945 | // to unwrap. This essentially mimics what |
| 3946 | // IsQualificationConversion does, but here we're checking for a |
| 3947 | // strict subset of qualifiers. |
| 3948 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3949 | // The qualifiers are the same, so this doesn't tell us anything |
| 3950 | // about how the sequences rank. |
| 3951 | ; |
| 3952 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3953 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3954 | if (Result == ImplicitConversionSequence::Worse) |
| 3955 | // Neither has qualifiers that are a subset of the other's |
| 3956 | // qualifiers. |
| 3957 | return ImplicitConversionSequence::Indistinguishable; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3958 | |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3959 | Result = ImplicitConversionSequence::Better; |
| 3960 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3961 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3962 | if (Result == ImplicitConversionSequence::Better) |
| 3963 | // Neither has qualifiers that are a subset of the other's |
| 3964 | // qualifiers. |
| 3965 | return ImplicitConversionSequence::Indistinguishable; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3966 | |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3967 | Result = ImplicitConversionSequence::Worse; |
| 3968 | } else { |
| 3969 | // Qualifiers are disjoint. |
| 3970 | return ImplicitConversionSequence::Indistinguishable; |
| 3971 | } |
| 3972 | |
| 3973 | // If the types after this point are equivalent, we're done. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3974 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3975 | break; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3978 | // Check that the winning standard conversion sequence isn't using |
| 3979 | // the deprecated string literal array to pointer conversion. |
| 3980 | switch (Result) { |
| 3981 | case ImplicitConversionSequence::Better: |
| Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3982 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3983 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3984 | break; |
| 3985 | |
| 3986 | case ImplicitConversionSequence::Indistinguishable: |
| 3987 | break; |
| 3988 | |
| 3989 | case ImplicitConversionSequence::Worse: |
| Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3990 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
| Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3991 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3992 | break; |
| 3993 | } |
| 3994 | |
| 3995 | return Result; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3998 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3999 | /// sequences to determine whether they can be ranked based on their |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 4000 | /// various kinds of derived-to-base conversions (C++ |
| 4001 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 4002 | /// conversions between Objective-C interface types. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 4003 | static ImplicitConversionSequence::CompareKind |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4004 | CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4005 | const StandardConversionSequence& SCS1, |
| 4006 | const StandardConversionSequence& SCS2) { |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4007 | QualType FromType1 = SCS1.getFromType(); |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4008 | QualType ToType1 = SCS1.getToType(1); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4009 | QualType FromType2 = SCS2.getFromType(); |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4010 | QualType ToType2 = SCS2.getToType(1); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4011 | |
| 4012 | // Adjust the types we're converting from via the array-to-pointer |
| 4013 | // conversion, if we need to. |
| 4014 | if (SCS1.First == ICK_Array_To_Pointer) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4015 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4016 | if (SCS2.First == ICK_Array_To_Pointer) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4017 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4018 | |
| 4019 | // Canonicalize all of the types. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4020 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 4021 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 4022 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 4023 | ToType2 = S.Context.getCanonicalType(ToType2); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4024 | |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4025 | // C++ [over.ics.rank]p4b3: |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4026 | // |
| 4027 | // If class B is derived directly or indirectly from class A and |
| 4028 | // class C is derived directly or indirectly from B, |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 4029 | // |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4030 | // Compare based on pointer conversions. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | if (SCS1.Second == ICK_Pointer_Conversion && |
| Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 4032 | SCS2.Second == ICK_Pointer_Conversion && |
| 4033 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 4034 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 4035 | ToType1->isPointerType() && ToType2->isPointerType()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4036 | QualType FromPointee1 |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4037 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | QualType ToPointee1 |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4039 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4040 | QualType FromPointee2 |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4041 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4042 | QualType ToPointee2 |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4043 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 4044 | |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4045 | // -- conversion of C* to B* is better than conversion of C* to A*, |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4046 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4047 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4048 | return ImplicitConversionSequence::Better; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4049 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4050 | return ImplicitConversionSequence::Worse; |
| 4051 | } |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4052 | |
| 4053 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 4054 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4055 | if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4056 | return ImplicitConversionSequence::Better; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4057 | else if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4058 | return ImplicitConversionSequence::Worse; |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 4061 | SCS2.Second == ICK_Pointer_Conversion) { |
| 4062 | const ObjCObjectPointerType *FromPtr1 |
| 4063 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 4064 | const ObjCObjectPointerType *FromPtr2 |
| 4065 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 4066 | const ObjCObjectPointerType *ToPtr1 |
| 4067 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 4068 | const ObjCObjectPointerType *ToPtr2 |
| 4069 | = ToType2->getAs<ObjCObjectPointerType>(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4070 | |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4071 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 4072 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 4073 | // that we do for C++ pointers to class types. However, we employ the |
| 4074 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 4075 | // Objective-C pointer types. |
| 4076 | bool FromAssignLeft |
| 4077 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 4078 | bool FromAssignRight |
| 4079 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 4080 | bool ToAssignLeft |
| 4081 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 4082 | bool ToAssignRight |
| 4083 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| Alex Lorenz | a983213 | 2017-04-06 13:06:34 +0000 | [diff] [blame] | 4084 | |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4085 | // A conversion to an a non-id object pointer type or qualified 'id' |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4086 | // type is better than a conversion to 'id'. |
| 4087 | if (ToPtr1->isObjCIdType() && |
| 4088 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 4089 | return ImplicitConversionSequence::Worse; |
| 4090 | if (ToPtr2->isObjCIdType() && |
| 4091 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 4092 | return ImplicitConversionSequence::Better; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4093 | |
| 4094 | // A conversion to a non-id object pointer type is better than a |
| 4095 | // conversion to a qualified 'id' type |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4096 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 4097 | return ImplicitConversionSequence::Worse; |
| 4098 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 4099 | return ImplicitConversionSequence::Better; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4100 | |
| 4101 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4102 | // type is better than a conversion to 'Class'. |
| 4103 | if (ToPtr1->isObjCClassType() && |
| 4104 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 4105 | return ImplicitConversionSequence::Worse; |
| 4106 | if (ToPtr2->isObjCClassType() && |
| 4107 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 4108 | return ImplicitConversionSequence::Better; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4109 | |
| 4110 | // A conversion to a non-Class object pointer type is better than a |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4111 | // conversion to a qualified 'Class' type. |
| 4112 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 4113 | return ImplicitConversionSequence::Worse; |
| 4114 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 4115 | return ImplicitConversionSequence::Better; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4116 | |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4117 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| Alex Lorenz | a983213 | 2017-04-06 13:06:34 +0000 | [diff] [blame] | 4118 | if (S.Context.hasSameType(FromType1, FromType2) && |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4119 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| Alex Lorenz | a983213 | 2017-04-06 13:06:34 +0000 | [diff] [blame] | 4120 | (ToAssignLeft != ToAssignRight)) { |
| 4121 | if (FromPtr1->isSpecialized()) { |
| 4122 | // "conversion of B<A> * to B * is better than conversion of B * to |
| 4123 | // C *. |
| 4124 | bool IsFirstSame = |
| 4125 | FromPtr1->getInterfaceDecl() == ToPtr1->getInterfaceDecl(); |
| 4126 | bool IsSecondSame = |
| 4127 | FromPtr1->getInterfaceDecl() == ToPtr2->getInterfaceDecl(); |
| 4128 | if (IsFirstSame) { |
| 4129 | if (!IsSecondSame) |
| 4130 | return ImplicitConversionSequence::Better; |
| 4131 | } else if (IsSecondSame) |
| 4132 | return ImplicitConversionSequence::Worse; |
| 4133 | } |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4134 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 4135 | : ImplicitConversionSequence::Better; |
| Alex Lorenz | a983213 | 2017-04-06 13:06:34 +0000 | [diff] [blame] | 4136 | } |
| Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 4137 | |
| 4138 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 4139 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 4140 | (FromAssignLeft != FromAssignRight)) |
| 4141 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 4142 | : ImplicitConversionSequence::Worse; |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4143 | } |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4144 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4145 | |
| Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 4146 | // Ranking of member-pointer types. |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4147 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 4148 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 4149 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4150 | const MemberPointerType * FromMemPointer1 = |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4151 | FromType1->getAs<MemberPointerType>(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4152 | const MemberPointerType * ToMemPointer1 = |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4153 | ToType1->getAs<MemberPointerType>(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4154 | const MemberPointerType * FromMemPointer2 = |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4155 | FromType2->getAs<MemberPointerType>(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4156 | const MemberPointerType * ToMemPointer2 = |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4157 | ToType2->getAs<MemberPointerType>(); |
| 4158 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 4159 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 4160 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 4161 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 4162 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 4163 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 4164 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 4165 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
| Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 4166 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4167 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4168 | if (S.IsDerivedFrom(Loc, ToPointee1, ToPointee2)) |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4169 | return ImplicitConversionSequence::Worse; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4170 | else if (S.IsDerivedFrom(Loc, ToPointee2, ToPointee1)) |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4171 | return ImplicitConversionSequence::Better; |
| 4172 | } |
| 4173 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 4174 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4175 | if (S.IsDerivedFrom(Loc, FromPointee1, FromPointee2)) |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4176 | return ImplicitConversionSequence::Better; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4177 | else if (S.IsDerivedFrom(Loc, FromPointee2, FromPointee1)) |
| Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 4178 | return ImplicitConversionSequence::Worse; |
| 4179 | } |
| 4180 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4182 | if (SCS1.Second == ICK_Derived_To_Base) { |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4183 | // -- conversion of C to B is better than conversion of C to A, |
| Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 4184 | // -- binding of an expression of type C to a reference of type |
| 4185 | // B& is better than binding an expression of type C to a |
| 4186 | // reference of type A&, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4187 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 4188 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4189 | if (S.IsDerivedFrom(Loc, ToType1, ToType2)) |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4190 | return ImplicitConversionSequence::Better; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4191 | else if (S.IsDerivedFrom(Loc, ToType2, ToType1)) |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4192 | return ImplicitConversionSequence::Worse; |
| 4193 | } |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4194 | |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4195 | // -- conversion of B to A is better than conversion of C to A. |
| Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 4196 | // -- binding of an expression of type B to a reference of type |
| 4197 | // A& is better than binding an expression of type C to a |
| 4198 | // reference of type A&, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4199 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 4200 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4201 | if (S.IsDerivedFrom(Loc, FromType2, FromType1)) |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4202 | return ImplicitConversionSequence::Better; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4203 | else if (S.IsDerivedFrom(Loc, FromType1, FromType2)) |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4204 | return ImplicitConversionSequence::Worse; |
| 4205 | } |
| 4206 | } |
| Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4207 | |
| Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 4208 | return ImplicitConversionSequence::Indistinguishable; |
| 4209 | } |
| 4210 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4211 | /// Determine whether the given type is valid, e.g., it is not an invalid |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 4212 | /// C++ class. |
| 4213 | static bool isTypeValid(QualType T) { |
| 4214 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 4215 | return !Record->isInvalidDecl(); |
| 4216 | |
| 4217 | return true; |
| 4218 | } |
| 4219 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4220 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 4221 | /// determine whether they are reference-related, |
| 4222 | /// reference-compatible, reference-compatible with added |
| 4223 | /// qualification, or incompatible, for use in C++ initialization by |
| 4224 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 4225 | /// type, and the first type (T1) is the pointee type of the reference |
| 4226 | /// type being initialized. |
| 4227 | Sema::ReferenceCompareResult |
| 4228 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 4229 | QualType OrigT1, QualType OrigT2, |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4230 | bool &DerivedToBase, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4231 | bool &ObjCConversion, |
| 4232 | bool &ObjCLifetimeConversion) { |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4233 | assert(!OrigT1->isReferenceType() && |
| 4234 | "T1 must be the pointee type of the reference type"); |
| 4235 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 4236 | |
| 4237 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 4238 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 4239 | Qualifiers T1Quals, T2Quals; |
| 4240 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 4241 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 4242 | |
| 4243 | // C++ [dcl.init.ref]p4: |
| 4244 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 4245 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 4246 | // T1 is a base class of T2. |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4247 | DerivedToBase = false; |
| 4248 | ObjCConversion = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4249 | ObjCLifetimeConversion = false; |
| Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 4250 | QualType ConvertedT2; |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4251 | if (UnqualT1 == UnqualT2) { |
| 4252 | // Nothing to do. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4253 | } else if (isCompleteType(Loc, OrigT2) && |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 4254 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4255 | IsDerivedFrom(Loc, UnqualT2, UnqualT1)) |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4256 | DerivedToBase = true; |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4257 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 4258 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 4259 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 4260 | ObjCConversion = true; |
| Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 4261 | else if (UnqualT2->isFunctionType() && |
| 4262 | IsFunctionConversion(UnqualT2, UnqualT1, ConvertedT2)) |
| 4263 | // C++1z [dcl.init.ref]p4: |
| 4264 | // cv1 T1" is reference-compatible with "cv2 T2" if [...] T2 is "noexcept |
| 4265 | // function" and T1 is "function" |
| 4266 | // |
| 4267 | // We extend this to also apply to 'noreturn', so allow any function |
| 4268 | // conversion between function types. |
| 4269 | return Ref_Compatible; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4270 | else |
| 4271 | return Ref_Incompatible; |
| 4272 | |
| 4273 | // At this point, we know that T1 and T2 are reference-related (at |
| 4274 | // least). |
| 4275 | |
| 4276 | // If the type is an array type, promote the element qualifiers to the type |
| 4277 | // for comparison. |
| 4278 | if (isa<ArrayType>(T1) && T1Quals) |
| 4279 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 4280 | if (isa<ArrayType>(T2) && T2Quals) |
| 4281 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 4282 | |
| 4283 | // C++ [dcl.init.ref]p4: |
| 4284 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 4285 | // reference-related to T2 and cv1 is the same cv-qualification |
| 4286 | // as, or greater cv-qualification than, cv2. For purposes of |
| 4287 | // overload resolution, cases for which cv1 is greater |
| 4288 | // cv-qualification than cv2 are identified as |
| 4289 | // reference-compatible with added qualification (see 13.3.3.2). |
| Douglas Gregor | d517d55 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4290 | // |
| 4291 | // Note that we also require equivalence of Objective-C GC and address-space |
| 4292 | // qualifiers when performing these computations, so that e.g., an int in |
| 4293 | // address space 1 is not reference-compatible with an int in address |
| 4294 | // space 2. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4295 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 4296 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 4297 | if (isNonTrivialObjCLifetimeConversion(T2Quals, T1Quals)) |
| 4298 | ObjCLifetimeConversion = true; |
| 4299 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4300 | T1Quals.removeObjCLifetime(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4301 | T2Quals.removeObjCLifetime(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4302 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4303 | |
| Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 4304 | // MS compiler ignores __unaligned qualifier for references; do the same. |
| 4305 | T1Quals.removeUnaligned(); |
| 4306 | T2Quals.removeUnaligned(); |
| 4307 | |
| Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4308 | if (T1Quals.compatiblyIncludes(T2Quals)) |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4309 | return Ref_Compatible; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4310 | else |
| 4311 | return Ref_Related; |
| 4312 | } |
| 4313 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4314 | /// Look for a user-defined conversion to a value reference-compatible |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4315 | /// with DeclType. Return true if something definite is found. |
| 4316 | static bool |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4317 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 4318 | QualType DeclType, SourceLocation DeclLoc, |
| 4319 | Expr *Init, QualType T2, bool AllowRvalues, |
| 4320 | bool AllowExplicit) { |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4321 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4322 | CXXRecordDecl *T2RecordDecl |
| 4323 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4324 | |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4325 | OverloadCandidateSet CandidateSet( |
| 4326 | DeclLoc, OverloadCandidateSet::CSK_InitByUserDefinedConversion); |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4327 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4328 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4329 | NamedDecl *D = *I; |
| 4330 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4331 | if (isa<UsingShadowDecl>(D)) |
| 4332 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4333 | |
| 4334 | FunctionTemplateDecl *ConvTemplate |
| 4335 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4336 | CXXConversionDecl *Conv; |
| 4337 | if (ConvTemplate) |
| 4338 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4339 | else |
| 4340 | Conv = cast<CXXConversionDecl>(D); |
| 4341 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | // If this is an explicit conversion, and we're not allowed to consider |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4343 | // explicit conversions, skip it. |
| 4344 | if (!AllowExplicit && Conv->isExplicit()) |
| 4345 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4347 | if (AllowRvalues) { |
| 4348 | bool DerivedToBase = false; |
| 4349 | bool ObjCConversion = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4350 | bool ObjCLifetimeConversion = false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4351 | |
| Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4352 | // If we are initializing an rvalue reference, don't permit conversion |
| 4353 | // functions that return lvalues. |
| 4354 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4355 | const ReferenceType *RefType |
| 4356 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4357 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4358 | continue; |
| 4359 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 4360 | |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4361 | if (!ConvTemplate && |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4362 | S.CompareReferenceRelationship( |
| 4363 | DeclLoc, |
| 4364 | Conv->getConversionType().getNonReferenceType() |
| 4365 | .getUnqualifiedType(), |
| 4366 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4367 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4368 | Sema::Ref_Incompatible) |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4369 | continue; |
| 4370 | } else { |
| 4371 | // If the conversion function doesn't return a reference type, |
| 4372 | // it can't be considered for this conversion. An rvalue reference |
| 4373 | // is only acceptable if its referencee is a function type. |
| 4374 | |
| 4375 | const ReferenceType *RefType = |
| 4376 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4377 | if (!RefType || |
| 4378 | (!RefType->isLValueReferenceType() && |
| 4379 | !RefType->getPointeeType()->isFunctionType())) |
| 4380 | continue; |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4381 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4383 | if (ConvTemplate) |
| 4384 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4385 | Init, DeclType, CandidateSet, |
| 4386 | /*AllowObjCConversionOnExplicit=*/false); |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4387 | else |
| 4388 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4389 | DeclType, CandidateSet, |
| 4390 | /*AllowObjCConversionOnExplicit=*/false); |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4393 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4394 | |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4395 | OverloadCandidateSet::iterator Best; |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 4396 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4397 | case OR_Success: |
| 4398 | // C++ [over.ics.ref]p1: |
| 4399 | // |
| 4400 | // [...] If the parameter binds directly to the result of |
| 4401 | // applying a conversion function to the argument |
| 4402 | // expression, the implicit conversion sequence is a |
| 4403 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4404 | // second standard conversion sequence either an identity |
| 4405 | // conversion or, if the conversion function returns an |
| 4406 | // entity of a type that is a derived class of the parameter |
| 4407 | // type, a derived-to-base Conversion. |
| 4408 | if (!Best->FinalConversion.DirectBinding) |
| 4409 | return false; |
| 4410 | |
| 4411 | ICS.setUserDefined(); |
| 4412 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4413 | ICS.UserDefined.After = Best->FinalConversion; |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4414 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4415 | ICS.UserDefined.ConversionFunction = Best->Function; |
| John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4416 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4417 | ICS.UserDefined.EllipsisConversion = false; |
| 4418 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4419 | ICS.UserDefined.After.DirectBinding && |
| 4420 | "Expected a direct reference binding!"); |
| 4421 | return true; |
| 4422 | |
| 4423 | case OR_Ambiguous: |
| 4424 | ICS.setAmbiguous(); |
| 4425 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4426 | Cand != CandidateSet.end(); ++Cand) |
| 4427 | if (Cand->Viable) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 4428 | ICS.Ambiguous.addConversion(Cand->FoundDecl, Cand->Function); |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4429 | return true; |
| 4430 | |
| 4431 | case OR_No_Viable_Function: |
| 4432 | case OR_Deleted: |
| 4433 | // There was no suitable conversion, or we found a deleted |
| 4434 | // conversion; continue with other checks. |
| 4435 | return false; |
| 4436 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4437 | |
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4438 | llvm_unreachable("Invalid OverloadResult!"); |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4441 | /// Compute an implicit conversion sequence for reference |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4442 | /// initialization. |
| 4443 | static ImplicitConversionSequence |
| Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4444 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4445 | SourceLocation DeclLoc, |
| 4446 | bool SuppressUserConversions, |
| Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4447 | bool AllowExplicit) { |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4448 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4449 | |
| 4450 | // Most paths end in a failed conversion. |
| 4451 | ImplicitConversionSequence ICS; |
| 4452 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4453 | |
| 4454 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4455 | QualType T2 = Init->getType(); |
| 4456 | |
| 4457 | // If the initializer is the address of an overloaded function, try |
| 4458 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4459 | // type of the resulting function. |
| 4460 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4461 | DeclAccessPair Found; |
| 4462 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4463 | false, Found)) |
| 4464 | T2 = Fn->getType(); |
| 4465 | } |
| 4466 | |
| 4467 | // Compute some basic properties of the types and the initializer. |
| 4468 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4469 | bool DerivedToBase = false; |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4470 | bool ObjCConversion = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4471 | bool ObjCLifetimeConversion = false; |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4472 | Expr::Classification InitCategory = Init->Classify(S.Context); |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4473 | Sema::ReferenceCompareResult RefRelationship |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4474 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4475 | ObjCConversion, ObjCLifetimeConversion); |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4476 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4477 | |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4478 | // C++0x [dcl.init.ref]p5: |
| Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4479 | // A reference to type "cv1 T1" is initialized by an expression |
| 4480 | // of type "cv2 T2" as follows: |
| 4481 | |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4482 | // -- If reference is an lvalue reference and the initializer expression |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4483 | if (!isRValRef) { |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4484 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4485 | // reference-compatible with "cv2 T2," or |
| 4486 | // |
| 4487 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4488 | if (InitCategory.isLValue() && RefRelationship == Sema::Ref_Compatible) { |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4489 | // C++ [over.ics.ref]p1: |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4490 | // When a parameter of reference type binds directly (8.5.3) |
| 4491 | // to an argument expression, the implicit conversion sequence |
| 4492 | // is the identity conversion, unless the argument expression |
| 4493 | // has a type that is a derived class of the parameter type, |
| 4494 | // in which case the implicit conversion sequence is a |
| 4495 | // derived-to-base Conversion (13.3.3.1). |
| 4496 | ICS.setStandard(); |
| 4497 | ICS.Standard.First = ICK_Identity; |
| Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4498 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4499 | : ObjCConversion? ICK_Compatible_Conversion |
| 4500 | : ICK_Identity; |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4501 | ICS.Standard.Third = ICK_Identity; |
| 4502 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4503 | ICS.Standard.setToType(0, T2); |
| 4504 | ICS.Standard.setToType(1, T1); |
| 4505 | ICS.Standard.setToType(2, T1); |
| 4506 | ICS.Standard.ReferenceBinding = true; |
| 4507 | ICS.Standard.DirectBinding = true; |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4508 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4509 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4510 | ICS.Standard.BindsToRvalue = false; |
| Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4511 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4512 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4513 | ICS.Standard.CopyConstructor = nullptr; |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4514 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4515 | |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4516 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4517 | // derived-to-base conversions is suppressed when we're |
| 4518 | // computing the implicit conversion sequence (C++ |
| 4519 | // [over.best.ics]p2). |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4520 | return ICS; |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4521 | } |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4522 | |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4523 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4524 | // not reference-related to T2, and can be implicitly |
| 4525 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4526 | // is reference-compatible with "cv3 T3" 92) (this |
| 4527 | // conversion is selected by enumerating the applicable |
| 4528 | // conversion functions (13.3.1.6) and choosing the best |
| 4529 | // one through overload resolution (13.3)), |
| 4530 | if (!SuppressUserConversions && T2->isRecordType() && |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4531 | S.isCompleteType(DeclLoc, T2) && |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4532 | RefRelationship == Sema::Ref_Incompatible) { |
| Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4533 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4534 | Init, T2, /*AllowRvalues=*/false, |
| 4535 | AllowExplicit)) |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4536 | return ICS; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
| 4539 | |
| Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4540 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4541 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4542 | // shall be an rvalue reference. |
| Richard Smith | ce4f608 | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4543 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4544 | return ICS; |
| 4545 | |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4546 | // -- If the initializer expression |
| 4547 | // |
| 4548 | // -- is an xvalue, class prvalue, array prvalue or function |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4549 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
| Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4550 | if (RefRelationship == Sema::Ref_Compatible && |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4551 | (InitCategory.isXValue() || |
| Richard Smith | ce76629 | 2016-10-21 23:01:55 +0000 | [diff] [blame] | 4552 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4553 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4554 | ICS.setStandard(); |
| 4555 | ICS.Standard.First = ICK_Identity; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4556 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4557 | : ObjCConversion? ICK_Compatible_Conversion |
| 4558 | : ICK_Identity; |
| 4559 | ICS.Standard.Third = ICK_Identity; |
| 4560 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4561 | ICS.Standard.setToType(0, T2); |
| 4562 | ICS.Standard.setToType(1, T1); |
| 4563 | ICS.Standard.setToType(2, T1); |
| 4564 | ICS.Standard.ReferenceBinding = true; |
| 4565 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4566 | // binding unless we're binding to a class prvalue. |
| 4567 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4568 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4569 | // standard library implementors; therefore, we need the xvalue check here. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | ICS.Standard.DirectBinding = |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4571 | S.getLangOpts().CPlusPlus11 || |
| Richard Smith | b94afe1 | 2014-07-14 19:54:05 +0000 | [diff] [blame] | 4572 | !(InitCategory.isPRValue() || T2->isRecordType()); |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4573 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4574 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4575 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
| Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4576 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4577 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4578 | ICS.Standard.CopyConstructor = nullptr; |
| Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4579 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4580 | return ICS; |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4581 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4582 | |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4583 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4584 | // reference-related to T2, and can be implicitly converted to |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | // an xvalue, class prvalue, or function lvalue of type |
| 4586 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4587 | // "cv3 T3", |
| 4588 | // |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4589 | // then the reference is bound to the value of the initializer |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4590 | // expression in the first case and to the result of the conversion |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | // in the second case (or, in either case, to an appropriate base |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4592 | // class subobject). |
| 4593 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4594 | T2->isRecordType() && S.isCompleteType(DeclLoc, T2) && |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4595 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4596 | Init, T2, /*AllowRvalues=*/true, |
| 4597 | AllowExplicit)) { |
| 4598 | // In the second case, if the reference is an rvalue reference |
| 4599 | // and the second standard conversion sequence of the |
| 4600 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4601 | // conversion, the program is ill-formed. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4602 | if (ICS.isUserDefined() && isRValRef && |
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4603 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4604 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4605 | |
| Douglas Gregor | 95273c3 | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4606 | return ICS; |
| Rafael Espindola | be468d9 | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4607 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 4609 | // A temporary of function type cannot be created; don't even try. |
| 4610 | if (T1->isFunctionType()) |
| 4611 | return ICS; |
| 4612 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4613 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4614 | // initialized from the initializer expression using the |
| 4615 | // rules for a non-reference copy initialization (8.5). The |
| 4616 | // reference is then bound to the temporary. If T1 is |
| 4617 | // reference-related to T2, cv1 must be the same |
| 4618 | // cv-qualification as, or greater cv-qualification than, |
| 4619 | // cv2; otherwise, the program is ill-formed. |
| 4620 | if (RefRelationship == Sema::Ref_Related) { |
| 4621 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4622 | // we would be reference-compatible or reference-compatible with |
| 4623 | // added qualification. But that wasn't the case, so the reference |
| 4624 | // initialization fails. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4625 | // |
| 4626 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 4627 | // ObjC GC, lifetime and unaligned qualifiers aren't important. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4628 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4629 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4630 | T1Quals.removeObjCGCAttr(); |
| 4631 | T1Quals.removeObjCLifetime(); |
| 4632 | T2Quals.removeObjCGCAttr(); |
| 4633 | T2Quals.removeObjCLifetime(); |
| Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 4634 | // MS compiler ignores __unaligned qualifier for references; do the same. |
| 4635 | T1Quals.removeUnaligned(); |
| 4636 | T2Quals.removeUnaligned(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4637 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4638 | return ICS; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | // If at least one of the types is a class type, the types are not |
| 4642 | // related, and we aren't allowed any user conversions, the |
| 4643 | // reference binding fails. This case is important for breaking |
| 4644 | // recursion, since TryImplicitConversion below will attempt to |
| 4645 | // create a temporary through the use of a copy constructor. |
| 4646 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4647 | (T1->isRecordType() || T2->isRecordType())) |
| 4648 | return ICS; |
| 4649 | |
| Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4650 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4651 | // reference, the initializer expression shall not be an lvalue. |
| 4652 | if (RefRelationship >= Sema::Ref_Related && |
| 4653 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4654 | return ICS; |
| 4655 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4656 | // C++ [over.ics.ref]p2: |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4657 | // When a parameter of reference type is not bound directly to |
| 4658 | // an argument expression, the conversion sequence is the one |
| 4659 | // required to convert the argument expression to the |
| 4660 | // underlying type of the reference according to |
| 4661 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4662 | // to copy-initializing a temporary of the underlying type with |
| 4663 | // the argument expression. Any difference in top-level |
| 4664 | // cv-qualification is subsumed by the initialization itself |
| 4665 | // and does not constitute a conversion. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4666 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4667 | /*AllowExplicit=*/false, |
| Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4668 | /*InOverloadResolution=*/false, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4669 | /*CStyle=*/false, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4670 | /*AllowObjCWritebackConversion=*/false, |
| 4671 | /*AllowObjCConversionOnExplicit=*/false); |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4672 | |
| 4673 | // Of course, that's still a reference binding. |
| 4674 | if (ICS.isStandard()) { |
| 4675 | ICS.Standard.ReferenceBinding = true; |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4676 | ICS.Standard.IsLvalueReference = !isRValRef; |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 4677 | ICS.Standard.BindsToFunctionLvalue = false; |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4678 | ICS.Standard.BindsToRvalue = true; |
| Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4679 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4680 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4681 | } else if (ICS.isUserDefined()) { |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 4682 | const ReferenceType *LValRefType = |
| 4683 | ICS.UserDefined.ConversionFunction->getReturnType() |
| 4684 | ->getAs<LValueReferenceType>(); |
| 4685 | |
| 4686 | // C++ [over.ics.ref]p3: |
| 4687 | // Except for an implicit object parameter, for which see 13.3.1, a |
| 4688 | // standard conversion sequence cannot be formed if it requires [...] |
| 4689 | // binding an rvalue reference to an lvalue other than a function |
| 4690 | // lvalue. |
| 4691 | // Note that the function case is not possible here. |
| 4692 | if (DeclType->isRValueReferenceType() && LValRefType) { |
| 4693 | // FIXME: This is the wrong BadConversionSequence. The problem is binding |
| 4694 | // an rvalue reference to a (non-function) lvalue, not binding an lvalue |
| 4695 | // reference to an rvalue! |
| 4696 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, DeclType); |
| 4697 | return ICS; |
| Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4698 | } |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 4699 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4700 | ICS.UserDefined.After.ReferenceBinding = true; |
| Douglas Gregor | 3ec7910 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4701 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| Richard Smith | 19172c4 | 2014-07-14 02:28:44 +0000 | [diff] [blame] | 4702 | ICS.UserDefined.After.BindsToFunctionLvalue = false; |
| 4703 | ICS.UserDefined.After.BindsToRvalue = !LValRefType; |
| Douglas Gregor | 3ec7910 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4704 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4705 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4706 | } |
| Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4707 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4708 | return ICS; |
| 4709 | } |
| 4710 | |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4711 | static ImplicitConversionSequence |
| 4712 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4713 | bool SuppressUserConversions, |
| 4714 | bool InOverloadResolution, |
| Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4715 | bool AllowObjCWritebackConversion, |
| 4716 | bool AllowExplicit = false); |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4717 | |
| 4718 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4719 | /// initializer list From. |
| 4720 | static ImplicitConversionSequence |
| 4721 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4722 | bool SuppressUserConversions, |
| 4723 | bool InOverloadResolution, |
| 4724 | bool AllowObjCWritebackConversion) { |
| 4725 | // C++11 [over.ics.list]p1: |
| 4726 | // When an argument is an initializer list, it is not an expression and |
| 4727 | // special rules apply for converting it to a parameter type. |
| 4728 | |
| 4729 | ImplicitConversionSequence Result; |
| 4730 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4731 | |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4732 | // We need a complete type for what follows. Incomplete types can never be |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4733 | // initialized from init lists. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4734 | if (!S.isCompleteType(From->getLocStart(), ToType)) |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4735 | return Result; |
| 4736 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4737 | // Per DR1467: |
| 4738 | // If the parameter type is a class X and the initializer list has a single |
| 4739 | // element of type cv U, where U is X or a class derived from X, the |
| 4740 | // implicit conversion sequence is the one required to convert the element |
| 4741 | // to the parameter type. |
| 4742 | // |
| 4743 | // Otherwise, if the parameter type is a character array [... ] |
| 4744 | // and the initializer list has a single element that is an |
| 4745 | // appropriately-typed string literal (8.5.2 [dcl.init.string]), the |
| 4746 | // implicit conversion sequence is the identity conversion. |
| 4747 | if (From->getNumInits() == 1) { |
| 4748 | if (ToType->isRecordType()) { |
| 4749 | QualType InitType = From->getInit(0)->getType(); |
| 4750 | if (S.Context.hasSameUnqualifiedType(InitType, ToType) || |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4751 | S.IsDerivedFrom(From->getLocStart(), InitType, ToType)) |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4752 | return TryCopyInitialization(S, From->getInit(0), ToType, |
| 4753 | SuppressUserConversions, |
| 4754 | InOverloadResolution, |
| 4755 | AllowObjCWritebackConversion); |
| 4756 | } |
| Richard Smith | 1bbaba8 | 2015-01-27 23:23:39 +0000 | [diff] [blame] | 4757 | // FIXME: Check the other conditions here: array of character type, |
| 4758 | // initializer is a string literal. |
| 4759 | if (ToType->isArrayType()) { |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4760 | InitializedEntity Entity = |
| 4761 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4762 | /*Consumed=*/false); |
| 4763 | if (S.CanPerformCopyInitialization(Entity, From)) { |
| 4764 | Result.setStandard(); |
| 4765 | Result.Standard.setAsIdentityConversion(); |
| 4766 | Result.Standard.setFromType(ToType); |
| 4767 | Result.Standard.setAllToTypes(ToType); |
| 4768 | return Result; |
| 4769 | } |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | // C++14 [over.ics.list]p2: Otherwise, if the parameter type [...] (below). |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4774 | // C++11 [over.ics.list]p2: |
| 4775 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4776 | // all the elements can be implicitly converted to X, the implicit |
| 4777 | // conversion sequence is the worst conversion necessary to convert an |
| 4778 | // element of the list to X. |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4779 | // |
| 4780 | // C++14 [over.ics.list]p3: |
| NAKAMURA Takumi | b01d86b | 2015-02-25 11:02:00 +0000 | [diff] [blame] | 4781 | // Otherwise, if the parameter type is "array of N X", if the initializer |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4782 | // list has exactly N elements or if it has fewer than N elements and X is |
| 4783 | // default-constructible, and if all the elements of the initializer list |
| 4784 | // can be implicitly converted to X, the implicit conversion sequence is |
| 4785 | // the worst conversion necessary to convert an element of the list to X. |
| Richard Smith | 1bbaba8 | 2015-01-27 23:23:39 +0000 | [diff] [blame] | 4786 | // |
| 4787 | // FIXME: We're missing a lot of these checks. |
| Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4788 | bool toStdInitializerList = false; |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4789 | QualType X; |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4790 | if (ToType->isArrayType()) |
| Richard Smith | 0db1ea5 | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4791 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4792 | else |
| Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4793 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4794 | if (!X.isNull()) { |
| 4795 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4796 | Expr *Init = From->getInit(i); |
| 4797 | ImplicitConversionSequence ICS = |
| 4798 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4799 | InOverloadResolution, |
| 4800 | AllowObjCWritebackConversion); |
| 4801 | // If a single element isn't convertible, fail. |
| 4802 | if (ICS.isBad()) { |
| 4803 | Result = ICS; |
| 4804 | break; |
| 4805 | } |
| 4806 | // Otherwise, look for the worst conversion. |
| 4807 | if (Result.isBad() || |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 4808 | CompareImplicitConversionSequences(S, From->getLocStart(), ICS, |
| 4809 | Result) == |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4810 | ImplicitConversionSequence::Worse) |
| 4811 | Result = ICS; |
| 4812 | } |
| Douglas Gregor | 0f5c1c0 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4813 | |
| 4814 | // For an empty list, we won't have computed any conversion sequence. |
| 4815 | // Introduce the identity conversion sequence. |
| 4816 | if (From->getNumInits() == 0) { |
| 4817 | Result.setStandard(); |
| 4818 | Result.Standard.setAsIdentityConversion(); |
| 4819 | Result.Standard.setFromType(ToType); |
| 4820 | Result.Standard.setAllToTypes(ToType); |
| 4821 | } |
| 4822 | |
| Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4823 | Result.setStdInitializerListElement(toStdInitializerList); |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4824 | return Result; |
| Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4825 | } |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4826 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4827 | // C++14 [over.ics.list]p4: |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4828 | // C++11 [over.ics.list]p3: |
| 4829 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4830 | // resolution chooses a single best constructor [...] the implicit |
| 4831 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4832 | // constructors are viable but none is better than the others, the |
| 4833 | // implicit conversion sequence is a user-defined conversion sequence. |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4834 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4835 | // This function can deal with initializer lists. |
| Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4836 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4837 | /*AllowExplicit=*/false, |
| 4838 | InOverloadResolution, /*CStyle=*/false, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4839 | AllowObjCWritebackConversion, |
| 4840 | /*AllowObjCConversionOnExplicit=*/false); |
| Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4841 | } |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4842 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4843 | // C++14 [over.ics.list]p5: |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4844 | // C++11 [over.ics.list]p4: |
| 4845 | // Otherwise, if the parameter has an aggregate type which can be |
| 4846 | // initialized from the initializer list [...] the implicit conversion |
| 4847 | // sequence is a user-defined conversion sequence. |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4848 | if (ToType->isAggregateType()) { |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4849 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4850 | // down to checking whether the initialization works. |
| 4851 | // FIXME: Find out whether this parameter is consumed or not. |
| Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 4852 | // FIXME: Expose SemaInit's aggregate initialization code so that we don't |
| 4853 | // need to call into the initialization code here; overload resolution |
| 4854 | // should not be doing that. |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4855 | InitializedEntity Entity = |
| 4856 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4857 | /*Consumed=*/false); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4858 | if (S.CanPerformCopyInitialization(Entity, From)) { |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4859 | Result.setUserDefined(); |
| 4860 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4861 | // Initializer lists don't have a type. |
| 4862 | Result.UserDefined.Before.setFromType(QualType()); |
| 4863 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4864 | |
| 4865 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4866 | Result.UserDefined.After.setFromType(ToType); |
| 4867 | Result.UserDefined.After.setAllToTypes(ToType); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4868 | Result.UserDefined.ConversionFunction = nullptr; |
| Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4869 | } |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4870 | return Result; |
| 4871 | } |
| 4872 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4873 | // C++14 [over.ics.list]p6: |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4874 | // C++11 [over.ics.list]p5: |
| 4875 | // Otherwise, if the parameter is a reference, see 13.3.3.1.4. |
| Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4876 | if (ToType->isReferenceType()) { |
| 4877 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4878 | // mention initializer lists in any way. So we go by what list- |
| 4879 | // initialization would do and try to extrapolate from that. |
| 4880 | |
| 4881 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4882 | |
| 4883 | // If the initializer list has a single element that is reference-related |
| 4884 | // to the parameter type, we initialize the reference from that. |
| 4885 | if (From->getNumInits() == 1) { |
| 4886 | Expr *Init = From->getInit(0); |
| 4887 | |
| 4888 | QualType T2 = Init->getType(); |
| 4889 | |
| 4890 | // If the initializer is the address of an overloaded function, try |
| 4891 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4892 | // type of the resulting function. |
| 4893 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4894 | DeclAccessPair Found; |
| 4895 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4896 | Init, ToType, false, Found)) |
| 4897 | T2 = Fn->getType(); |
| 4898 | } |
| 4899 | |
| 4900 | // Compute some basic properties of the types and the initializer. |
| 4901 | bool dummy1 = false; |
| 4902 | bool dummy2 = false; |
| 4903 | bool dummy3 = false; |
| 4904 | Sema::ReferenceCompareResult RefRelationship |
| 4905 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4906 | dummy2, dummy3); |
| 4907 | |
| Richard Smith | 4d2bbd7 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4908 | if (RefRelationship >= Sema::Ref_Related) { |
| Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4909 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4910 | SuppressUserConversions, |
| 4911 | /*AllowExplicit=*/false); |
| Richard Smith | 4d2bbd7 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4912 | } |
| Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | // Otherwise, we bind the reference to a temporary created from the |
| 4916 | // initializer list. |
| 4917 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4918 | InOverloadResolution, |
| 4919 | AllowObjCWritebackConversion); |
| 4920 | if (Result.isFailure()) |
| 4921 | return Result; |
| 4922 | assert(!Result.isEllipsis() && |
| 4923 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4924 | |
| 4925 | // Can we even bind to a temporary? |
| 4926 | if (ToType->isRValueReferenceType() || |
| 4927 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4928 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4929 | Result.UserDefined.After; |
| 4930 | SCS.ReferenceBinding = true; |
| 4931 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4932 | SCS.BindsToRvalue = true; |
| 4933 | SCS.BindsToFunctionLvalue = false; |
| 4934 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4935 | SCS.ObjCLifetimeConversionBinding = false; |
| 4936 | } else |
| 4937 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4938 | From, ToType); |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4939 | return Result; |
| Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4940 | } |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4941 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4942 | // C++14 [over.ics.list]p7: |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4943 | // C++11 [over.ics.list]p6: |
| 4944 | // Otherwise, if the parameter type is not a class: |
| 4945 | if (!ToType->isRecordType()) { |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4946 | // - if the initializer list has one element that is not itself an |
| 4947 | // initializer list, the implicit conversion sequence is the one |
| 4948 | // required to convert the element to the parameter type. |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4949 | unsigned NumInits = From->getNumInits(); |
| Richard Smith | 1bbaba8 | 2015-01-27 23:23:39 +0000 | [diff] [blame] | 4950 | if (NumInits == 1 && !isa<InitListExpr>(From->getInit(0))) |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4951 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4952 | SuppressUserConversions, |
| 4953 | InOverloadResolution, |
| 4954 | AllowObjCWritebackConversion); |
| 4955 | // - if the initializer list has no elements, the implicit conversion |
| 4956 | // sequence is the identity conversion. |
| 4957 | else if (NumInits == 0) { |
| 4958 | Result.setStandard(); |
| 4959 | Result.Standard.setAsIdentityConversion(); |
| John McCall | b73bc9a | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4960 | Result.Standard.setFromType(ToType); |
| 4961 | Result.Standard.setAllToTypes(ToType); |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4962 | } |
| 4963 | return Result; |
| 4964 | } |
| 4965 | |
| Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 4966 | // C++14 [over.ics.list]p8: |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4967 | // C++11 [over.ics.list]p7: |
| 4968 | // In all cases other than those enumerated above, no conversion is possible |
| 4969 | return Result; |
| 4970 | } |
| 4971 | |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4972 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4973 | /// ToType from the expression From. Return the implicit conversion |
| 4974 | /// sequence required to pass this argument, which may be a bad |
| 4975 | /// conversion sequence (meaning that the argument cannot be passed to |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4976 | /// a parameter of this type). If @p SuppressUserConversions, then we |
| Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4977 | /// do not permit any user-defined conversion sequences. |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4978 | static ImplicitConversionSequence |
| 4979 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | bool SuppressUserConversions, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4981 | bool InOverloadResolution, |
| Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4982 | bool AllowObjCWritebackConversion, |
| 4983 | bool AllowExplicit) { |
| Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4984 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4985 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4986 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4987 | |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4988 | if (ToType->isReferenceType()) |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4989 | return TryReferenceInit(S, From, ToType, |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4990 | /*FIXME:*/From->getLocStart(), |
| 4991 | SuppressUserConversions, |
| Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4992 | AllowExplicit); |
| Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4993 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4994 | return TryImplicitConversion(S, From, ToType, |
| 4995 | SuppressUserConversions, |
| 4996 | /*AllowExplicit=*/false, |
| Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4997 | InOverloadResolution, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4998 | /*CStyle=*/false, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4999 | AllowObjCWritebackConversion, |
| 5000 | /*AllowObjCConversionOnExplicit=*/false); |
| Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
| Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 5003 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 5004 | const CanQualType ToQTy, |
| 5005 | Sema &S, |
| 5006 | SourceLocation Loc, |
| 5007 | ExprValueKind FromVK) { |
| 5008 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 5009 | ImplicitConversionSequence ICS = |
| 5010 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 5011 | |
| 5012 | return !ICS.isBad(); |
| 5013 | } |
| 5014 | |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5015 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 5016 | /// parameter of the given member function (@c Method) from the |
| 5017 | /// expression @p From. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5018 | static ImplicitConversionSequence |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5019 | TryObjectArgumentInitialization(Sema &S, SourceLocation Loc, QualType FromType, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5020 | Expr::Classification FromClassification, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5021 | CXXMethodDecl *Method, |
| 5022 | CXXRecordDecl *ActingContext) { |
| 5023 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
| Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 5024 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 5025 | // const volatile object. |
| 5026 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 5027 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5028 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5029 | |
| 5030 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 5031 | // to exit early. |
| 5032 | ImplicitConversionSequence ICS; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5033 | |
| 5034 | // We need to have an object of class type. |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5035 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5036 | FromType = PT->getPointeeType(); |
| 5037 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5038 | // When we had a pointer, it's implicitly dereferenced, so we |
| 5039 | // better have an lvalue. |
| 5040 | assert(FromClassification.isLValue()); |
| 5041 | } |
| 5042 | |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5043 | assert(FromType->isRecordType()); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5044 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5045 | // C++0x [over.match.funcs]p4: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5046 | // For non-static member functions, the type of the implicit object |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5047 | // parameter is |
| 5048 | // |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5049 | // - "lvalue reference to cv X" for functions declared without a |
| 5050 | // ref-qualifier or with the & ref-qualifier |
| 5051 | // - "rvalue reference to cv X" for functions declared with the && |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5052 | // ref-qualifier |
| 5053 | // |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5054 | // where X is the class of which the function is a member and cv is the |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5055 | // cv-qualification on the member function declaration. |
| 5056 | // |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5057 | // However, when finding an implicit conversion sequence for the argument, we |
| Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 5058 | // are not allowed to perform user-defined conversions |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5059 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 5060 | // reference binding here, that allows class rvalues to bind to |
| 5061 | // non-constant references. |
| 5062 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5063 | // First check the qualifiers. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5064 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5065 | if (ImplicitParamType.getCVRQualifiers() |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5066 | != FromTypeCanon.getLocalCVRQualifiers() && |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5067 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5068 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| Richard Smith | 03c66d3 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 5069 | FromType, ImplicitParamType); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5070 | return ICS; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5071 | } |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5072 | |
| 5073 | // Check that we have either the same type or a derived type. It |
| 5074 | // affects the conversion rank. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5075 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5076 | ImplicitConversionKind SecondKind; |
| 5077 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 5078 | SecondKind = ICK_Identity; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5079 | } else if (S.IsDerivedFrom(Loc, FromType, ClassType)) |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5080 | SecondKind = ICK_Derived_To_Base; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5081 | else { |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5082 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 5083 | FromType, ImplicitParamType); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5084 | return ICS; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5085 | } |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5086 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5087 | // Check the ref-qualifier. |
| 5088 | switch (Method->getRefQualifier()) { |
| 5089 | case RQ_None: |
| 5090 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 5091 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5093 | case RQ_LValue: |
| 5094 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 5095 | // non-const lvalue reference cannot bind to an rvalue |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5096 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5097 | ImplicitParamType); |
| 5098 | return ICS; |
| 5099 | } |
| 5100 | break; |
| 5101 | |
| 5102 | case RQ_RValue: |
| 5103 | if (!FromClassification.isRValue()) { |
| 5104 | // rvalue reference cannot bind to an lvalue |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5105 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5106 | ImplicitParamType); |
| 5107 | return ICS; |
| 5108 | } |
| 5109 | break; |
| 5110 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5112 | // Success. Mark this as a reference binding. |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5113 | ICS.setStandard(); |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5114 | ICS.Standard.setAsIdentityConversion(); |
| 5115 | ICS.Standard.Second = SecondKind; |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5116 | ICS.Standard.setFromType(FromType); |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5117 | ICS.Standard.setAllToTypes(ImplicitParamType); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5118 | ICS.Standard.ReferenceBinding = true; |
| 5119 | ICS.Standard.DirectBinding = true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5120 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
| Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 5121 | ICS.Standard.BindsToFunctionLvalue = false; |
| Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 5122 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 5123 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 5124 | = (Method->getRefQualifier() == RQ_None); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5125 | return ICS; |
| 5126 | } |
| 5127 | |
| 5128 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 5129 | /// the implicit object parameter for the given Method with the given |
| 5130 | /// expression. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5131 | ExprResult |
| 5132 | Sema::PerformObjectArgumentInitialization(Expr *From, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5133 | NestedNameSpecifier *Qualifier, |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5134 | NamedDecl *FoundDecl, |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 5135 | CXXMethodDecl *Method) { |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5136 | QualType FromRecordType, DestType; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5137 | QualType ImplicitParamRecordType = |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5138 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5139 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5140 | Expr::Classification FromClassification; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5141 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5142 | FromRecordType = PT->getPointeeType(); |
| 5143 | DestType = Method->getThisType(Context); |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5144 | FromClassification = Expr::Classification::makeSimpleLValue(); |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5145 | } else { |
| 5146 | FromRecordType = From->getType(); |
| 5147 | DestType = ImplicitParamRecordType; |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5148 | FromClassification = From->Classify(Context); |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5151 | // Note that we always use the true parent context when performing |
| 5152 | // the actual argument initialization. |
| Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 5153 | ImplicitConversionSequence ICS = TryObjectArgumentInitialization( |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5154 | *this, From->getLocStart(), From->getType(), FromClassification, Method, |
| 5155 | Method->getParent()); |
| Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 5156 | if (ICS.isBad()) { |
| Jacob Bandes-Storch | bb93578 | 2017-12-31 18:27:29 +0000 | [diff] [blame] | 5157 | switch (ICS.Bad.Kind) { |
| 5158 | case BadConversionSequence::bad_qualifiers: { |
| Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 5159 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 5160 | Qualifiers ToQs = DestType.getQualifiers(); |
| 5161 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5162 | if (CVR) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5163 | Diag(From->getLocStart(), |
| Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 5164 | diag::err_member_function_call_bad_cvr) |
| 5165 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 5166 | << From->getSourceRange(); |
| 5167 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 5168 | << Method->getDeclName(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5169 | return ExprError(); |
| Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 5170 | } |
| Jacob Bandes-Storch | bb93578 | 2017-12-31 18:27:29 +0000 | [diff] [blame] | 5171 | break; |
| 5172 | } |
| 5173 | |
| 5174 | case BadConversionSequence::lvalue_ref_to_rvalue: |
| 5175 | case BadConversionSequence::rvalue_ref_to_lvalue: { |
| 5176 | bool IsRValueQualified = |
| 5177 | Method->getRefQualifier() == RefQualifierKind::RQ_RValue; |
| 5178 | Diag(From->getLocStart(), diag::err_member_function_call_bad_ref) |
| 5179 | << Method->getDeclName() << FromClassification.isRValue() |
| 5180 | << IsRValueQualified; |
| 5181 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 5182 | << Method->getDeclName(); |
| 5183 | return ExprError(); |
| 5184 | } |
| 5185 | |
| 5186 | case BadConversionSequence::no_conversion: |
| 5187 | case BadConversionSequence::unrelated_class: |
| 5188 | break; |
| Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5191 | return Diag(From->getLocStart(), |
| Jacob Bandes-Storch | bb93578 | 2017-12-31 18:27:29 +0000 | [diff] [blame] | 5192 | diag::err_member_function_call_bad_type) |
| Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5193 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
| Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 5194 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5195 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5196 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 5197 | ExprResult FromRes = |
| 5198 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 5199 | if (FromRes.isInvalid()) |
| 5200 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5201 | From = FromRes.get(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5202 | } |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5203 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 5204 | if (!Context.hasSameType(From->getType(), DestType)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5205 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5206 | From->getValueKind()).get(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5207 | return From; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5210 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 5211 | /// expression From to bool (C++0x [conv]p3). |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5212 | static ImplicitConversionSequence |
| 5213 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5214 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
| Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 5215 | /*SuppressUserConversions=*/false, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5216 | /*AllowExplicit=*/true, |
| Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 5217 | /*InOverloadResolution=*/false, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5218 | /*CStyle=*/false, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5219 | /*AllowObjCWritebackConversion=*/false, |
| 5220 | /*AllowObjCConversionOnExplicit=*/false); |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 5224 | /// of the expression From to bool (C++0x [conv]p3). |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5225 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5226 | if (checkPlaceholderForOverload(*this, From)) |
| 5227 | return ExprError(); |
| 5228 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5229 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5230 | if (!ICS.isBad()) |
| 5231 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5232 | |
| Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 5233 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5234 | return Diag(From->getLocStart(), |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5235 | diag::err_typecheck_bool_condition) |
| Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 5236 | << From->getType() << From->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5237 | return ExprError(); |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5238 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5240 | /// Check that the specified conversion is permitted in a converted constant |
| 5241 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 5242 | /// is acceptable. |
| 5243 | static bool CheckConvertedConstantConversions(Sema &S, |
| 5244 | StandardConversionSequence &SCS) { |
| 5245 | // Since we know that the target type is an integral or unscoped enumeration |
| 5246 | // type, most conversion kinds are impossible. All possible First and Third |
| 5247 | // conversions are fine. |
| 5248 | switch (SCS.Second) { |
| 5249 | case ICK_Identity: |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 5250 | case ICK_Function_Conversion: |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5251 | case ICK_Integral_Promotion: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5252 | case ICK_Integral_Conversion: // Narrowing conversions are checked elsewhere. |
| Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 5253 | case ICK_Zero_Queue_Conversion: |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5254 | return true; |
| 5255 | |
| 5256 | case ICK_Boolean_Conversion: |
| Richard Smith | ca24ed4 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 5257 | // Conversion from an integral or unscoped enumeration type to bool is |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5258 | // classified as ICK_Boolean_Conversion, but it's also arguably an integral |
| 5259 | // conversion, so we allow it in a converted constant expression. |
| 5260 | // |
| 5261 | // FIXME: Per core issue 1407, we should not allow this, but that breaks |
| 5262 | // a lot of popular code. We should at least add a warning for this |
| 5263 | // (non-conforming) extension. |
| Richard Smith | ca24ed4 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 5264 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 5265 | SCS.getToType(2)->isBooleanType(); |
| 5266 | |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5267 | case ICK_Pointer_Conversion: |
| 5268 | case ICK_Pointer_Member: |
| 5269 | // C++1z: null pointer conversions and null member pointer conversions are |
| 5270 | // only permitted if the source type is std::nullptr_t. |
| 5271 | return SCS.getFromType()->isNullPtrType(); |
| 5272 | |
| 5273 | case ICK_Floating_Promotion: |
| 5274 | case ICK_Complex_Promotion: |
| 5275 | case ICK_Floating_Conversion: |
| 5276 | case ICK_Complex_Conversion: |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5277 | case ICK_Floating_Integral: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5278 | case ICK_Compatible_Conversion: |
| 5279 | case ICK_Derived_To_Base: |
| 5280 | case ICK_Vector_Conversion: |
| 5281 | case ICK_Vector_Splat: |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5282 | case ICK_Complex_Real: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5283 | case ICK_Block_Pointer_Conversion: |
| 5284 | case ICK_TransparentUnionConversion: |
| 5285 | case ICK_Writeback_Conversion: |
| 5286 | case ICK_Zero_Event_Conversion: |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 5287 | case ICK_C_Only_Conversion: |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 5288 | case ICK_Incompatible_Pointer_Conversion: |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5289 | return false; |
| 5290 | |
| 5291 | case ICK_Lvalue_To_Rvalue: |
| 5292 | case ICK_Array_To_Pointer: |
| 5293 | case ICK_Function_To_Pointer: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5294 | llvm_unreachable("found a first conversion kind in Second"); |
| 5295 | |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5296 | case ICK_Qualification: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5297 | llvm_unreachable("found a third conversion kind in Second"); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5298 | |
| 5299 | case ICK_Num_Conversion_Kinds: |
| 5300 | break; |
| 5301 | } |
| 5302 | |
| 5303 | llvm_unreachable("unknown conversion kind"); |
| 5304 | } |
| 5305 | |
| 5306 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 5307 | /// converted constant expression of type T, perform the conversion and produce |
| 5308 | /// the converted expression, per C++11 [expr.const]p3. |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5309 | static ExprResult CheckConvertedConstantExpression(Sema &S, Expr *From, |
| 5310 | QualType T, APValue &Value, |
| 5311 | Sema::CCEKind CCE, |
| 5312 | bool RequireInt) { |
| 5313 | assert(S.getLangOpts().CPlusPlus11 && |
| 5314 | "converted constant expression outside C++11"); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5315 | |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5316 | if (checkPlaceholderForOverload(S, From)) |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5317 | return ExprError(); |
| 5318 | |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5319 | // C++1z [expr.const]p3: |
| 5320 | // A converted constant expression of type T is an expression, |
| 5321 | // implicitly converted to type T, where the converted |
| 5322 | // expression is a constant expression and the implicit conversion |
| 5323 | // sequence contains only [... list of conversions ...]. |
| Ismail Pazarbasi | 4a00774 | 2016-09-07 18:24:54 +0000 | [diff] [blame] | 5324 | // C++1z [stmt.if]p2: |
| 5325 | // If the if statement is of the form if constexpr, the value of the |
| 5326 | // condition shall be a contextually converted constant expression of type |
| 5327 | // bool. |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5328 | ImplicitConversionSequence ICS = |
| Ismail Pazarbasi | 4a00774 | 2016-09-07 18:24:54 +0000 | [diff] [blame] | 5329 | CCE == Sema::CCEK_ConstexprIf |
| 5330 | ? TryContextuallyConvertToBool(S, From) |
| 5331 | : TryCopyInitialization(S, From, T, |
| 5332 | /*SuppressUserConversions=*/false, |
| 5333 | /*InOverloadResolution=*/false, |
| 5334 | /*AllowObjcWritebackConversion=*/false, |
| 5335 | /*AllowExplicit=*/false); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5336 | StandardConversionSequence *SCS = nullptr; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5337 | switch (ICS.getKind()) { |
| 5338 | case ImplicitConversionSequence::StandardConversion: |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5339 | SCS = &ICS.Standard; |
| 5340 | break; |
| 5341 | case ImplicitConversionSequence::UserDefinedConversion: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5342 | // We are converting to a non-class type, so the Before sequence |
| 5343 | // must be trivial. |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5344 | SCS = &ICS.UserDefined.After; |
| 5345 | break; |
| 5346 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5347 | case ImplicitConversionSequence::BadConversion: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5348 | if (!S.DiagnoseMultipleUserDefinedConversion(From, T)) |
| 5349 | return S.Diag(From->getLocStart(), |
| 5350 | diag::err_typecheck_converted_constant_expression) |
| 5351 | << From->getType() << From->getSourceRange() << T; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5352 | return ExprError(); |
| 5353 | |
| 5354 | case ImplicitConversionSequence::EllipsisConversion: |
| 5355 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 5356 | } |
| 5357 | |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5358 | // Check that we would only use permitted conversions. |
| 5359 | if (!CheckConvertedConstantConversions(S, *SCS)) { |
| 5360 | return S.Diag(From->getLocStart(), |
| 5361 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 5362 | << From->getType() << From->getSourceRange() << T; |
| 5363 | } |
| 5364 | // [...] and where the reference binding (if any) binds directly. |
| 5365 | if (SCS->ReferenceBinding && !SCS->DirectBinding) { |
| 5366 | return S.Diag(From->getLocStart(), |
| 5367 | diag::err_typecheck_converted_constant_expression_indirect) |
| 5368 | << From->getType() << From->getSourceRange() << T; |
| 5369 | } |
| 5370 | |
| 5371 | ExprResult Result = |
| 5372 | S.PerformImplicitConversion(From, T, ICS, Sema::AA_Converting); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5373 | if (Result.isInvalid()) |
| 5374 | return Result; |
| 5375 | |
| 5376 | // Check for a narrowing implicit conversion. |
| 5377 | APValue PreNarrowingValue; |
| Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5378 | QualType PreNarrowingType; |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5379 | switch (SCS->getNarrowingKind(S.Context, Result.get(), PreNarrowingValue, |
| Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5380 | PreNarrowingType)) { |
| Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5381 | case NK_Dependent_Narrowing: |
| 5382 | // Implicit conversion to a narrower type, but the expression is |
| 5383 | // value-dependent so we can't tell whether it's actually narrowing. |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5384 | case NK_Variable_Narrowing: |
| 5385 | // Implicit conversion to a narrower type, and the value is not a constant |
| 5386 | // expression. We'll diagnose this in a moment. |
| 5387 | case NK_Not_Narrowing: |
| 5388 | break; |
| 5389 | |
| 5390 | case NK_Constant_Narrowing: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5391 | S.Diag(From->getLocStart(), diag::ext_cce_narrowing) |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5392 | << CCE << /*Constant*/1 |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5393 | << PreNarrowingValue.getAsString(S.Context, PreNarrowingType) << T; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5394 | break; |
| 5395 | |
| 5396 | case NK_Type_Narrowing: |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5397 | S.Diag(From->getLocStart(), diag::ext_cce_narrowing) |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5398 | << CCE << /*Constant*/0 << From->getType() << T; |
| 5399 | break; |
| 5400 | } |
| 5401 | |
| Richard Smith | 52e624f | 2016-12-21 21:42:57 +0000 | [diff] [blame] | 5402 | if (Result.get()->isValueDependent()) { |
| 5403 | Value = APValue(); |
| 5404 | return Result; |
| 5405 | } |
| 5406 | |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5407 | // Check the expression is a constant expression. |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5408 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5409 | Expr::EvalResult Eval; |
| 5410 | Eval.Diag = &Notes; |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 5411 | Expr::ConstExprUsage Usage = CCE == Sema::CCEK_TemplateArg |
| 5412 | ? Expr::EvaluateForMangling |
| 5413 | : Expr::EvaluateForCodeGen; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5414 | |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 5415 | if (!Result.get()->EvaluateAsConstantExpr(Eval, Usage, S.Context) || |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5416 | (RequireInt && !Eval.Val.isInt())) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5417 | // The expression can't be folded, so we can't keep it at this position in |
| 5418 | // the AST. |
| 5419 | Result = ExprError(); |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5420 | } else { |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5421 | Value = Eval.Val; |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5422 | |
| 5423 | if (Notes.empty()) { |
| 5424 | // It's a constant expression. |
| 5425 | return Result; |
| 5426 | } |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5430 | if (Notes.size() == 1 && |
| 5431 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5432 | S.Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5433 | else { |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5434 | S.Diag(From->getLocStart(), diag::err_expr_not_cce) |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5435 | << CCE << From->getSourceRange(); |
| 5436 | for (unsigned I = 0; I < Notes.size(); ++I) |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5437 | S.Diag(Notes[I].first, Notes[I].second); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5438 | } |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5439 | return ExprError(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5442 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 5443 | APValue &Value, CCEKind CCE) { |
| 5444 | return ::CheckConvertedConstantExpression(*this, From, T, Value, CCE, false); |
| 5445 | } |
| 5446 | |
| 5447 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 5448 | llvm::APSInt &Value, |
| 5449 | CCEKind CCE) { |
| 5450 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 5451 | |
| 5452 | APValue V; |
| 5453 | auto R = ::CheckConvertedConstantExpression(*this, From, T, V, CCE, true); |
| Richard Smith | 01bfa68 | 2016-12-27 02:02:09 +0000 | [diff] [blame] | 5454 | if (!R.isInvalid() && !R.get()->isValueDependent()) |
| Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 5455 | Value = V.getInt(); |
| 5456 | return R; |
| 5457 | } |
| 5458 | |
| 5459 | |
| John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5460 | /// dropPointerConversions - If the given standard conversion sequence |
| 5461 | /// involves any pointer conversions, remove them. This may change |
| 5462 | /// the result type of the conversion sequence. |
| 5463 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5464 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5465 | SCS.Second = ICK_Identity; |
| 5466 | SCS.Third = ICK_Identity; |
| 5467 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5468 | } |
| Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5469 | } |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5470 | |
| John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5471 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5472 | /// convert the expression From to an Objective-C pointer type. |
| 5473 | static ImplicitConversionSequence |
| 5474 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5475 | // Do an implicit conversion to 'id'. |
| 5476 | QualType Ty = S.Context.getObjCIdType(); |
| 5477 | ImplicitConversionSequence ICS |
| 5478 | = TryImplicitConversion(S, From, Ty, |
| 5479 | // FIXME: Are these flags correct? |
| 5480 | /*SuppressUserConversions=*/false, |
| 5481 | /*AllowExplicit=*/true, |
| 5482 | /*InOverloadResolution=*/false, |
| 5483 | /*CStyle=*/false, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5484 | /*AllowObjCWritebackConversion=*/false, |
| 5485 | /*AllowObjCConversionOnExplicit=*/true); |
| John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5486 | |
| 5487 | // Strip off any final conversions to 'id'. |
| 5488 | switch (ICS.getKind()) { |
| 5489 | case ImplicitConversionSequence::BadConversion: |
| 5490 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5491 | case ImplicitConversionSequence::EllipsisConversion: |
| 5492 | break; |
| 5493 | |
| 5494 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5495 | dropPointerConversion(ICS.UserDefined.After); |
| 5496 | break; |
| 5497 | |
| 5498 | case ImplicitConversionSequence::StandardConversion: |
| 5499 | dropPointerConversion(ICS.Standard); |
| 5500 | break; |
| 5501 | } |
| 5502 | |
| 5503 | return ICS; |
| 5504 | } |
| 5505 | |
| 5506 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5507 | /// conversion of the expression From to an Objective-C pointer type. |
| Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 5508 | /// Returns a valid but null ExprResult if no conversion sequence exists. |
| John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5509 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5510 | if (checkPlaceholderForOverload(*this, From)) |
| 5511 | return ExprError(); |
| 5512 | |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5513 | QualType Ty = Context.getObjCIdType(); |
| John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5514 | ImplicitConversionSequence ICS = |
| 5515 | TryContextuallyConvertToObjCPointer(*this, From); |
| Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5516 | if (!ICS.isBad()) |
| 5517 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 5518 | return ExprResult(); |
| Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5519 | } |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5520 | |
| Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5521 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5522 | /// type of a permitted flavor. |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5523 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5524 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5525 | : T->isIntegralOrUnscopedEnumerationType(); |
| Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5528 | static ExprResult |
| 5529 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5530 | Sema::ContextualImplicitConverter &Converter, |
| 5531 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5532 | |
| 5533 | if (Converter.Suppress) |
| 5534 | return ExprError(); |
| 5535 | |
| 5536 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5537 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5538 | CXXConversionDecl *Conv = |
| 5539 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5540 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5541 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5542 | } |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5543 | return From; |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | static bool |
| 5547 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5548 | Sema::ContextualImplicitConverter &Converter, |
| 5549 | QualType T, bool HadMultipleCandidates, |
| 5550 | UnresolvedSetImpl &ExplicitConversions) { |
| 5551 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5552 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5553 | CXXConversionDecl *Conversion = |
| 5554 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5555 | |
| 5556 | // The user probably meant to invoke the given explicit |
| 5557 | // conversion; use it. |
| 5558 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5559 | std::string TypeStr; |
| 5560 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5561 | |
| 5562 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5563 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5564 | "static_cast<" + TypeStr + ">(") |
| 5565 | << FixItHint::CreateInsertion( |
| Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 5566 | SemaRef.getLocForEndOfToken(From->getLocEnd()), ")"); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5567 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5568 | |
| 5569 | // If we aren't in a SFINAE context, build a call to the |
| 5570 | // explicit conversion function. |
| 5571 | if (SemaRef.isSFINAEContext()) |
| 5572 | return true; |
| 5573 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5574 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, nullptr, Found); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5575 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5576 | HadMultipleCandidates); |
| 5577 | if (Result.isInvalid()) |
| 5578 | return true; |
| 5579 | // Record usage of conversion in an implicit cast. |
| 5580 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5581 | CK_UserDefinedConversion, Result.get(), |
| 5582 | nullptr, Result.get()->getValueKind()); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5583 | } |
| 5584 | return false; |
| 5585 | } |
| 5586 | |
| 5587 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5588 | Sema::ContextualImplicitConverter &Converter, |
| 5589 | QualType T, bool HadMultipleCandidates, |
| 5590 | DeclAccessPair &Found) { |
| 5591 | CXXConversionDecl *Conversion = |
| 5592 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5593 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, nullptr, Found); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5594 | |
| 5595 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5596 | if (!Converter.SuppressConversion) { |
| 5597 | if (SemaRef.isSFINAEContext()) |
| 5598 | return true; |
| 5599 | |
| 5600 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5601 | << From->getSourceRange(); |
| 5602 | } |
| 5603 | |
| 5604 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5605 | HadMultipleCandidates); |
| 5606 | if (Result.isInvalid()) |
| 5607 | return true; |
| 5608 | // Record usage of conversion in an implicit cast. |
| 5609 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5610 | CK_UserDefinedConversion, Result.get(), |
| 5611 | nullptr, Result.get()->getValueKind()); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5612 | return false; |
| 5613 | } |
| 5614 | |
| 5615 | static ExprResult finishContextualImplicitConversion( |
| 5616 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5617 | Sema::ContextualImplicitConverter &Converter) { |
| 5618 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5619 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5620 | << From->getSourceRange(); |
| 5621 | |
| 5622 | return SemaRef.DefaultLvalueConversion(From); |
| 5623 | } |
| 5624 | |
| 5625 | static void |
| 5626 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5627 | UnresolvedSetImpl &ViableConversions, |
| 5628 | OverloadCandidateSet &CandidateSet) { |
| 5629 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5630 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5631 | NamedDecl *D = FoundDecl.getDecl(); |
| 5632 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5633 | if (isa<UsingShadowDecl>(D)) |
| 5634 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5635 | |
| 5636 | CXXConversionDecl *Conv; |
| 5637 | FunctionTemplateDecl *ConvTemplate; |
| 5638 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5639 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5640 | else |
| 5641 | Conv = cast<CXXConversionDecl>(D); |
| 5642 | |
| 5643 | if (ConvTemplate) |
| 5644 | SemaRef.AddTemplateConversionCandidate( |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5645 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet, |
| 5646 | /*AllowObjCConversionOnExplicit=*/false); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5647 | else |
| 5648 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5649 | ToType, CandidateSet, |
| 5650 | /*AllowObjCConversionOnExplicit=*/false); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5651 | } |
| 5652 | } |
| 5653 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5654 | /// Attempt to convert the given expression to a type which is accepted |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5655 | /// by the given converter. |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5656 | /// |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5657 | /// This routine will attempt to convert an expression of class type to a |
| 5658 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5659 | /// must have a single non-explicit conversion function converting to a matching |
| 5660 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5661 | /// one target type. |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5662 | /// |
| Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5663 | /// \param Loc The source location of the construct that requires the |
| 5664 | /// conversion. |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5665 | /// |
| James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5666 | /// \param From The expression we're converting from. |
| Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5667 | /// |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5668 | /// \param Converter Used to control and diagnose the conversion process. |
| Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5669 | /// |
| Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5670 | /// \returns The expression, converted to an integral or enumeration type if |
| 5671 | /// successful. |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5672 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5673 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5674 | // We can't perform any more checking for type-dependent expressions. |
| 5675 | if (From->isTypeDependent()) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5676 | return From; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5677 | |
| Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5678 | // Process placeholders immediately. |
| 5679 | if (From->hasPlaceholderType()) { |
| 5680 | ExprResult result = CheckPlaceholderExpr(From); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5681 | if (result.isInvalid()) |
| 5682 | return result; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5683 | From = result.get(); |
| Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5684 | } |
| 5685 | |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5686 | // If the expression already has a matching type, we're golden. |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5687 | QualType T = From->getType(); |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5688 | if (Converter.match(T)) |
| Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5689 | return DefaultLvalueConversion(From); |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5690 | |
| 5691 | // FIXME: Check for missing '()' if T is a function type? |
| 5692 | |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5693 | // We can only perform contextual implicit conversions on objects of class |
| 5694 | // type. |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5695 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5696 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5697 | if (!Converter.Suppress) |
| 5698 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5699 | return From; |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5700 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5701 | |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5702 | // We must have a complete class type. |
| Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5703 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5704 | ContextualImplicitConverter &Converter; |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5705 | Expr *From; |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5706 | |
| 5707 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5708 | : Converter(Converter), From(From) {} |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5709 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5710 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5711 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5712 | } |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5713 | } IncompleteDiagnoser(Converter, From); |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5714 | |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 5715 | if (Converter.Suppress ? !isCompleteType(Loc, T) |
| 5716 | : RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5717 | return From; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5718 | |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5719 | // Look for a conversion to an integral or enumeration type. |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5720 | UnresolvedSet<4> |
| 5721 | ViableConversions; // These are *potentially* viable in C++1y. |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5722 | UnresolvedSet<4> ExplicitConversions; |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 5723 | const auto &Conversions = |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5724 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5725 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5726 | bool HadMultipleCandidates = |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 5727 | (std::distance(Conversions.begin(), Conversions.end()) > 1); |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5728 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5729 | // To check that there is only one target type, in C++1y: |
| 5730 | QualType ToType; |
| 5731 | bool HasUniqueTargetType = true; |
| 5732 | |
| 5733 | // Collect explicit or viable (potentially in C++1y) conversions. |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 5734 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5735 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5736 | CXXConversionDecl *Conversion; |
| 5737 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5738 | if (ConvTemplate) { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5739 | if (getLangOpts().CPlusPlus14) |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5740 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5741 | else |
| 5742 | continue; // C++11 does not consider conversion operator templates(?). |
| 5743 | } else |
| 5744 | Conversion = cast<CXXConversionDecl>(D); |
| 5745 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5746 | assert((!ConvTemplate || getLangOpts().CPlusPlus14) && |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5747 | "Conversion operator templates are considered potentially " |
| 5748 | "viable in C++1y"); |
| 5749 | |
| 5750 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5751 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5752 | |
| 5753 | if (Conversion->isExplicit()) { |
| 5754 | // FIXME: For C++1y, do we need this restriction? |
| 5755 | // cf. diagnoseNoViableConversion() |
| 5756 | if (!ConvTemplate) |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5757 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5758 | } else { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5759 | if (!ConvTemplate && getLangOpts().CPlusPlus14) { |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5760 | if (ToType.isNull()) |
| 5761 | ToType = CurToType.getUnqualifiedType(); |
| 5762 | else if (HasUniqueTargetType && |
| 5763 | (CurToType.getUnqualifiedType() != ToType)) |
| 5764 | HasUniqueTargetType = false; |
| 5765 | } |
| 5766 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5767 | } |
| Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5768 | } |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5769 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5770 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5771 | if (getLangOpts().CPlusPlus14) { |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5772 | // C++1y [conv]p6: |
| 5773 | // ... An expression e of class type E appearing in such a context |
| 5774 | // is said to be contextually implicitly converted to a specified |
| 5775 | // type T and is well-formed if and only if e can be implicitly |
| 5776 | // converted to a type T that is determined as follows: E is searched |
| Larisse Voufo | 67170bd | 2013-06-10 08:25:58 +0000 | [diff] [blame] | 5777 | // for conversion functions whose return type is cv T or reference to |
| 5778 | // cv T such that T is allowed by the context. There shall be |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5779 | // exactly one such T. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5780 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5781 | // If no unique T is found: |
| 5782 | if (ToType.isNull()) { |
| 5783 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5784 | HadMultipleCandidates, |
| 5785 | ExplicitConversions)) |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5786 | return ExprError(); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5787 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5788 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5789 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5790 | // If more than one unique Ts are found: |
| 5791 | if (!HasUniqueTargetType) |
| 5792 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5793 | ViableConversions); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5794 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5795 | // If one unique T is found: |
| 5796 | // First, build a candidate set from the previously recorded |
| 5797 | // potentially viable conversions. |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5798 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5799 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5800 | CandidateSet); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5801 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5802 | // Then, perform overload resolution over the candidate set. |
| 5803 | OverloadCandidateSet::iterator Best; |
| 5804 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5805 | case OR_Success: { |
| 5806 | // Apply this conversion. |
| 5807 | DeclAccessPair Found = |
| 5808 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5809 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5810 | HadMultipleCandidates, Found)) |
| 5811 | return ExprError(); |
| 5812 | break; |
| 5813 | } |
| 5814 | case OR_Ambiguous: |
| 5815 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5816 | ViableConversions); |
| 5817 | case OR_No_Viable_Function: |
| 5818 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5819 | HadMultipleCandidates, |
| 5820 | ExplicitConversions)) |
| 5821 | return ExprError(); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5822 | LLVM_FALLTHROUGH; |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5823 | case OR_Deleted: |
| 5824 | // We'll complain below about a non-integral condition type. |
| 5825 | break; |
| 5826 | } |
| 5827 | } else { |
| 5828 | switch (ViableConversions.size()) { |
| 5829 | case 0: { |
| 5830 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5831 | HadMultipleCandidates, |
| 5832 | ExplicitConversions)) |
| Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5833 | return ExprError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5834 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5835 | // We'll complain below about a non-integral condition type. |
| 5836 | break; |
| Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5837 | } |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5838 | case 1: { |
| 5839 | // Apply this conversion. |
| 5840 | DeclAccessPair Found = ViableConversions[0]; |
| 5841 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5842 | HadMultipleCandidates, Found)) |
| 5843 | return ExprError(); |
| 5844 | break; |
| 5845 | } |
| 5846 | default: |
| 5847 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5848 | ViableConversions); |
| 5849 | } |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5850 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5851 | |
| Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5852 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
| Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5853 | } |
| 5854 | |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5855 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 5856 | /// an acceptable non-member overloaded operator for a call whose |
| 5857 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 5858 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 5859 | /// enumeration types. |
| 5860 | static bool IsAcceptableNonMemberOperatorCandidate(ASTContext &Context, |
| 5861 | FunctionDecl *Fn, |
| 5862 | ArrayRef<Expr *> Args) { |
| 5863 | QualType T1 = Args[0]->getType(); |
| 5864 | QualType T2 = Args.size() > 1 ? Args[1]->getType() : QualType(); |
| 5865 | |
| 5866 | if (T1->isDependentType() || (!T2.isNull() && T2->isDependentType())) |
| 5867 | return true; |
| 5868 | |
| 5869 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 5870 | return true; |
| 5871 | |
| 5872 | const FunctionProtoType *Proto = Fn->getType()->getAs<FunctionProtoType>(); |
| 5873 | if (Proto->getNumParams() < 1) |
| 5874 | return false; |
| 5875 | |
| 5876 | if (T1->isEnumeralType()) { |
| 5877 | QualType ArgType = Proto->getParamType(0).getNonReferenceType(); |
| 5878 | if (Context.hasSameUnqualifiedType(T1, ArgType)) |
| 5879 | return true; |
| 5880 | } |
| 5881 | |
| 5882 | if (Proto->getNumParams() < 2) |
| 5883 | return false; |
| 5884 | |
| 5885 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 5886 | QualType ArgType = Proto->getParamType(1).getNonReferenceType(); |
| 5887 | if (Context.hasSameUnqualifiedType(T2, ArgType)) |
| 5888 | return true; |
| 5889 | } |
| 5890 | |
| 5891 | return false; |
| 5892 | } |
| 5893 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5894 | /// AddOverloadCandidate - Adds the given function to the set of |
| Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5895 | /// candidate functions, using the given function call arguments. If |
| 5896 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5897 | /// conversions via constructors or conversion operators. |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5898 | /// |
| James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5899 | /// \param PartialOverloading true if we are performing "partial" overloading |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5900 | /// based on an incomplete set of function arguments. This feature is used by |
| 5901 | /// code completion. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5902 | void |
| 5903 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5904 | DeclAccessPair FoundDecl, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5905 | ArrayRef<Expr *> Args, |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5906 | OverloadCandidateSet &CandidateSet, |
| Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5907 | bool SuppressUserConversions, |
| Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5908 | bool PartialOverloading, |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 5909 | bool AllowExplicit, |
| 5910 | ConversionSequenceList EarlyConversions) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5911 | const FunctionProtoType *Proto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5912 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5913 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5914 | assert(!Function->getDescribedFunctionTemplate() && |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5915 | "Use AddTemplateOverloadCandidate for function templates"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5916 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5917 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5918 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5919 | // If we get here, it's because we're calling a member function |
| 5920 | // that is named without a member access expression (e.g., |
| 5921 | // "this->f") that was either written explicitly or created |
| 5922 | // implicitly. This can happen with a qualified call to a member |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5923 | // function, e.g., X::f(). We use an empty type for the implied |
| 5924 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5925 | // is irrelevant. |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 5926 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), QualType(), |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 5927 | Expr::Classification::makeSimpleLValue(), Args, |
| 5928 | CandidateSet, SuppressUserConversions, |
| 5929 | PartialOverloading, EarlyConversions); |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5930 | return; |
| 5931 | } |
| 5932 | // We treat a constructor like a non-member function, since its object |
| 5933 | // argument doesn't participate in overload resolution. |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
| Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5936 | if (!CandidateSet.isNewCandidate(Function)) |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5937 | return; |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5938 | |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5939 | // C++ [over.match.oper]p3: |
| 5940 | // if no operand has a class type, only those non-member functions in the |
| 5941 | // lookup set that have a first parameter of type T1 or "reference to |
| 5942 | // (possibly cv-qualified) T1", when T1 is an enumeration type, or (if there |
| 5943 | // is a right operand) a second parameter of type T2 or "reference to |
| 5944 | // (possibly cv-qualified) T2", when T2 is an enumeration type, are |
| 5945 | // candidate functions. |
| 5946 | if (CandidateSet.getKind() == OverloadCandidateSet::CSK_Operator && |
| 5947 | !IsAcceptableNonMemberOperatorCandidate(Context, Function, Args)) |
| 5948 | return; |
| 5949 | |
| Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5950 | // C++11 [class.copy]p11: [DR1402] |
| 5951 | // A defaulted move constructor that is defined as deleted is ignored by |
| 5952 | // overload resolution. |
| 5953 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function); |
| 5954 | if (Constructor && Constructor->isDefaulted() && Constructor->isDeleted() && |
| 5955 | Constructor->isMoveConstructor()) |
| 5956 | return; |
| 5957 | |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5958 | // Overload resolution is always an unevaluated context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 5959 | EnterExpressionEvaluationContext Unevaluated( |
| 5960 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5961 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5962 | // Add this candidate |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 5963 | OverloadCandidate &Candidate = |
| 5964 | CandidateSet.addCandidate(Args.size(), EarlyConversions); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5965 | Candidate.FoundDecl = FoundDecl; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5966 | Candidate.Function = Function; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5967 | Candidate.Viable = true; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5968 | Candidate.IsSurrogate = false; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5969 | Candidate.IgnoreObjectArgument = false; |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5970 | Candidate.ExplicitCallArguments = Args.size(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5971 | |
| Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 5972 | if (Function->isMultiVersion() && |
| 5973 | !Function->getAttr<TargetAttr>()->isDefaultVersion()) { |
| 5974 | Candidate.Viable = false; |
| 5975 | Candidate.FailureKind = ovl_non_default_multiversion_function; |
| 5976 | return; |
| 5977 | } |
| 5978 | |
| John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 5979 | if (Constructor) { |
| 5980 | // C++ [class.copy]p3: |
| 5981 | // A member function template is never instantiated to perform the copy |
| 5982 | // of a class object to an object of its class type. |
| 5983 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5984 | if (Args.size() == 1 && Constructor->isSpecializationCopyingObject() && |
| John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 5985 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 5986 | IsDerivedFrom(Args[0]->getLocStart(), Args[0]->getType(), |
| 5987 | ClassType))) { |
| John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 5988 | Candidate.Viable = false; |
| 5989 | Candidate.FailureKind = ovl_fail_illegal_constructor; |
| 5990 | return; |
| 5991 | } |
| Richard Smith | 836a3b4 | 2017-01-13 20:46:54 +0000 | [diff] [blame] | 5992 | |
| 5993 | // C++ [over.match.funcs]p8: (proposed DR resolution) |
| 5994 | // A constructor inherited from class type C that has a first parameter |
| 5995 | // of type "reference to P" (including such a constructor instantiated |
| 5996 | // from a template) is excluded from the set of candidate functions when |
| 5997 | // constructing an object of type cv D if the argument list has exactly |
| 5998 | // one argument and D is reference-related to P and P is reference-related |
| 5999 | // to C. |
| 6000 | auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl.getDecl()); |
| 6001 | if (Shadow && Args.size() == 1 && Constructor->getNumParams() >= 1 && |
| 6002 | Constructor->getParamDecl(0)->getType()->isReferenceType()) { |
| 6003 | QualType P = Constructor->getParamDecl(0)->getType()->getPointeeType(); |
| 6004 | QualType C = Context.getRecordType(Constructor->getParent()); |
| 6005 | QualType D = Context.getRecordType(Shadow->getParent()); |
| 6006 | SourceLocation Loc = Args.front()->getExprLoc(); |
| 6007 | if ((Context.hasSameUnqualifiedType(P, C) || IsDerivedFrom(Loc, P, C)) && |
| 6008 | (Context.hasSameUnqualifiedType(D, P) || IsDerivedFrom(Loc, D, P))) { |
| 6009 | Candidate.Viable = false; |
| 6010 | Candidate.FailureKind = ovl_fail_inhctor_slice; |
| 6011 | return; |
| 6012 | } |
| 6013 | } |
| John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 6016 | unsigned NumParams = Proto->getNumParams(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6017 | |
| 6018 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6019 | // parameters is viable only if it has an ellipsis in its parameter |
| 6020 | // list (8.3.5). |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6021 | if (TooManyArguments(NumParams, Args.size(), PartialOverloading) && |
| Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 6022 | !Proto->isVariadic()) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6023 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6024 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6025 | return; |
| 6026 | } |
| 6027 | |
| 6028 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 6029 | // is viable only if the (m+1)st parameter has a default argument |
| 6030 | // (8.3.6). For the purposes of overload resolution, the |
| 6031 | // parameter list is truncated on the right, so that there are |
| 6032 | // exactly m parameters. |
| 6033 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6034 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6035 | // Not enough arguments. |
| 6036 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6037 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6038 | return; |
| 6039 | } |
| 6040 | |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 6041 | // (CUDA B.1): Check for invalid calls between targets. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6042 | if (getLangOpts().CUDA) |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 6043 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 6044 | // Skip the check for callers that are implicit members, because in this |
| 6045 | // case we may not yet know what the member's target is; the target is |
| 6046 | // inferred for the member automatically, based on the bases and fields of |
| 6047 | // the class. |
| Justin Lebar | b008003 | 2016-08-10 01:09:11 +0000 | [diff] [blame] | 6048 | if (!Caller->isImplicit() && !IsAllowedCUDACall(Caller, Function)) { |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 6049 | Candidate.Viable = false; |
| 6050 | Candidate.FailureKind = ovl_fail_bad_target; |
| 6051 | return; |
| 6052 | } |
| 6053 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6054 | // Determine the implicit conversion sequences for each of the |
| 6055 | // arguments. |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6056 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6057 | if (Candidate.Conversions[ArgIdx].isInitialized()) { |
| 6058 | // We already formed a conversion sequence for this parameter during |
| 6059 | // template argument deduction. |
| 6060 | } else if (ArgIdx < NumParams) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6061 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6062 | // exist for each argument an implicit conversion sequence |
| 6063 | // (13.3.3.1) that converts that argument to the corresponding |
| 6064 | // parameter of F. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6065 | QualType ParamType = Proto->getParamType(ArgIdx); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6066 | Candidate.Conversions[ArgIdx] |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6067 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6068 | SuppressUserConversions, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6069 | /*InOverloadResolution=*/true, |
| 6070 | /*AllowObjCWritebackConversion=*/ |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6071 | getLangOpts().ObjCAutoRefCount, |
| Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6072 | AllowExplicit); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6073 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 6074 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6075 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6076 | return; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6077 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6078 | } else { |
| 6079 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6080 | // argument for which there is no corresponding parameter is |
| 6081 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6082 | Candidate.Conversions[ArgIdx].setEllipsis(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6083 | } |
| 6084 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6085 | |
| 6086 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Function, Args)) { |
| 6087 | Candidate.Viable = false; |
| 6088 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6089 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6090 | return; |
| 6091 | } |
| Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 6092 | |
| 6093 | if (LangOpts.OpenCL && isOpenCLDisabledDecl(Function)) { |
| 6094 | Candidate.Viable = false; |
| 6095 | Candidate.FailureKind = ovl_fail_ext_disabled; |
| 6096 | return; |
| 6097 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 6100 | ObjCMethodDecl * |
| 6101 | Sema::SelectBestMethod(Selector Sel, MultiExprArg Args, bool IsInstance, |
| 6102 | SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
| 6103 | if (Methods.size() <= 1) |
| Fariborz Jahanian | 0ded424 | 2014-08-13 21:24:14 +0000 | [diff] [blame] | 6104 | return nullptr; |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 6105 | |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6106 | for (unsigned b = 0, e = Methods.size(); b < e; b++) { |
| 6107 | bool Match = true; |
| 6108 | ObjCMethodDecl *Method = Methods[b]; |
| 6109 | unsigned NumNamedArgs = Sel.getNumArgs(); |
| 6110 | // Method might have more arguments than selector indicates. This is due |
| 6111 | // to addition of c-style arguments in method. |
| 6112 | if (Method->param_size() > NumNamedArgs) |
| 6113 | NumNamedArgs = Method->param_size(); |
| 6114 | if (Args.size() < NumNamedArgs) |
| 6115 | continue; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 6116 | |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6117 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
| 6118 | // We can't do any type-checking on a type-dependent argument. |
| 6119 | if (Args[i]->isTypeDependent()) { |
| 6120 | Match = false; |
| 6121 | break; |
| 6122 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 6123 | |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6124 | ParmVarDecl *param = Method->parameters()[i]; |
| 6125 | Expr *argExpr = Args[i]; |
| 6126 | assert(argExpr && "SelectBestMethod(): missing expression"); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 6127 | |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6128 | // Strip the unbridged-cast placeholder expression off unless it's |
| 6129 | // a consumed argument. |
| 6130 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
| 6131 | !param->hasAttr<CFConsumedAttr>()) |
| 6132 | argExpr = stripARCUnbridgedCast(argExpr); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 6133 | |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6134 | // If the parameter is __unknown_anytype, move on to the next method. |
| 6135 | if (param->getType() == Context.UnknownAnyTy) { |
| 6136 | Match = false; |
| 6137 | break; |
| 6138 | } |
| George Burgess IV | 4546181 | 2015-10-11 20:13:20 +0000 | [diff] [blame] | 6139 | |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6140 | ImplicitConversionSequence ConversionState |
| 6141 | = TryCopyInitialization(*this, argExpr, param->getType(), |
| 6142 | /*SuppressUserConversions*/false, |
| 6143 | /*InOverloadResolution=*/true, |
| 6144 | /*AllowObjCWritebackConversion=*/ |
| 6145 | getLangOpts().ObjCAutoRefCount, |
| 6146 | /*AllowExplicit*/false); |
| George Burgess IV | 2099b54 | 2016-09-02 22:59:57 +0000 | [diff] [blame] | 6147 | // This function looks for a reasonably-exact match, so we consider |
| 6148 | // incompatible pointer conversions to be a failure here. |
| 6149 | if (ConversionState.isBad() || |
| 6150 | (ConversionState.isStandard() && |
| 6151 | ConversionState.Standard.Second == |
| 6152 | ICK_Incompatible_Pointer_Conversion)) { |
| 6153 | Match = false; |
| 6154 | break; |
| 6155 | } |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6156 | } |
| 6157 | // Promote additional arguments to variadic methods. |
| 6158 | if (Match && Method->isVariadic()) { |
| 6159 | for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) { |
| 6160 | if (Args[i]->isTypeDependent()) { |
| 6161 | Match = false; |
| 6162 | break; |
| 6163 | } |
| 6164 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
| 6165 | nullptr); |
| 6166 | if (Arg.isInvalid()) { |
| 6167 | Match = false; |
| 6168 | break; |
| 6169 | } |
| 6170 | } |
| Fariborz Jahanian | 180d76b | 2014-08-27 16:38:47 +0000 | [diff] [blame] | 6171 | } else { |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6172 | // Check for extra arguments to non-variadic methods. |
| 6173 | if (Args.size() != NumNamedArgs) |
| 6174 | Match = false; |
| Fariborz Jahanian | 180d76b | 2014-08-27 16:38:47 +0000 | [diff] [blame] | 6175 | else if (Match && NumNamedArgs == 0 && Methods.size() > 1) { |
| 6176 | // Special case when selectors have no argument. In this case, select |
| 6177 | // one with the most general result type of 'id'. |
| 6178 | for (unsigned b = 0, e = Methods.size(); b < e; b++) { |
| 6179 | QualType ReturnT = Methods[b]->getReturnType(); |
| 6180 | if (ReturnT->isObjCIdType()) |
| 6181 | return Methods[b]; |
| 6182 | } |
| 6183 | } |
| 6184 | } |
| Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 6185 | |
| 6186 | if (Match) |
| 6187 | return Method; |
| 6188 | } |
| 6189 | return nullptr; |
| 6190 | } |
| 6191 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6192 | static bool |
| 6193 | convertArgsForAvailabilityChecks(Sema &S, FunctionDecl *Function, Expr *ThisArg, |
| 6194 | ArrayRef<Expr *> Args, Sema::SFINAETrap &Trap, |
| 6195 | bool MissingImplicitThis, Expr *&ConvertedThis, |
| 6196 | SmallVectorImpl<Expr *> &ConvertedArgs) { |
| 6197 | if (ThisArg) { |
| 6198 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Function); |
| 6199 | assert(!isa<CXXConstructorDecl>(Method) && |
| 6200 | "Shouldn't have `this` for ctors!"); |
| 6201 | assert(!Method->isStatic() && "Shouldn't have `this` for static methods!"); |
| 6202 | ExprResult R = S.PerformObjectArgumentInitialization( |
| 6203 | ThisArg, /*Qualifier=*/nullptr, Method, Method); |
| 6204 | if (R.isInvalid()) |
| 6205 | return false; |
| 6206 | ConvertedThis = R.get(); |
| 6207 | } else { |
| 6208 | if (auto *MD = dyn_cast<CXXMethodDecl>(Function)) { |
| 6209 | (void)MD; |
| 6210 | assert((MissingImplicitThis || MD->isStatic() || |
| 6211 | isa<CXXConstructorDecl>(MD)) && |
| 6212 | "Expected `this` for non-ctor instance methods"); |
| 6213 | } |
| 6214 | ConvertedThis = nullptr; |
| 6215 | } |
| Nick Lewycky | e283c55 | 2015-08-25 22:33:16 +0000 | [diff] [blame] | 6216 | |
| George Burgess IV | 458b3f3 | 2016-08-12 04:19:35 +0000 | [diff] [blame] | 6217 | // Ignore any variadic arguments. Converting them is pointless, since the |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6218 | // user can't refer to them in the function condition. |
| George Burgess IV | 53b938d | 2016-08-12 04:12:31 +0000 | [diff] [blame] | 6219 | unsigned ArgSizeNoVarargs = std::min(Function->param_size(), Args.size()); |
| 6220 | |
| Nick Lewycky | e283c55 | 2015-08-25 22:33:16 +0000 | [diff] [blame] | 6221 | // Convert the arguments. |
| George Burgess IV | 53b938d | 2016-08-12 04:12:31 +0000 | [diff] [blame] | 6222 | for (unsigned I = 0; I != ArgSizeNoVarargs; ++I) { |
| George Burgess IV | e96abf7 | 2016-02-24 22:31:14 +0000 | [diff] [blame] | 6223 | ExprResult R; |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6224 | R = S.PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6225 | S.Context, Function->getParamDecl(I)), |
| George Burgess IV | e96abf7 | 2016-02-24 22:31:14 +0000 | [diff] [blame] | 6226 | SourceLocation(), Args[I]); |
| George Burgess IV | e96abf7 | 2016-02-24 22:31:14 +0000 | [diff] [blame] | 6227 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6228 | if (R.isInvalid()) |
| 6229 | return false; |
| George Burgess IV | e96abf7 | 2016-02-24 22:31:14 +0000 | [diff] [blame] | 6230 | |
| George Burgess IV | e96abf7 | 2016-02-24 22:31:14 +0000 | [diff] [blame] | 6231 | ConvertedArgs.push_back(R.get()); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6234 | if (Trap.hasErrorOccurred()) |
| 6235 | return false; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6236 | |
| Nick Lewycky | e283c55 | 2015-08-25 22:33:16 +0000 | [diff] [blame] | 6237 | // Push default arguments if needed. |
| 6238 | if (!Function->isVariadic() && Args.size() < Function->getNumParams()) { |
| 6239 | for (unsigned i = Args.size(), e = Function->getNumParams(); i != e; ++i) { |
| 6240 | ParmVarDecl *P = Function->getParamDecl(i); |
| Ilya Biryukov | 1a1dffd | 2018-03-09 14:43:29 +0000 | [diff] [blame] | 6241 | Expr *DefArg = P->hasUninstantiatedDefaultArg() |
| 6242 | ? P->getUninstantiatedDefaultArg() |
| 6243 | : P->getDefaultArg(); |
| 6244 | // This can only happen in code completion, i.e. when PartialOverloading |
| 6245 | // is true. |
| 6246 | if (!DefArg) |
| 6247 | return false; |
| 6248 | ExprResult R = |
| 6249 | S.PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6250 | S.Context, Function->getParamDecl(i)), |
| 6251 | SourceLocation(), DefArg); |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6252 | if (R.isInvalid()) |
| 6253 | return false; |
| Nick Lewycky | e283c55 | 2015-08-25 22:33:16 +0000 | [diff] [blame] | 6254 | ConvertedArgs.push_back(R.get()); |
| 6255 | } |
| 6256 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6257 | if (Trap.hasErrorOccurred()) |
| 6258 | return false; |
| Nick Lewycky | e283c55 | 2015-08-25 22:33:16 +0000 | [diff] [blame] | 6259 | } |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6260 | return true; |
| 6261 | } |
| 6262 | |
| 6263 | EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, |
| 6264 | bool MissingImplicitThis) { |
| Michael Kruse | ea31f0e | 2018-06-19 23:46:52 +0000 | [diff] [blame] | 6265 | auto EnableIfAttrs = Function->specific_attrs<EnableIfAttr>(); |
| 6266 | |
| 6267 | if (EnableIfAttrs.begin() == EnableIfAttrs.end()) |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6268 | return nullptr; |
| 6269 | |
| 6270 | SFINAETrap Trap(*this); |
| 6271 | SmallVector<Expr *, 16> ConvertedArgs; |
| 6272 | // FIXME: We should look into making enable_if late-parsed. |
| 6273 | Expr *DiscardedThis; |
| 6274 | if (!convertArgsForAvailabilityChecks( |
| 6275 | *this, Function, /*ThisArg=*/nullptr, Args, Trap, |
| 6276 | /*MissingImplicitThis=*/true, DiscardedThis, ConvertedArgs)) |
| Michael Kruse | ea31f0e | 2018-06-19 23:46:52 +0000 | [diff] [blame] | 6277 | return *EnableIfAttrs.begin(); |
| Nick Lewycky | e283c55 | 2015-08-25 22:33:16 +0000 | [diff] [blame] | 6278 | |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 6279 | for (auto *EIA : EnableIfAttrs) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6280 | APValue Result; |
| George Burgess IV | e8f10cc | 2016-05-11 01:38:27 +0000 | [diff] [blame] | 6281 | // FIXME: This doesn't consider value-dependent cases, because doing so is |
| 6282 | // very difficult. Ideally, we should handle them more gracefully. |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6283 | if (!EIA->getCond()->EvaluateWithSubstitution( |
| George Burgess IV | e8f10cc | 2016-05-11 01:38:27 +0000 | [diff] [blame] | 6284 | Result, Context, Function, llvm::makeArrayRef(ConvertedArgs))) |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6285 | return EIA; |
| George Burgess IV | e8f10cc | 2016-05-11 01:38:27 +0000 | [diff] [blame] | 6286 | |
| 6287 | if (!Result.isInt() || !Result.getInt().getBoolValue()) |
| 6288 | return EIA; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6289 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6290 | return nullptr; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6293 | template <typename CheckFn> |
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 6294 | static bool diagnoseDiagnoseIfAttrsWith(Sema &S, const NamedDecl *ND, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6295 | bool ArgDependent, SourceLocation Loc, |
| 6296 | CheckFn &&IsSuccessful) { |
| 6297 | SmallVector<const DiagnoseIfAttr *, 8> Attrs; |
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 6298 | for (const auto *DIA : ND->specific_attrs<DiagnoseIfAttr>()) { |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6299 | if (ArgDependent == DIA->getArgDependent()) |
| 6300 | Attrs.push_back(DIA); |
| 6301 | } |
| 6302 | |
| 6303 | // Common case: No diagnose_if attributes, so we can quit early. |
| 6304 | if (Attrs.empty()) |
| 6305 | return false; |
| 6306 | |
| 6307 | auto WarningBegin = std::stable_partition( |
| 6308 | Attrs.begin(), Attrs.end(), |
| 6309 | [](const DiagnoseIfAttr *DIA) { return DIA->isError(); }); |
| 6310 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6311 | // Note that diagnose_if attributes are late-parsed, so they appear in the |
| 6312 | // correct order (unlike enable_if attributes). |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6313 | auto ErrAttr = llvm::find_if(llvm::make_range(Attrs.begin(), WarningBegin), |
| 6314 | IsSuccessful); |
| 6315 | if (ErrAttr != WarningBegin) { |
| 6316 | const DiagnoseIfAttr *DIA = *ErrAttr; |
| 6317 | S.Diag(Loc, diag::err_diagnose_if_succeeded) << DIA->getMessage(); |
| 6318 | S.Diag(DIA->getLocation(), diag::note_from_diagnose_if) |
| 6319 | << DIA->getParent() << DIA->getCond()->getSourceRange(); |
| 6320 | return true; |
| 6321 | } |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6322 | |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6323 | for (const auto *DIA : llvm::make_range(WarningBegin, Attrs.end())) |
| 6324 | if (IsSuccessful(DIA)) { |
| 6325 | S.Diag(Loc, diag::warn_diagnose_if_succeeded) << DIA->getMessage(); |
| 6326 | S.Diag(DIA->getLocation(), diag::note_from_diagnose_if) |
| 6327 | << DIA->getParent() << DIA->getCond()->getSourceRange(); |
| 6328 | } |
| 6329 | |
| 6330 | return false; |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6333 | bool Sema::diagnoseArgDependentDiagnoseIfAttrs(const FunctionDecl *Function, |
| 6334 | const Expr *ThisArg, |
| 6335 | ArrayRef<const Expr *> Args, |
| 6336 | SourceLocation Loc) { |
| 6337 | return diagnoseDiagnoseIfAttrsWith( |
| 6338 | *this, Function, /*ArgDependent=*/true, Loc, |
| 6339 | [&](const DiagnoseIfAttr *DIA) { |
| 6340 | APValue Result; |
| 6341 | // It's sane to use the same Args for any redecl of this function, since |
| 6342 | // EvaluateWithSubstitution only cares about the position of each |
| 6343 | // argument in the arg list, not the ParmVarDecl* it maps to. |
| 6344 | if (!DIA->getCond()->EvaluateWithSubstitution( |
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 6345 | Result, Context, cast<FunctionDecl>(DIA->getParent()), Args, ThisArg)) |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6346 | return false; |
| 6347 | return Result.isInt() && Result.getInt().getBoolValue(); |
| 6348 | }); |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 6351 | bool Sema::diagnoseArgIndependentDiagnoseIfAttrs(const NamedDecl *ND, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6352 | SourceLocation Loc) { |
| 6353 | return diagnoseDiagnoseIfAttrsWith( |
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 6354 | *this, ND, /*ArgDependent=*/false, Loc, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6355 | [&](const DiagnoseIfAttr *DIA) { |
| 6356 | bool Result; |
| 6357 | return DIA->getCond()->EvaluateAsBooleanCondition(Result, Context) && |
| 6358 | Result; |
| 6359 | }); |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6362 | /// Add all of the function declarations in the given function set to |
| Nick Lewycky | ed4265c | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 6363 | /// the overload candidate set. |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6364 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6365 | ArrayRef<Expr *> Args, |
| Ivan Donchevskii | f83cfda | 2018-06-21 08:34:50 +0000 | [diff] [blame] | 6366 | OverloadCandidateSet &CandidateSet, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6367 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 6368 | bool SuppressUserConversions, |
| Benjamin Kramer | e3962ae | 2017-10-26 08:41:28 +0000 | [diff] [blame] | 6369 | bool PartialOverloading, |
| 6370 | bool FirstArgumentIsBase) { |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6371 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6372 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| Ivan Donchevskii | f83cfda | 2018-06-21 08:34:50 +0000 | [diff] [blame] | 6373 | ArrayRef<Expr *> FunctionArgs = Args; |
| 6374 | |
| 6375 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 6376 | FunctionDecl *FD = |
| 6377 | FunTmpl ? FunTmpl->getTemplatedDecl() : cast<FunctionDecl>(D); |
| 6378 | |
| 6379 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) { |
| 6380 | QualType ObjectType; |
| 6381 | Expr::Classification ObjectClassification; |
| 6382 | if (Args.size() > 0) { |
| Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 6383 | if (Expr *E = Args[0]) { |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6384 | // Use the explicit base to restrict the lookup: |
| Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 6385 | ObjectType = E->getType(); |
| 6386 | ObjectClassification = E->Classify(Context); |
| Ivan Donchevskii | f83cfda | 2018-06-21 08:34:50 +0000 | [diff] [blame] | 6387 | } // .. else there is an implicit base. |
| 6388 | FunctionArgs = Args.slice(1); |
| 6389 | } |
| 6390 | if (FunTmpl) { |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6391 | AddMethodTemplateCandidate( |
| 6392 | FunTmpl, F.getPair(), |
| 6393 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
| Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 6394 | ExplicitTemplateArgs, ObjectType, ObjectClassification, |
| Ivan Donchevskii | f83cfda | 2018-06-21 08:34:50 +0000 | [diff] [blame] | 6395 | FunctionArgs, CandidateSet, SuppressUserConversions, |
| Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 6396 | PartialOverloading); |
| 6397 | } else { |
| Ivan Donchevskii | f83cfda | 2018-06-21 08:34:50 +0000 | [diff] [blame] | 6398 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
| 6399 | cast<CXXMethodDecl>(FD)->getParent(), ObjectType, |
| 6400 | ObjectClassification, FunctionArgs, CandidateSet, |
| 6401 | SuppressUserConversions, PartialOverloading); |
| 6402 | } |
| 6403 | } else { |
| 6404 | // This branch handles both standalone functions and static methods. |
| 6405 | |
| 6406 | // Slice the first argument (which is the base) when we access |
| 6407 | // static method as non-static. |
| 6408 | if (Args.size() > 0 && |
| 6409 | (!Args[0] || (FirstArgumentIsBase && isa<CXXMethodDecl>(FD) && |
| 6410 | !isa<CXXConstructorDecl>(FD)))) { |
| 6411 | assert(cast<CXXMethodDecl>(FD)->isStatic()); |
| 6412 | FunctionArgs = Args.slice(1); |
| 6413 | } |
| 6414 | if (FunTmpl) { |
| 6415 | AddTemplateOverloadCandidate( |
| 6416 | FunTmpl, F.getPair(), ExplicitTemplateArgs, FunctionArgs, |
| 6417 | CandidateSet, SuppressUserConversions, PartialOverloading); |
| 6418 | } else { |
| 6419 | AddOverloadCandidate(FD, F.getPair(), FunctionArgs, CandidateSet, |
| 6420 | SuppressUserConversions, PartialOverloading); |
| Erik Verbruggen | f1898cf | 2017-03-28 07:22:21 +0000 | [diff] [blame] | 6421 | } |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6422 | } |
| Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6423 | } |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6426 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 6427 | /// method) as a method candidate to the given overload set. |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6428 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6429 | QualType ObjectType, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6430 | Expr::Classification ObjectClassification, |
| Rafael Espindola | 51629df | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6431 | ArrayRef<Expr *> Args, |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6432 | OverloadCandidateSet& CandidateSet, |
| Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 6433 | bool SuppressUserConversions) { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6434 | NamedDecl *Decl = FoundDecl.getDecl(); |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6435 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6436 | |
| 6437 | if (isa<UsingShadowDecl>(Decl)) |
| 6438 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6439 | |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6440 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 6441 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 6442 | "Expected a member function template"); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6443 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6444 | /*ExplicitArgs*/ nullptr, ObjectType, |
| 6445 | ObjectClassification, Args, CandidateSet, |
| Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 6446 | SuppressUserConversions); |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6447 | } else { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6448 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6449 | ObjectType, ObjectClassification, Args, CandidateSet, |
| 6450 | SuppressUserConversions); |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6451 | } |
| 6452 | } |
| 6453 | |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6454 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 6455 | /// of candidate functions, using the given function call arguments |
| 6456 | /// and the object argument (@c Object). For example, in a call |
| 6457 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 6458 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 6459 | /// allow user-defined conversions via constructors or conversion |
| Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 6460 | /// operators. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6461 | void |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6462 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
| John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6463 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6464 | Expr::Classification ObjectClassification, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6465 | ArrayRef<Expr *> Args, |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6466 | OverloadCandidateSet &CandidateSet, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6467 | bool SuppressUserConversions, |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6468 | bool PartialOverloading, |
| 6469 | ConversionSequenceList EarlyConversions) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6470 | const FunctionProtoType *Proto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6471 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6472 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6473 | assert(!isa<CXXConstructorDecl>(Method) && |
| 6474 | "Use AddOverloadCandidate for constructors"); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6475 | |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6476 | if (!CandidateSet.isNewCandidate(Method)) |
| 6477 | return; |
| 6478 | |
| Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 6479 | // C++11 [class.copy]p23: [DR1402] |
| 6480 | // A defaulted move assignment operator that is defined as deleted is |
| 6481 | // ignored by overload resolution. |
| 6482 | if (Method->isDefaulted() && Method->isDeleted() && |
| 6483 | Method->isMoveAssignmentOperator()) |
| 6484 | return; |
| 6485 | |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6486 | // Overload resolution is always an unevaluated context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6487 | EnterExpressionEvaluationContext Unevaluated( |
| 6488 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6489 | |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6490 | // Add this candidate |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6491 | OverloadCandidate &Candidate = |
| 6492 | CandidateSet.addCandidate(Args.size() + 1, EarlyConversions); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6493 | Candidate.FoundDecl = FoundDecl; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6494 | Candidate.Function = Method; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6495 | Candidate.IsSurrogate = false; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6496 | Candidate.IgnoreObjectArgument = false; |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6497 | Candidate.ExplicitCallArguments = Args.size(); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6498 | |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 6499 | unsigned NumParams = Proto->getNumParams(); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6500 | |
| 6501 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6502 | // parameters is viable only if it has an ellipsis in its parameter |
| 6503 | // list (8.3.5). |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6504 | if (TooManyArguments(NumParams, Args.size(), PartialOverloading) && |
| 6505 | !Proto->isVariadic()) { |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6506 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6507 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6508 | return; |
| 6509 | } |
| 6510 | |
| 6511 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 6512 | // is viable only if the (m+1)st parameter has a default argument |
| 6513 | // (8.3.6). For the purposes of overload resolution, the |
| 6514 | // parameter list is truncated on the right, so that there are |
| 6515 | // exactly m parameters. |
| 6516 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6517 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6518 | // Not enough arguments. |
| 6519 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6520 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6521 | return; |
| 6522 | } |
| 6523 | |
| 6524 | Candidate.Viable = true; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6525 | |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6526 | if (Method->isStatic() || ObjectType.isNull()) |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6527 | // The implicit object argument is ignored. |
| 6528 | Candidate.IgnoreObjectArgument = true; |
| 6529 | else { |
| 6530 | // Determine the implicit conversion sequence for the object |
| 6531 | // parameter. |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 6532 | Candidate.Conversions[0] = TryObjectArgumentInitialization( |
| 6533 | *this, CandidateSet.getLocation(), ObjectType, ObjectClassification, |
| 6534 | Method, ActingContext); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6535 | if (Candidate.Conversions[0].isBad()) { |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6536 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6537 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6538 | return; |
| 6539 | } |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
| Eli Bendersky | 291a57e | 2014-09-25 23:59:08 +0000 | [diff] [blame] | 6542 | // (CUDA B.1): Check for invalid calls between targets. |
| 6543 | if (getLangOpts().CUDA) |
| 6544 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| Justin Lebar | b008003 | 2016-08-10 01:09:11 +0000 | [diff] [blame] | 6545 | if (!IsAllowedCUDACall(Caller, Method)) { |
| Eli Bendersky | 291a57e | 2014-09-25 23:59:08 +0000 | [diff] [blame] | 6546 | Candidate.Viable = false; |
| 6547 | Candidate.FailureKind = ovl_fail_bad_target; |
| 6548 | return; |
| 6549 | } |
| 6550 | |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6551 | // Determine the implicit conversion sequences for each of the |
| 6552 | // arguments. |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6553 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6554 | if (Candidate.Conversions[ArgIdx + 1].isInitialized()) { |
| 6555 | // We already formed a conversion sequence for this parameter during |
| 6556 | // template argument deduction. |
| 6557 | } else if (ArgIdx < NumParams) { |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6558 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6559 | // exist for each argument an implicit conversion sequence |
| 6560 | // (13.3.3.1) that converts that argument to the corresponding |
| 6561 | // parameter of F. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6562 | QualType ParamType = Proto->getParamType(ArgIdx); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6563 | Candidate.Conversions[ArgIdx + 1] |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6564 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6565 | SuppressUserConversions, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6566 | /*InOverloadResolution=*/true, |
| 6567 | /*AllowObjCWritebackConversion=*/ |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6568 | getLangOpts().ObjCAutoRefCount); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6569 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6570 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6571 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6572 | return; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6573 | } |
| 6574 | } else { |
| 6575 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6576 | // argument for which there is no corresponding parameter is |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6577 | // considered to "match the ellipsis" (C+ 13.3.3.1.3). |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6578 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6579 | } |
| 6580 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6581 | |
| 6582 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Method, Args, true)) { |
| 6583 | Candidate.Viable = false; |
| 6584 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6585 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6586 | return; |
| 6587 | } |
| Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 6588 | |
| 6589 | if (Method->isMultiVersion() && |
| 6590 | !Method->getAttr<TargetAttr>()->isDefaultVersion()) { |
| 6591 | Candidate.Viable = false; |
| 6592 | Candidate.FailureKind = ovl_non_default_multiversion_function; |
| 6593 | } |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6594 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6595 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6596 | /// Add a C++ member function template as a candidate to the candidate |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6597 | /// set, using template argument deduction to produce an appropriate member |
| 6598 | /// function template specialization. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6599 | void |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6600 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6601 | DeclAccessPair FoundDecl, |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6602 | CXXRecordDecl *ActingContext, |
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6603 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6604 | QualType ObjectType, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6605 | Expr::Classification ObjectClassification, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6606 | ArrayRef<Expr *> Args, |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6607 | OverloadCandidateSet& CandidateSet, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6608 | bool SuppressUserConversions, |
| 6609 | bool PartialOverloading) { |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6610 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 6611 | return; |
| 6612 | |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6613 | // C++ [over.match.funcs]p7: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6614 | // In each case where a candidate is a function template, candidate |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6615 | // function template specializations are generated using template argument |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6616 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6617 | // candidate functions in the usual way.113) A given name can refer to one |
| 6618 | // or more function templates and also to a set of overloaded non-template |
| 6619 | // functions. In such a case, the candidate functions generated from each |
| 6620 | // function template are combined with the set of non-template candidate |
| 6621 | // functions. |
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6622 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6623 | FunctionDecl *Specialization = nullptr; |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6624 | ConversionSequenceList Conversions; |
| 6625 | if (TemplateDeductionResult Result = DeduceTemplateArguments( |
| 6626 | MethodTmpl, ExplicitTemplateArgs, Args, Specialization, Info, |
| 6627 | PartialOverloading, [&](ArrayRef<QualType> ParamTypes) { |
| 6628 | return CheckNonDependentConversions( |
| 6629 | MethodTmpl, ParamTypes, Args, CandidateSet, Conversions, |
| 6630 | SuppressUserConversions, ActingContext, ObjectType, |
| 6631 | ObjectClassification); |
| 6632 | })) { |
| 6633 | OverloadCandidate &Candidate = |
| 6634 | CandidateSet.addCandidate(Conversions.size(), Conversions); |
| Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6635 | Candidate.FoundDecl = FoundDecl; |
| 6636 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 6637 | Candidate.Viable = false; |
| Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6638 | Candidate.IsSurrogate = false; |
| Richard Smith | 9d05e15 | 2017-01-10 20:52:50 +0000 | [diff] [blame] | 6639 | Candidate.IgnoreObjectArgument = |
| 6640 | cast<CXXMethodDecl>(Candidate.Function)->isStatic() || |
| 6641 | ObjectType.isNull(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6642 | Candidate.ExplicitCallArguments = Args.size(); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6643 | if (Result == TDK_NonDependentConversionFailure) |
| 6644 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 6645 | else { |
| 6646 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6647 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 6648 | Info); |
| 6649 | } |
| Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6650 | return; |
| 6651 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6652 | |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6653 | // Add the function template specialization produced by template argument |
| 6654 | // deduction as a candidate. |
| 6655 | assert(Specialization && "Missing member function template specialization?"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6656 | assert(isa<CXXMethodDecl>(Specialization) && |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6657 | "Specialization is not a member function?"); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6658 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 6659 | ActingContext, ObjectType, ObjectClassification, Args, |
| 6660 | CandidateSet, SuppressUserConversions, PartialOverloading, |
| 6661 | Conversions); |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6664 | /// Add a C++ function template specialization as a candidate |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6665 | /// in the candidate set, using template argument deduction to produce |
| 6666 | /// an appropriate function template specialization. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6667 | void |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6668 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6669 | DeclAccessPair FoundDecl, |
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6670 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6671 | ArrayRef<Expr *> Args, |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6672 | OverloadCandidateSet& CandidateSet, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 6673 | bool SuppressUserConversions, |
| 6674 | bool PartialOverloading) { |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6675 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 6676 | return; |
| 6677 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6678 | // C++ [over.match.funcs]p7: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6679 | // In each case where a candidate is a function template, candidate |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6680 | // function template specializations are generated using template argument |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6681 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6682 | // candidate functions in the usual way.113) A given name can refer to one |
| 6683 | // or more function templates and also to a set of overloaded non-template |
| 6684 | // functions. In such a case, the candidate functions generated from each |
| 6685 | // function template are combined with the set of non-template candidate |
| 6686 | // functions. |
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6687 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6688 | FunctionDecl *Specialization = nullptr; |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6689 | ConversionSequenceList Conversions; |
| 6690 | if (TemplateDeductionResult Result = DeduceTemplateArguments( |
| 6691 | FunctionTemplate, ExplicitTemplateArgs, Args, Specialization, Info, |
| 6692 | PartialOverloading, [&](ArrayRef<QualType> ParamTypes) { |
| 6693 | return CheckNonDependentConversions(FunctionTemplate, ParamTypes, |
| 6694 | Args, CandidateSet, Conversions, |
| 6695 | SuppressUserConversions); |
| 6696 | })) { |
| 6697 | OverloadCandidate &Candidate = |
| 6698 | CandidateSet.addCandidate(Conversions.size(), Conversions); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6699 | Candidate.FoundDecl = FoundDecl; |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6700 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 6701 | Candidate.Viable = false; |
| 6702 | Candidate.IsSurrogate = false; |
| Richard Smith | 9d05e15 | 2017-01-10 20:52:50 +0000 | [diff] [blame] | 6703 | // Ignore the object argument if there is one, since we don't have an object |
| 6704 | // type. |
| 6705 | Candidate.IgnoreObjectArgument = |
| 6706 | isa<CXXMethodDecl>(Candidate.Function) && |
| 6707 | !isa<CXXConstructorDecl>(Candidate.Function); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6708 | Candidate.ExplicitCallArguments = Args.size(); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6709 | if (Result == TDK_NonDependentConversionFailure) |
| 6710 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 6711 | else { |
| 6712 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6713 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 6714 | Info); |
| 6715 | } |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6716 | return; |
| 6717 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6718 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6719 | // Add the function template specialization produced by template argument |
| 6720 | // deduction as a candidate. |
| 6721 | assert(Specialization && "Missing function template specialization?"); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6722 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6723 | SuppressUserConversions, PartialOverloading, |
| 6724 | /*AllowExplicit*/false, Conversions); |
| 6725 | } |
| 6726 | |
| 6727 | /// Check that implicit conversion sequences can be formed for each argument |
| 6728 | /// whose corresponding parameter has a non-dependent type, per DR1391's |
| 6729 | /// [temp.deduct.call]p10. |
| 6730 | bool Sema::CheckNonDependentConversions( |
| 6731 | FunctionTemplateDecl *FunctionTemplate, ArrayRef<QualType> ParamTypes, |
| 6732 | ArrayRef<Expr *> Args, OverloadCandidateSet &CandidateSet, |
| 6733 | ConversionSequenceList &Conversions, bool SuppressUserConversions, |
| 6734 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 6735 | Expr::Classification ObjectClassification) { |
| 6736 | // FIXME: The cases in which we allow explicit conversions for constructor |
| 6737 | // arguments never consider calling a constructor template. It's not clear |
| 6738 | // that is correct. |
| 6739 | const bool AllowExplicit = false; |
| 6740 | |
| 6741 | auto *FD = FunctionTemplate->getTemplatedDecl(); |
| 6742 | auto *Method = dyn_cast<CXXMethodDecl>(FD); |
| 6743 | bool HasThisConversion = Method && !isa<CXXConstructorDecl>(Method); |
| 6744 | unsigned ThisConversions = HasThisConversion ? 1 : 0; |
| 6745 | |
| 6746 | Conversions = |
| 6747 | CandidateSet.allocateConversionSequences(ThisConversions + Args.size()); |
| 6748 | |
| 6749 | // Overload resolution is always an unevaluated context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6750 | EnterExpressionEvaluationContext Unevaluated( |
| 6751 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 6752 | |
| 6753 | // For a method call, check the 'this' conversion here too. DR1391 doesn't |
| 6754 | // require that, but this check should never result in a hard error, and |
| 6755 | // overload resolution is permitted to sidestep instantiations. |
| 6756 | if (HasThisConversion && !cast<CXXMethodDecl>(FD)->isStatic() && |
| 6757 | !ObjectType.isNull()) { |
| 6758 | Conversions[0] = TryObjectArgumentInitialization( |
| 6759 | *this, CandidateSet.getLocation(), ObjectType, ObjectClassification, |
| 6760 | Method, ActingContext); |
| 6761 | if (Conversions[0].isBad()) |
| 6762 | return true; |
| 6763 | } |
| 6764 | |
| 6765 | for (unsigned I = 0, N = std::min(ParamTypes.size(), Args.size()); I != N; |
| 6766 | ++I) { |
| 6767 | QualType ParamType = ParamTypes[I]; |
| 6768 | if (!ParamType->isDependentType()) { |
| 6769 | Conversions[ThisConversions + I] |
| 6770 | = TryCopyInitialization(*this, Args[I], ParamType, |
| 6771 | SuppressUserConversions, |
| 6772 | /*InOverloadResolution=*/true, |
| 6773 | /*AllowObjCWritebackConversion=*/ |
| 6774 | getLangOpts().ObjCAutoRefCount, |
| 6775 | AllowExplicit); |
| 6776 | if (Conversions[ThisConversions + I].isBad()) |
| 6777 | return true; |
| 6778 | } |
| 6779 | } |
| 6780 | |
| 6781 | return false; |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 6782 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6783 | |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6784 | /// Determine whether this is an allowable conversion from the result |
| 6785 | /// of an explicit conversion operator to the expected type, per C++ |
| 6786 | /// [over.match.conv]p1 and [over.match.ref]p1. |
| 6787 | /// |
| 6788 | /// \param ConvType The return type of the conversion function. |
| 6789 | /// |
| 6790 | /// \param ToType The type we are converting to. |
| 6791 | /// |
| 6792 | /// \param AllowObjCPointerConversion Allow a conversion from one |
| 6793 | /// Objective-C pointer to another. |
| 6794 | /// |
| 6795 | /// \returns true if the conversion is allowable, false otherwise. |
| 6796 | static bool isAllowableExplicitConversion(Sema &S, |
| 6797 | QualType ConvType, QualType ToType, |
| 6798 | bool AllowObjCPointerConversion) { |
| 6799 | QualType ToNonRefType = ToType.getNonReferenceType(); |
| 6800 | |
| 6801 | // Easy case: the types are the same. |
| 6802 | if (S.Context.hasSameUnqualifiedType(ConvType, ToNonRefType)) |
| 6803 | return true; |
| 6804 | |
| 6805 | // Allow qualification conversions. |
| 6806 | bool ObjCLifetimeConversion; |
| 6807 | if (S.IsQualificationConversion(ConvType, ToNonRefType, /*CStyle*/false, |
| 6808 | ObjCLifetimeConversion)) |
| 6809 | return true; |
| 6810 | |
| 6811 | // If we're not allowed to consider Objective-C pointer conversions, |
| 6812 | // we're done. |
| 6813 | if (!AllowObjCPointerConversion) |
| 6814 | return false; |
| 6815 | |
| 6816 | // Is this an Objective-C pointer conversion? |
| 6817 | bool IncompatibleObjC = false; |
| 6818 | QualType ConvertedType; |
| 6819 | return S.isObjCPointerConversion(ConvType, ToNonRefType, ConvertedType, |
| 6820 | IncompatibleObjC); |
| 6821 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 6822 | |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6823 | /// AddConversionCandidate - Add a C++ conversion function as a |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6824 | /// candidate in the candidate set (C++ [over.match.conv], |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6825 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6826 | /// and ToType is the type that we're eventually trying to convert to |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6827 | /// (which may or may not be the same type as the type that the |
| 6828 | /// conversion function produces). |
| 6829 | void |
| 6830 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6831 | DeclAccessPair FoundDecl, |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6832 | CXXRecordDecl *ActingContext, |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6833 | Expr *From, QualType ToType, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6834 | OverloadCandidateSet& CandidateSet, |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6835 | bool AllowObjCConversionOnExplicit, |
| 6836 | bool AllowResultConversion) { |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6837 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 6838 | "Conversion function templates use AddTemplateConversionCandidate"); |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6839 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6840 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6841 | return; |
| 6842 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6843 | // If the conversion function has an undeduced return type, trigger its |
| 6844 | // deduction now. |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 6845 | if (getLangOpts().CPlusPlus14 && ConvType->isUndeducedType()) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6846 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 6847 | return; |
| 6848 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 6849 | } |
| 6850 | |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 6851 | // If we don't allow any conversion of the result type, ignore conversion |
| 6852 | // functions that don't convert to exactly (possibly cv-qualified) T. |
| 6853 | if (!AllowResultConversion && |
| 6854 | !Context.hasSameUnqualifiedType(Conversion->getConversionType(), ToType)) |
| 6855 | return; |
| 6856 | |
| Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 6857 | // Per C++ [over.match.conv]p1, [over.match.ref]p1, an explicit conversion |
| 6858 | // operator is only a candidate if its return type is the target type or |
| 6859 | // can be converted to the target type with a qualification conversion. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 6860 | if (Conversion->isExplicit() && |
| 6861 | !isAllowableExplicitConversion(*this, ConvType, ToType, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6862 | AllowObjCConversionOnExplicit)) |
| Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 6863 | return; |
| 6864 | |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6865 | // Overload resolution is always an unevaluated context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6866 | EnterExpressionEvaluationContext Unevaluated( |
| 6867 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6868 | |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6869 | // Add this candidate |
| Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6870 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6871 | Candidate.FoundDecl = FoundDecl; |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6872 | Candidate.Function = Conversion; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6873 | Candidate.IsSurrogate = false; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6874 | Candidate.IgnoreObjectArgument = false; |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6875 | Candidate.FinalConversion.setAsIdentityConversion(); |
| Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6876 | Candidate.FinalConversion.setFromType(ConvType); |
| Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 6877 | Candidate.FinalConversion.setAllToTypes(ToType); |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6878 | Candidate.Viable = true; |
| Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6879 | Candidate.ExplicitCallArguments = 1; |
| Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6880 | |
| Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 6881 | // C++ [over.match.funcs]p4: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6882 | // For conversion functions, the function is considered to be a member of |
| 6883 | // the class of the implicit implied object argument for the purpose of |
| Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 6884 | // defining the type of the implicit object parameter. |
| Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6885 | // |
| 6886 | // Determine the implicit conversion sequence for the implicit |
| 6887 | // object parameter. |
| 6888 | QualType ImplicitParamType = From->getType(); |
| 6889 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 6890 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 6891 | CXXRecordDecl *ConversionContext |
| 6892 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6893 | |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 6894 | Candidate.Conversions[0] = TryObjectArgumentInitialization( |
| 6895 | *this, CandidateSet.getLocation(), From->getType(), |
| 6896 | From->Classify(Context), Conversion, ConversionContext); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6897 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6898 | if (Candidate.Conversions[0].isBad()) { |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6899 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6900 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6901 | return; |
| 6902 | } |
| Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6903 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6904 | // We won't go through a user-defined type conversion function to convert a |
| Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 6905 | // derived to base as such conversions are given Conversion Rank. They only |
| 6906 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 6907 | QualType FromCanon |
| 6908 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 6909 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 6910 | if (FromCanon == ToCanon || |
| 6911 | IsDerivedFrom(CandidateSet.getLocation(), FromCanon, ToCanon)) { |
| Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 6912 | Candidate.Viable = false; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6913 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
| Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 6914 | return; |
| 6915 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6916 | |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6917 | // To determine what the conversion from the result of calling the |
| 6918 | // conversion function to the type we're eventually trying to |
| 6919 | // convert to (ToType), we need to synthesize a call to the |
| 6920 | // conversion function and attempt copy initialization from it. This |
| 6921 | // makes sure that we get the right semantics with respect to |
| 6922 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 6923 | // call on the stack and we don't need its arguments to be |
| 6924 | // well-formed. |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 6925 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6926 | VK_LValue, From->getLocStart()); |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6927 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 6928 | Context.getPointerType(Conversion->getType()), |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6929 | CK_FunctionToPointerDecay, |
| John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6930 | &ConversionRef, VK_RValue); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6931 | |
| Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6932 | QualType ConversionType = Conversion->getConversionType(); |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 6933 | if (!isCompleteType(From->getLocStart(), ConversionType)) { |
| Douglas Gregor | 72ebdab | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 6934 | Candidate.Viable = false; |
| 6935 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 6936 | return; |
| 6937 | } |
| 6938 | |
| Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6939 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6940 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6941 | // Note that it is safe to allocate CallExpr on the stack here because |
| Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 6942 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 6943 | // allocator). |
| Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6944 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
| Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6945 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
| Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 6946 | From->getLocStart()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6947 | ImplicitConversionSequence ICS = |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6948 | TryCopyInitialization(*this, &Call, ToType, |
| Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6949 | /*SuppressUserConversions=*/true, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6950 | /*InOverloadResolution=*/false, |
| 6951 | /*AllowObjCWritebackConversion=*/false); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6952 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6953 | switch (ICS.getKind()) { |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6954 | case ImplicitConversionSequence::StandardConversion: |
| 6955 | Candidate.FinalConversion = ICS.Standard; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6956 | |
| Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6957 | // C++ [over.ics.user]p3: |
| 6958 | // If the user-defined conversion is specified by a specialization of a |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6959 | // conversion function template, the second standard conversion sequence |
| Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6960 | // shall have exact match rank. |
| 6961 | if (Conversion->getPrimaryTemplate() && |
| 6962 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 6963 | Candidate.Viable = false; |
| 6964 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6965 | return; |
| Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6966 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | |
| Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6968 | // C++0x [dcl.init.ref]p5: |
| 6969 | // In the second case, if the reference is an rvalue reference and |
| 6970 | // the second standard conversion sequence of the user-defined |
| 6971 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 6972 | // program is ill-formed. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6973 | if (ToType->isRValueReferenceType() && |
| Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6974 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 6975 | Candidate.Viable = false; |
| 6976 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6977 | return; |
| Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6978 | } |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6979 | break; |
| 6980 | |
| 6981 | case ImplicitConversionSequence::BadConversion: |
| 6982 | Candidate.Viable = false; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6983 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6984 | return; |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6985 | |
| 6986 | default: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6987 | llvm_unreachable( |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6988 | "Can only end up with a standard conversion sequence or failure"); |
| 6989 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6990 | |
| Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6991 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, None)) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6992 | Candidate.Viable = false; |
| 6993 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6994 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6995 | return; |
| 6996 | } |
| Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 6997 | |
| 6998 | if (Conversion->isMultiVersion() && |
| 6999 | !Conversion->getAttr<TargetAttr>()->isDefaultVersion()) { |
| 7000 | Candidate.Viable = false; |
| 7001 | Candidate.FailureKind = ovl_non_default_multiversion_function; |
| 7002 | } |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7003 | } |
| 7004 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7005 | /// Adds a conversion function template specialization |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7006 | /// candidate to the overload set, using template argument deduction |
| 7007 | /// to deduce the template arguments of the conversion function |
| 7008 | /// template from the type that we are converting to (C++ |
| 7009 | /// [temp.deduct.conv]). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7010 | void |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7011 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7012 | DeclAccessPair FoundDecl, |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7013 | CXXRecordDecl *ActingDC, |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7014 | Expr *From, QualType ToType, |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 7015 | OverloadCandidateSet &CandidateSet, |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 7016 | bool AllowObjCConversionOnExplicit, |
| 7017 | bool AllowResultConversion) { |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7018 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 7019 | "Only conversion function templates permitted here"); |
| 7020 | |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 7021 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 7022 | return; |
| 7023 | |
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 7024 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7025 | CXXConversionDecl *Specialization = nullptr; |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7026 | if (TemplateDeductionResult Result |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7027 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7028 | Specialization, Info)) { |
| Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 7029 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
| Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 7030 | Candidate.FoundDecl = FoundDecl; |
| 7031 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 7032 | Candidate.Viable = false; |
| 7033 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 7034 | Candidate.IsSurrogate = false; |
| 7035 | Candidate.IgnoreObjectArgument = false; |
| Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7036 | Candidate.ExplicitCallArguments = 1; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7037 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 7038 | Info); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7039 | return; |
| 7040 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7041 | |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7042 | // Add the conversion function template specialization produced by |
| 7043 | // template argument deduction as a candidate. |
| 7044 | assert(Specialization && "Missing function template specialization?"); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7045 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 7046 | CandidateSet, AllowObjCConversionOnExplicit, |
| 7047 | AllowResultConversion); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7048 | } |
| 7049 | |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7050 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 7051 | /// converts the given @c Object to a function pointer via the |
| 7052 | /// conversion function @c Conversion, and then attempts to call it |
| 7053 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 7054 | /// the type of function that we'll eventually be calling. |
| 7055 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7056 | DeclAccessPair FoundDecl, |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7057 | CXXRecordDecl *ActingContext, |
| Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7058 | const FunctionProtoType *Proto, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 7059 | Expr *Object, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7060 | ArrayRef<Expr *> Args, |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7061 | OverloadCandidateSet& CandidateSet) { |
| Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 7062 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 7063 | return; |
| 7064 | |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7065 | // Overload resolution is always an unevaluated context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 7066 | EnterExpressionEvaluationContext Unevaluated( |
| 7067 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7068 | |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7069 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7070 | Candidate.FoundDecl = FoundDecl; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7071 | Candidate.Function = nullptr; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7072 | Candidate.Surrogate = Conversion; |
| 7073 | Candidate.Viable = true; |
| 7074 | Candidate.IsSurrogate = true; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7075 | Candidate.IgnoreObjectArgument = false; |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7076 | Candidate.ExplicitCallArguments = Args.size(); |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7077 | |
| 7078 | // Determine the implicit conversion sequence for the implicit |
| 7079 | // object parameter. |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 7080 | ImplicitConversionSequence ObjectInit = TryObjectArgumentInitialization( |
| 7081 | *this, CandidateSet.getLocation(), Object->getType(), |
| 7082 | Object->Classify(Context), Conversion, ActingContext); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7083 | if (ObjectInit.isBad()) { |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7084 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7085 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7086 | Candidate.Conversions[0] = ObjectInit; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7087 | return; |
| 7088 | } |
| 7089 | |
| 7090 | // The first conversion is actually a user-defined conversion whose |
| 7091 | // first conversion is ObjectInit's standard conversion (which is |
| 7092 | // effectively a reference binding). Record it as such. |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7093 | Candidate.Conversions[0].setUserDefined(); |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7094 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 7095 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 7096 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7097 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
| John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 7098 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7099 | Candidate.Conversions[0].UserDefined.After |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7100 | = Candidate.Conversions[0].UserDefined.Before; |
| 7101 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 7102 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7103 | // Find the |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 7104 | unsigned NumParams = Proto->getNumParams(); |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7105 | |
| 7106 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 7107 | // parameters is viable only if it has an ellipsis in its parameter |
| 7108 | // list (8.3.5). |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 7109 | if (Args.size() > NumParams && !Proto->isVariadic()) { |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7110 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7111 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7112 | return; |
| 7113 | } |
| 7114 | |
| 7115 | // Function types don't have any default arguments, so just check if |
| 7116 | // we have enough arguments. |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 7117 | if (Args.size() < NumParams) { |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7118 | // Not enough arguments. |
| 7119 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7120 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7121 | return; |
| 7122 | } |
| 7123 | |
| 7124 | // Determine the implicit conversion sequences for each of the |
| 7125 | // arguments. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7126 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 7127 | if (ArgIdx < NumParams) { |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7128 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 7129 | // exist for each argument an implicit conversion sequence |
| 7130 | // (13.3.3.1) that converts that argument to the corresponding |
| 7131 | // parameter of F. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7132 | QualType ParamType = Proto->getParamType(ArgIdx); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7133 | Candidate.Conversions[ArgIdx + 1] |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7134 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
| Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 7135 | /*SuppressUserConversions=*/false, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7136 | /*InOverloadResolution=*/false, |
| 7137 | /*AllowObjCWritebackConversion=*/ |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7138 | getLangOpts().ObjCAutoRefCount); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7139 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7140 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7141 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7142 | return; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7143 | } |
| 7144 | } else { |
| 7145 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 7146 | // argument for which there is no corresponding parameter is |
| 7147 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7148 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7149 | } |
| 7150 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7151 | |
| Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 7152 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, None)) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7153 | Candidate.Viable = false; |
| 7154 | Candidate.FailureKind = ovl_fail_enable_if; |
| 7155 | Candidate.DeductionFailure.Data = FailedAttr; |
| 7156 | return; |
| 7157 | } |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7158 | } |
| 7159 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7160 | /// Add overload candidates for overloaded operators that are |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7161 | /// member functions. |
| 7162 | /// |
| 7163 | /// Add the overloaded operator candidates that are member functions |
| 7164 | /// for the operator Op that was used in an operator expression such |
| 7165 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 7166 | /// CandidateSet will store the added overload candidates. (C++ |
| 7167 | /// [over.match.oper]). |
| 7168 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 7169 | SourceLocation OpLoc, |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7170 | ArrayRef<Expr *> Args, |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7171 | OverloadCandidateSet& CandidateSet, |
| 7172 | SourceRange OpRange) { |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 7173 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 7174 | |
| 7175 | // C++ [over.match.oper]p3: |
| 7176 | // For a unary operator @ with an operand of a type whose |
| 7177 | // cv-unqualified version is T1, and for a binary operator @ with |
| 7178 | // a left operand of a type whose cv-unqualified version is T1 and |
| 7179 | // a right operand of a type whose cv-unqualified version is T2, |
| 7180 | // three sets of candidate functions, designated member |
| 7181 | // candidates, non-member candidates and built-in candidates, are |
| 7182 | // constructed as follows: |
| 7183 | QualType T1 = Args[0]->getType(); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 7184 | |
| Richard Smith | 0feaf0c | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 7185 | // -- If T1 is a complete class type or a class currently being |
| 7186 | // defined, the set of member candidates is the result of the |
| 7187 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 7188 | // the set of member candidates is empty. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7189 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
| Richard Smith | 0feaf0c | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 7190 | // Complete the type if it can be completed. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 7191 | if (!isCompleteType(OpLoc, T1) && !T1Rec->isBeingDefined()) |
| Richard Smith | 82b8d4e | 2015-12-18 22:19:11 +0000 | [diff] [blame] | 7192 | return; |
| Richard Smith | 0feaf0c | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 7193 | // If the type is neither complete nor being defined, bail out now. |
| 7194 | if (!T1Rec->getDecl()->getDefinition()) |
| Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 7195 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7196 | |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7197 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7198 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 7199 | Operators.suppressDiagnostics(); |
| 7200 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7201 | for (LookupResult::iterator Oper = Operators.begin(), |
| Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 7202 | OperEnd = Operators.end(); |
| 7203 | Oper != OperEnd; |
| John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 7204 | ++Oper) |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7205 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 7206 | Args[0]->Classify(Context), Args.slice(1), |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 7207 | CandidateSet, /*SuppressUserConversions=*/false); |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 7208 | } |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 7209 | } |
| 7210 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7211 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 7212 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 7213 | /// of the built-in candidate, respectively. Args and NumArgs are the |
| Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 7214 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 7215 | /// should be true when this built-in candidate is an assignment |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7216 | /// operator. NumContextualBoolArguments is the number of arguments |
| 7217 | /// (at the beginning of the argument list) that will be contextually |
| 7218 | /// converted to bool. |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7219 | void Sema::AddBuiltinCandidate(QualType *ParamTys, ArrayRef<Expr *> Args, |
| Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 7220 | OverloadCandidateSet& CandidateSet, |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7221 | bool IsAssignmentOperator, |
| 7222 | unsigned NumContextualBoolArguments) { |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7223 | // Overload resolution is always an unevaluated context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 7224 | EnterExpressionEvaluationContext Unevaluated( |
| 7225 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7226 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7227 | // Add this candidate |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7228 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7229 | Candidate.FoundDecl = DeclAccessPair::make(nullptr, AS_none); |
| 7230 | Candidate.Function = nullptr; |
| Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 7231 | Candidate.IsSurrogate = false; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7232 | Candidate.IgnoreObjectArgument = false; |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 7233 | std::copy(ParamTys, ParamTys + Args.size(), Candidate.BuiltinParamTypes); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7234 | |
| 7235 | // Determine the implicit conversion sequences for each of the |
| 7236 | // arguments. |
| 7237 | Candidate.Viable = true; |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7238 | Candidate.ExplicitCallArguments = Args.size(); |
| 7239 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 7240 | // C++ [over.match.oper]p4: |
| 7241 | // For the built-in assignment operators, conversions of the |
| 7242 | // left operand are restricted as follows: |
| 7243 | // -- no temporaries are introduced to hold the left operand, and |
| 7244 | // -- no user-defined conversions are applied to the left |
| 7245 | // operand to achieve a type match with the left-most |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7246 | // parameter of a built-in candidate. |
| Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 7247 | // |
| 7248 | // We block these conversions by turning off user-defined |
| 7249 | // conversions, since that is the only way that initialization of |
| 7250 | // a reference to a non-class type can occur from something that |
| 7251 | // is not of the same type. |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7252 | if (ArgIdx < NumContextualBoolArguments) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7253 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7254 | "Contextual conversion to bool requires bool type"); |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7255 | Candidate.Conversions[ArgIdx] |
| 7256 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7257 | } else { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7258 | Candidate.Conversions[ArgIdx] |
| Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7259 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
| Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 7260 | ArgIdx == 0 && IsAssignmentOperator, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7261 | /*InOverloadResolution=*/false, |
| 7262 | /*AllowObjCWritebackConversion=*/ |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7263 | getLangOpts().ObjCAutoRefCount); |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7264 | } |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7265 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7266 | Candidate.Viable = false; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7267 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 7268 | break; |
| 7269 | } |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7270 | } |
| 7271 | } |
| 7272 | |
| Craig Topper | cd7b033 | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 7273 | namespace { |
| 7274 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7275 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 7276 | /// candidate operator functions for built-in operators (C++ |
| 7277 | /// [over.built]). The types are separated into pointer types and |
| 7278 | /// enumeration types. |
| 7279 | class BuiltinCandidateTypeSet { |
| 7280 | /// TypeSet - A set of types. |
| Richard Smith | 9585307 | 2016-03-25 00:08:53 +0000 | [diff] [blame] | 7281 | typedef llvm::SetVector<QualType, SmallVector<QualType, 8>, |
| 7282 | llvm::SmallPtrSet<QualType, 8>> TypeSet; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7283 | |
| 7284 | /// PointerTypes - The set of pointer types that will be used in the |
| 7285 | /// built-in candidates. |
| 7286 | TypeSet PointerTypes; |
| 7287 | |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7288 | /// MemberPointerTypes - The set of member pointer types that will be |
| 7289 | /// used in the built-in candidates. |
| 7290 | TypeSet MemberPointerTypes; |
| 7291 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7292 | /// EnumerationTypes - The set of enumeration types that will be |
| 7293 | /// used in the built-in candidates. |
| 7294 | TypeSet EnumerationTypes; |
| 7295 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7296 | /// The set of vector types that will be used in the built-in |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7297 | /// candidates. |
| 7298 | TypeSet VectorTypes; |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7299 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7300 | /// A flag indicating non-record types are viable candidates |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7301 | bool HasNonRecordTypes; |
| 7302 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7303 | /// A flag indicating whether either arithmetic or enumeration types |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7304 | /// were present in the candidate set. |
| 7305 | bool HasArithmeticOrEnumeralTypes; |
| 7306 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7307 | /// A flag indicating whether the nullptr type was present in the |
| Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7308 | /// candidate set. |
| 7309 | bool HasNullPtrType; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7310 | |
| Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 7311 | /// Sema - The semantic analysis instance where we are building the |
| 7312 | /// candidate type set. |
| 7313 | Sema &SemaRef; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7314 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7315 | /// Context - The AST context in which we will build the type sets. |
| 7316 | ASTContext &Context; |
| 7317 | |
| Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 7318 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 7319 | const Qualifiers &VisibleQuals); |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7320 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7321 | |
| 7322 | public: |
| 7323 | /// iterator - Iterates through the types that are part of the set. |
| Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 7324 | typedef TypeSet::iterator iterator; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7325 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7326 | BuiltinCandidateTypeSet(Sema &SemaRef) |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7327 | : HasNonRecordTypes(false), |
| 7328 | HasArithmeticOrEnumeralTypes(false), |
| Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7329 | HasNullPtrType(false), |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7330 | SemaRef(SemaRef), |
| 7331 | Context(SemaRef.Context) { } |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7332 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7333 | void AddTypesConvertedFrom(QualType Ty, |
| Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 7334 | SourceLocation Loc, |
| 7335 | bool AllowUserConversions, |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7336 | bool AllowExplicitConversions, |
| 7337 | const Qualifiers &VisibleTypeConversionsQuals); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7338 | |
| 7339 | /// pointer_begin - First pointer type found; |
| 7340 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 7341 | |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7342 | /// pointer_end - Past the last pointer type found; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7343 | iterator pointer_end() { return PointerTypes.end(); } |
| 7344 | |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7345 | /// member_pointer_begin - First member pointer type found; |
| 7346 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 7347 | |
| 7348 | /// member_pointer_end - Past the last member pointer type found; |
| 7349 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 7350 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7351 | /// enumeration_begin - First enumeration type found; |
| 7352 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 7353 | |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7354 | /// enumeration_end - Past the last enumeration type found; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7355 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7356 | |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7357 | iterator vector_begin() { return VectorTypes.begin(); } |
| 7358 | iterator vector_end() { return VectorTypes.end(); } |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7359 | |
| 7360 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 7361 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
| Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7362 | bool hasNullPtrType() const { return HasNullPtrType; } |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7363 | }; |
| 7364 | |
| Craig Topper | cd7b033 | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 7365 | } // end anonymous namespace |
| 7366 | |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7367 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7368 | /// the set of pointer types along with any more-qualified variants of |
| 7369 | /// that type. For example, if @p Ty is "int const *", this routine |
| 7370 | /// will add "int const *", "int const volatile *", "int const |
| 7371 | /// restrict *", and "int const volatile restrict *" to the set of |
| 7372 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 7373 | /// false otherwise. |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7374 | /// |
| 7375 | /// FIXME: what to do about extended qualifiers? |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7376 | bool |
| Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 7377 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 7378 | const Qualifiers &VisibleQuals) { |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7379 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7380 | // Insert this type. |
| Richard Smith | 9585307 | 2016-03-25 00:08:53 +0000 | [diff] [blame] | 7381 | if (!PointerTypes.insert(Ty)) |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7382 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7383 | |
| Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 7384 | QualType PointeeTy; |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7385 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 7386 | bool buildObjCPtr = false; |
| Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 7387 | if (!PointerTy) { |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7388 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 7389 | PointeeTy = PTy->getPointeeType(); |
| 7390 | buildObjCPtr = true; |
| 7391 | } else { |
| Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 7392 | PointeeTy = PointerTy->getPointeeType(); |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7393 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7394 | |
| Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 7395 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 7396 | // (the qualifier would sink to the element type), and for another, the |
| 7397 | // only overload situation where it matters is subscript or pointer +- int, |
| 7398 | // and those shouldn't have qualifier variants anyway. |
| 7399 | if (PointeeTy->isArrayType()) |
| 7400 | return true; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7401 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7402 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 7403 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 7404 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7405 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7406 | // Iterate through all strict supersets of BaseCVR. |
| 7407 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 7408 | if ((CVR | BaseCVR) != CVR) continue; |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7409 | // Skip over volatile if no volatile found anywhere in the types. |
| Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 7410 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7411 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7412 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 7413 | // the type cannot be restrict-qualified. |
| 7414 | if ((CVR & Qualifiers::Restrict) && |
| 7415 | (!hasRestrict || |
| 7416 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 7417 | continue; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7418 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7419 | // Build qualified pointee type. |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7420 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7421 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7422 | // Build qualified pointer type. |
| 7423 | QualType QPointerTy; |
| Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 7424 | if (!buildObjCPtr) |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7425 | QPointerTy = Context.getPointerType(QPointeeTy); |
| Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 7426 | else |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7427 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7428 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7429 | // Insert qualified pointer type. |
| 7430 | PointerTypes.insert(QPointerTy); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
| 7433 | return true; |
| 7434 | } |
| 7435 | |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7436 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 7437 | /// to the set of pointer types along with any more-qualified variants of |
| 7438 | /// that type. For example, if @p Ty is "int const *", this routine |
| 7439 | /// will add "int const *", "int const volatile *", "int const |
| 7440 | /// restrict *", and "int const volatile restrict *" to the set of |
| 7441 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 7442 | /// false otherwise. |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7443 | /// |
| 7444 | /// FIXME: what to do about extended qualifiers? |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7445 | bool |
| 7446 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 7447 | QualType Ty) { |
| 7448 | // Insert this type. |
| Richard Smith | 9585307 | 2016-03-25 00:08:53 +0000 | [diff] [blame] | 7449 | if (!MemberPointerTypes.insert(Ty)) |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7450 | return false; |
| 7451 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7452 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 7453 | assert(PointerTy && "type was not a member pointer type!"); |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7454 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7455 | QualType PointeeTy = PointerTy->getPointeeType(); |
| Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 7456 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 7457 | // (the qualifier would sink to the element type), and for another, the |
| 7458 | // only overload situation where it matters is subscript or pointer +- int, |
| 7459 | // and those shouldn't have qualifier variants anyway. |
| 7460 | if (PointeeTy->isArrayType()) |
| 7461 | return true; |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7462 | const Type *ClassTy = PointerTy->getClass(); |
| 7463 | |
| 7464 | // Iterate through all strict supersets of the pointee type's CVR |
| 7465 | // qualifiers. |
| 7466 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 7467 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 7468 | if ((CVR | BaseCVR) != CVR) continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7469 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7470 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7471 | MemberPointerTypes.insert( |
| 7472 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7473 | } |
| 7474 | |
| 7475 | return true; |
| 7476 | } |
| 7477 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7478 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 7479 | /// Ty can be implicit converted to the given set of @p Types. We're |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7480 | /// primarily interested in pointer types and enumeration types. We also |
| 7481 | /// take member pointer types, for the conditional operator. |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7482 | /// AllowUserConversions is true if we should look at the conversion |
| 7483 | /// functions of a class type, and AllowExplicitConversions if we |
| 7484 | /// should also include the explicit conversion functions of a class |
| 7485 | /// type. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7486 | void |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7487 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 7488 | SourceLocation Loc, |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7489 | bool AllowUserConversions, |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7490 | bool AllowExplicitConversions, |
| 7491 | const Qualifiers &VisibleQuals) { |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7492 | // Only deal with canonical types. |
| 7493 | Ty = Context.getCanonicalType(Ty); |
| 7494 | |
| 7495 | // Look through reference types; they aren't part of the type of an |
| 7496 | // expression for the purposes of conversions. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7497 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7498 | Ty = RefTy->getPointeeType(); |
| 7499 | |
| John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 7500 | // If we're dealing with an array type, decay to the pointer. |
| 7501 | if (Ty->isArrayType()) |
| 7502 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 7503 | |
| 7504 | // Otherwise, we don't care about qualifiers on the type. |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7505 | Ty = Ty.getLocalUnqualifiedType(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7506 | |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7507 | // Flag if we ever add a non-record type. |
| 7508 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 7509 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 7510 | |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7511 | // Flag if we encounter an arithmetic type. |
| 7512 | HasArithmeticOrEnumeralTypes = |
| 7513 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 7514 | |
| Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 7515 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 7516 | PointerTypes.insert(Ty); |
| 7517 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7518 | // Insert our type, and its more-qualified variants, into the set |
| 7519 | // of types. |
| Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 7520 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7521 | return; |
| Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 7522 | } else if (Ty->isMemberPointerType()) { |
| 7523 | // Member pointers are far easier, since the pointee can't be converted. |
| 7524 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 7525 | return; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7526 | } else if (Ty->isEnumeralType()) { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7527 | HasArithmeticOrEnumeralTypes = true; |
| Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 7528 | EnumerationTypes.insert(Ty); |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7529 | } else if (Ty->isVectorType()) { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7530 | // We treat vector types as arithmetic types in many contexts as an |
| 7531 | // extension. |
| 7532 | HasArithmeticOrEnumeralTypes = true; |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7533 | VectorTypes.insert(Ty); |
| Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7534 | } else if (Ty->isNullPtrType()) { |
| 7535 | HasNullPtrType = true; |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7536 | } else if (AllowUserConversions && TyRec) { |
| 7537 | // No conversion functions in incomplete types. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 7538 | if (!SemaRef.isCompleteType(Loc, Ty)) |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7539 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7540 | |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7541 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 7542 | for (NamedDecl *D : ClassDecl->getVisibleConversionFunctions()) { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7543 | if (isa<UsingShadowDecl>(D)) |
| 7544 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7545 | |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7546 | // Skip conversion function templates; they don't tell us anything |
| 7547 | // about which builtin types we can convert to. |
| 7548 | if (isa<FunctionTemplateDecl>(D)) |
| 7549 | continue; |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7550 | |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7551 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 7552 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 7553 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 7554 | VisibleQuals); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7555 | } |
| 7556 | } |
| 7557 | } |
| 7558 | } |
| 7559 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7560 | /// Helper function for AddBuiltinOperatorCandidates() that adds |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7561 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 7562 | /// given type to the candidate set. |
| 7563 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 7564 | QualType T, |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7565 | ArrayRef<Expr *> Args, |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7566 | OverloadCandidateSet &CandidateSet) { |
| 7567 | QualType ParamTypes[2]; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7568 | |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7569 | // T& operator=(T&, T) |
| 7570 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 7571 | ParamTypes[1] = T; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7572 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7573 | /*IsAssignmentOperator=*/true); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7574 | |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7575 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 7576 | // volatile T& operator=(volatile T&, T) |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7577 | ParamTypes[0] |
| 7578 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7579 | ParamTypes[1] = T; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7580 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7581 | /*IsAssignmentOperator=*/true); |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7582 | } |
| 7583 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7584 | |
| Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 7585 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 7586 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7587 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 7588 | Qualifiers VRQuals; |
| 7589 | const RecordType *TyRec; |
| 7590 | if (const MemberPointerType *RHSMPType = |
| 7591 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 7592 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7593 | else |
| 7594 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 7595 | if (!TyRec) { |
| Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 7596 | // Just to be safe, assume the worst case. |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7597 | VRQuals.addVolatile(); |
| 7598 | VRQuals.addRestrict(); |
| 7599 | return VRQuals; |
| 7600 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7601 | |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7602 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 7603 | if (!ClassDecl->hasDefinition()) |
| 7604 | return VRQuals; |
| 7605 | |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 7606 | for (NamedDecl *D : ClassDecl->getVisibleConversionFunctions()) { |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7607 | if (isa<UsingShadowDecl>(D)) |
| 7608 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7609 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7610 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 7611 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 7612 | CanTy = ResTypeRef->getPointeeType(); |
| 7613 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 7614 | // as see them. |
| 7615 | bool done = false; |
| 7616 | while (!done) { |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7617 | if (CanTy.isRestrictQualified()) |
| 7618 | VRQuals.addRestrict(); |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7619 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 7620 | CanTy = ResTypePtr->getPointeeType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7621 | else if (const MemberPointerType *ResTypeMPtr = |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7622 | CanTy->getAs<MemberPointerType>()) |
| 7623 | CanTy = ResTypeMPtr->getPointeeType(); |
| 7624 | else |
| 7625 | done = true; |
| 7626 | if (CanTy.isVolatileQualified()) |
| 7627 | VRQuals.addVolatile(); |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7628 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 7629 | return VRQuals; |
| 7630 | } |
| 7631 | } |
| 7632 | } |
| 7633 | return VRQuals; |
| 7634 | } |
| John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7635 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7636 | namespace { |
| John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7637 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7638 | /// Helper class to manage the addition of builtin operator overload |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7639 | /// candidates. It provides shared state and utility methods used throughout |
| 7640 | /// the process, as well as a helper method to add each group of builtin |
| 7641 | /// operator overloads from the standard to a candidate set. |
| 7642 | class BuiltinOperatorOverloadBuilder { |
| Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7643 | // Common instance state available to all overload candidate addition methods. |
| 7644 | Sema &S; |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7645 | ArrayRef<Expr *> Args; |
| Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7646 | Qualifiers VisibleTypeConversionsQuals; |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7647 | bool HasArithmeticOrEnumeralCandidateType; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7648 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
| Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7649 | OverloadCandidateSet &CandidateSet; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7650 | |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7651 | static constexpr int ArithmeticTypesCap = 24; |
| 7652 | SmallVector<CanQualType, ArithmeticTypesCap> ArithmeticTypes; |
| 7653 | |
| 7654 | // Define some indices used to iterate over the arithemetic types in |
| 7655 | // ArithmeticTypes. The "promoted arithmetic types" are the arithmetic |
| John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7656 | // types are that preserved by promotion (C++ [over.built]p2). |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7657 | unsigned FirstIntegralType, |
| 7658 | LastIntegralType; |
| 7659 | unsigned FirstPromotedIntegralType, |
| 7660 | LastPromotedIntegralType; |
| 7661 | unsigned FirstPromotedArithmeticType, |
| 7662 | LastPromotedArithmeticType; |
| 7663 | unsigned NumArithmeticTypes; |
| John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7664 | |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7665 | void InitArithmeticTypes() { |
| 7666 | // Start of promoted types. |
| 7667 | FirstPromotedArithmeticType = 0; |
| 7668 | ArithmeticTypes.push_back(S.Context.FloatTy); |
| 7669 | ArithmeticTypes.push_back(S.Context.DoubleTy); |
| 7670 | ArithmeticTypes.push_back(S.Context.LongDoubleTy); |
| 7671 | if (S.Context.getTargetInfo().hasFloat128Type()) |
| 7672 | ArithmeticTypes.push_back(S.Context.Float128Ty); |
| John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 7673 | |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7674 | // Start of integral types. |
| 7675 | FirstIntegralType = ArithmeticTypes.size(); |
| 7676 | FirstPromotedIntegralType = ArithmeticTypes.size(); |
| 7677 | ArithmeticTypes.push_back(S.Context.IntTy); |
| 7678 | ArithmeticTypes.push_back(S.Context.LongTy); |
| 7679 | ArithmeticTypes.push_back(S.Context.LongLongTy); |
| 7680 | if (S.Context.getTargetInfo().hasInt128Type()) |
| 7681 | ArithmeticTypes.push_back(S.Context.Int128Ty); |
| 7682 | ArithmeticTypes.push_back(S.Context.UnsignedIntTy); |
| 7683 | ArithmeticTypes.push_back(S.Context.UnsignedLongTy); |
| 7684 | ArithmeticTypes.push_back(S.Context.UnsignedLongLongTy); |
| 7685 | if (S.Context.getTargetInfo().hasInt128Type()) |
| 7686 | ArithmeticTypes.push_back(S.Context.UnsignedInt128Ty); |
| 7687 | LastPromotedIntegralType = ArithmeticTypes.size(); |
| 7688 | LastPromotedArithmeticType = ArithmeticTypes.size(); |
| 7689 | // End of promoted types. |
| Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7690 | |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7691 | ArithmeticTypes.push_back(S.Context.BoolTy); |
| 7692 | ArithmeticTypes.push_back(S.Context.CharTy); |
| 7693 | ArithmeticTypes.push_back(S.Context.WCharTy); |
| Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 7694 | if (S.Context.getLangOpts().Char8) |
| 7695 | ArithmeticTypes.push_back(S.Context.Char8Ty); |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7696 | ArithmeticTypes.push_back(S.Context.Char16Ty); |
| 7697 | ArithmeticTypes.push_back(S.Context.Char32Ty); |
| 7698 | ArithmeticTypes.push_back(S.Context.SignedCharTy); |
| 7699 | ArithmeticTypes.push_back(S.Context.ShortTy); |
| 7700 | ArithmeticTypes.push_back(S.Context.UnsignedCharTy); |
| 7701 | ArithmeticTypes.push_back(S.Context.UnsignedShortTy); |
| 7702 | LastIntegralType = ArithmeticTypes.size(); |
| 7703 | NumArithmeticTypes = ArithmeticTypes.size(); |
| 7704 | // End of integral types. |
| 7705 | // FIXME: What about complex? What about half? |
| 7706 | |
| 7707 | assert(ArithmeticTypes.size() <= ArithmeticTypesCap && |
| 7708 | "Enough inline storage for all arithmetic types."); |
| Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7709 | } |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7710 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7711 | /// Helper method to factor out the common pattern of adding overloads |
| Chandler Carruth | 5659c0c | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 7712 | /// for '++' and '--' builtin operators. |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7713 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7714 | bool HasVolatile, |
| 7715 | bool HasRestrict) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7716 | QualType ParamTypes[2] = { |
| 7717 | S.Context.getLValueReferenceType(CandidateTy), |
| 7718 | S.Context.IntTy |
| 7719 | }; |
| 7720 | |
| 7721 | // Non-volatile version. |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7722 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7723 | |
| 7724 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 7725 | // add volatile version only if there are conversions to a volatile type. |
| 7726 | if (HasVolatile) { |
| 7727 | ParamTypes[0] = |
| 7728 | S.Context.getLValueReferenceType( |
| 7729 | S.Context.getVolatileType(CandidateTy)); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7730 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7731 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7732 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7733 | // Add restrict version only if there are conversions to a restrict type |
| 7734 | // and our candidate type is a non-restrict-qualified pointer. |
| 7735 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 7736 | !CandidateTy.isRestrictQualified()) { |
| 7737 | ParamTypes[0] |
| 7738 | = S.Context.getLValueReferenceType( |
| 7739 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7740 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 7741 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7742 | if (HasVolatile) { |
| 7743 | ParamTypes[0] |
| 7744 | = S.Context.getLValueReferenceType( |
| 7745 | S.Context.getCVRQualifiedType(CandidateTy, |
| 7746 | (Qualifiers::Volatile | |
| 7747 | Qualifiers::Restrict))); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7748 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7749 | } |
| 7750 | } |
| 7751 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | public: |
| 7755 | BuiltinOperatorOverloadBuilder( |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7756 | Sema &S, ArrayRef<Expr *> Args, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7757 | Qualifiers VisibleTypeConversionsQuals, |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7758 | bool HasArithmeticOrEnumeralCandidateType, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7759 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7760 | OverloadCandidateSet &CandidateSet) |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7761 | : S(S), Args(Args), |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7762 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7763 | HasArithmeticOrEnumeralCandidateType( |
| 7764 | HasArithmeticOrEnumeralCandidateType), |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7765 | CandidateTypes(CandidateTypes), |
| 7766 | CandidateSet(CandidateSet) { |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7767 | |
| 7768 | InitArithmeticTypes(); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
| Jan Korous | 536d2e3 | 2018-04-18 13:38:39 +0000 | [diff] [blame] | 7771 | // Increment is deprecated for bool since C++17. |
| 7772 | // |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7773 | // C++ [over.built]p3: |
| 7774 | // |
| Jan Korous | 536d2e3 | 2018-04-18 13:38:39 +0000 | [diff] [blame] | 7775 | // For every pair (T, VQ), where T is an arithmetic type other |
| 7776 | // than bool, and VQ is either volatile or empty, there exist |
| 7777 | // candidate operator functions of the form |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7778 | // |
| 7779 | // VQ T& operator++(VQ T&); |
| 7780 | // T operator++(VQ T&, int); |
| 7781 | // |
| 7782 | // C++ [over.built]p4: |
| 7783 | // |
| 7784 | // For every pair (T, VQ), where T is an arithmetic type other |
| 7785 | // than bool, and VQ is either volatile or empty, there exist |
| 7786 | // candidate operator functions of the form |
| 7787 | // |
| 7788 | // VQ T& operator--(VQ T&); |
| 7789 | // T operator--(VQ T&, int); |
| 7790 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7791 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7792 | return; |
| 7793 | |
| Jan Korous | d74ebe2 | 2018-04-11 13:36:29 +0000 | [diff] [blame] | 7794 | for (unsigned Arith = 0; Arith < NumArithmeticTypes; ++Arith) { |
| 7795 | const auto TypeOfT = ArithmeticTypes[Arith]; |
| Jan Korous | 536d2e3 | 2018-04-18 13:38:39 +0000 | [diff] [blame] | 7796 | if (TypeOfT == S.Context.BoolTy) { |
| 7797 | if (Op == OO_MinusMinus) |
| 7798 | continue; |
| 7799 | if (Op == OO_PlusPlus && S.getLangOpts().CPlusPlus17) |
| 7800 | continue; |
| 7801 | } |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7802 | addPlusPlusMinusMinusStyleOverloads( |
| Jan Korous | d74ebe2 | 2018-04-11 13:36:29 +0000 | [diff] [blame] | 7803 | TypeOfT, |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7804 | VisibleTypeConversionsQuals.hasVolatile(), |
| 7805 | VisibleTypeConversionsQuals.hasRestrict()); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7806 | } |
| 7807 | } |
| 7808 | |
| 7809 | // C++ [over.built]p5: |
| 7810 | // |
| 7811 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7812 | // cv-unqualified object type, and VQ is either volatile or |
| 7813 | // empty, there exist candidate operator functions of the form |
| 7814 | // |
| 7815 | // T*VQ& operator++(T*VQ&); |
| 7816 | // T*VQ& operator--(T*VQ&); |
| 7817 | // T* operator++(T*VQ&, int); |
| 7818 | // T* operator--(T*VQ&, int); |
| 7819 | void addPlusPlusMinusMinusPointerOverloads() { |
| 7820 | for (BuiltinCandidateTypeSet::iterator |
| 7821 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7822 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7823 | Ptr != PtrEnd; ++Ptr) { |
| 7824 | // Skip pointer types that aren't pointers to object types. |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7825 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7826 | continue; |
| 7827 | |
| 7828 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7829 | (!(*Ptr).isVolatileQualified() && |
| 7830 | VisibleTypeConversionsQuals.hasVolatile()), |
| 7831 | (!(*Ptr).isRestrictQualified() && |
| 7832 | VisibleTypeConversionsQuals.hasRestrict())); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7833 | } |
| 7834 | } |
| 7835 | |
| 7836 | // C++ [over.built]p6: |
| 7837 | // For every cv-qualified or cv-unqualified object type T, there |
| 7838 | // exist candidate operator functions of the form |
| 7839 | // |
| 7840 | // T& operator*(T*); |
| 7841 | // |
| 7842 | // C++ [over.built]p7: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7843 | // For every function type T that does not have cv-qualifiers or a |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 7844 | // ref-qualifier, there exist candidate operator functions of the form |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7845 | // T& operator*(T*); |
| 7846 | void addUnaryStarPointerOverloads() { |
| 7847 | for (BuiltinCandidateTypeSet::iterator |
| 7848 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7849 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7850 | Ptr != PtrEnd; ++Ptr) { |
| 7851 | QualType ParamTy = *Ptr; |
| 7852 | QualType PointeeTy = ParamTy->getPointeeType(); |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7853 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 7854 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7855 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 7856 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 7857 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 7858 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7859 | |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7860 | S.AddBuiltinCandidate(&ParamTy, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7861 | } |
| 7862 | } |
| 7863 | |
| 7864 | // C++ [over.built]p9: |
| 7865 | // For every promoted arithmetic type T, there exist candidate |
| 7866 | // operator functions of the form |
| 7867 | // |
| 7868 | // T operator+(T); |
| 7869 | // T operator-(T); |
| 7870 | void addUnaryPlusOrMinusArithmeticOverloads() { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7871 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7872 | return; |
| 7873 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7874 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 7875 | Arith < LastPromotedArithmeticType; ++Arith) { |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7876 | QualType ArithTy = ArithmeticTypes[Arith]; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7877 | S.AddBuiltinCandidate(&ArithTy, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | // Extension: We also add these operators for vector types. |
| 7881 | for (BuiltinCandidateTypeSet::iterator |
| 7882 | Vec = CandidateTypes[0].vector_begin(), |
| 7883 | VecEnd = CandidateTypes[0].vector_end(); |
| 7884 | Vec != VecEnd; ++Vec) { |
| 7885 | QualType VecTy = *Vec; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7886 | S.AddBuiltinCandidate(&VecTy, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7887 | } |
| 7888 | } |
| 7889 | |
| 7890 | // C++ [over.built]p8: |
| 7891 | // For every type T, there exist candidate operator functions of |
| 7892 | // the form |
| 7893 | // |
| 7894 | // T* operator+(T*); |
| 7895 | void addUnaryPlusPointerOverloads() { |
| 7896 | for (BuiltinCandidateTypeSet::iterator |
| 7897 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7898 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7899 | Ptr != PtrEnd; ++Ptr) { |
| 7900 | QualType ParamTy = *Ptr; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7901 | S.AddBuiltinCandidate(&ParamTy, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7902 | } |
| 7903 | } |
| 7904 | |
| 7905 | // C++ [over.built]p10: |
| 7906 | // For every promoted integral type T, there exist candidate |
| 7907 | // operator functions of the form |
| 7908 | // |
| 7909 | // T operator~(T); |
| 7910 | void addUnaryTildePromotedIntegralOverloads() { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7911 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7912 | return; |
| 7913 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7914 | for (unsigned Int = FirstPromotedIntegralType; |
| 7915 | Int < LastPromotedIntegralType; ++Int) { |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 7916 | QualType IntTy = ArithmeticTypes[Int]; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7917 | S.AddBuiltinCandidate(&IntTy, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7918 | } |
| 7919 | |
| 7920 | // Extension: We also add this operator for vector types. |
| 7921 | for (BuiltinCandidateTypeSet::iterator |
| 7922 | Vec = CandidateTypes[0].vector_begin(), |
| 7923 | VecEnd = CandidateTypes[0].vector_end(); |
| 7924 | Vec != VecEnd; ++Vec) { |
| 7925 | QualType VecTy = *Vec; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7926 | S.AddBuiltinCandidate(&VecTy, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7927 | } |
| 7928 | } |
| 7929 | |
| 7930 | // C++ [over.match.oper]p16: |
| Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 7931 | // For every pointer to member type T or type std::nullptr_t, there |
| 7932 | // exist candidate operator functions of the form |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7933 | // |
| 7934 | // bool operator==(T,T); |
| 7935 | // bool operator!=(T,T); |
| Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 7936 | void addEqualEqualOrNotEqualMemberPointerOrNullptrOverloads() { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7937 | /// Set of (canonical) types that we've already handled. |
| 7938 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7939 | |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7940 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7941 | for (BuiltinCandidateTypeSet::iterator |
| 7942 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7943 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7944 | MemPtr != MemPtrEnd; |
| 7945 | ++MemPtr) { |
| 7946 | // Don't add the same builtin candidate twice. |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 7947 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7948 | continue; |
| 7949 | |
| 7950 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7951 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7952 | } |
| Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 7953 | |
| 7954 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7955 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7956 | if (AddedTypes.insert(NullPtrTy).second) { |
| 7957 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 7958 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 7959 | } |
| 7960 | } |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7961 | } |
| 7962 | } |
| 7963 | |
| 7964 | // C++ [over.built]p15: |
| 7965 | // |
| Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 7966 | // For every T, where T is an enumeration type or a pointer type, |
| 7967 | // there exist candidate operator functions of the form |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7968 | // |
| 7969 | // bool operator<(T, T); |
| 7970 | // bool operator>(T, T); |
| 7971 | // bool operator<=(T, T); |
| 7972 | // bool operator>=(T, T); |
| 7973 | // bool operator==(T, T); |
| 7974 | // bool operator!=(T, T); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7975 | // R operator<=>(T, T) |
| 7976 | void addGenericBinaryPointerOrEnumeralOverloads() { |
| Eli Friedman | 14f082b | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7977 | // C++ [over.match.oper]p3: |
| 7978 | // [...]the built-in candidates include all of the candidate operator |
| 7979 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 7980 | // do not have the same parameter-type-list as any non-template non-member |
| 7981 | // candidate. |
| Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7982 | // |
| Eli Friedman | 14f082b | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7983 | // Note that in practice, this only affects enumeration types because there |
| 7984 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7985 | // must have an operand of record or enumeration type. Also, the only other |
| 7986 | // overloaded operator with enumeration arguments, operator=, |
| Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7987 | // cannot be overloaded for enumeration types, so this is the only place |
| 7988 | // where we must suppress candidates like this. |
| 7989 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7990 | UserDefinedBinaryOperators; |
| 7991 | |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7992 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7993 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7994 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7995 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7996 | CEnd = CandidateSet.end(); |
| 7997 | C != CEnd; ++C) { |
| 7998 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7999 | continue; |
| 8000 | |
| Eli Friedman | 14f082b | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 8001 | if (C->Function->isFunctionTemplateSpecialization()) |
| 8002 | continue; |
| 8003 | |
| Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 8004 | QualType FirstParamType = |
| 8005 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 8006 | QualType SecondParamType = |
| 8007 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 8008 | |
| 8009 | // Skip if either parameter isn't of enumeral type. |
| 8010 | if (!FirstParamType->isEnumeralType() || |
| 8011 | !SecondParamType->isEnumeralType()) |
| 8012 | continue; |
| 8013 | |
| 8014 | // Add this operator to the set of known user-defined operators. |
| 8015 | UserDefinedBinaryOperators.insert( |
| 8016 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 8017 | S.Context.getCanonicalType(SecondParamType))); |
| 8018 | } |
| 8019 | } |
| 8020 | } |
| 8021 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8022 | /// Set of (canonical) types that we've already handled. |
| 8023 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 8024 | |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8025 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8026 | for (BuiltinCandidateTypeSet::iterator |
| 8027 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 8028 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 8029 | Ptr != PtrEnd; ++Ptr) { |
| 8030 | // Don't add the same builtin candidate twice. |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8031 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8032 | continue; |
| 8033 | |
| 8034 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8035 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8036 | } |
| 8037 | for (BuiltinCandidateTypeSet::iterator |
| 8038 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 8039 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 8040 | Enum != EnumEnd; ++Enum) { |
| 8041 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 8042 | |
| Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 8043 | // Don't add the same builtin candidate twice, or if a user defined |
| 8044 | // candidate exists. |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8045 | if (!AddedTypes.insert(CanonType).second || |
| Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 8046 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 8047 | CanonType))) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8048 | continue; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8049 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8050 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8051 | } |
| 8052 | } |
| 8053 | } |
| 8054 | |
| 8055 | // C++ [over.built]p13: |
| 8056 | // |
| 8057 | // For every cv-qualified or cv-unqualified object type T |
| 8058 | // there exist candidate operator functions of the form |
| 8059 | // |
| 8060 | // T* operator+(T*, ptrdiff_t); |
| 8061 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 8062 | // T* operator-(T*, ptrdiff_t); |
| 8063 | // T* operator+(ptrdiff_t, T*); |
| 8064 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 8065 | // |
| 8066 | // C++ [over.built]p14: |
| 8067 | // |
| 8068 | // For every T, where T is a pointer to object type, there |
| 8069 | // exist candidate operator functions of the form |
| 8070 | // |
| 8071 | // ptrdiff_t operator-(T, T); |
| 8072 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 8073 | /// Set of (canonical) types that we've already handled. |
| 8074 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 8075 | |
| 8076 | for (int Arg = 0; Arg < 2; ++Arg) { |
| Eric Christopher | 9207a52 | 2015-08-21 16:24:01 +0000 | [diff] [blame] | 8077 | QualType AsymmetricParamTypes[2] = { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8078 | S.Context.getPointerDiffType(), |
| 8079 | S.Context.getPointerDiffType(), |
| 8080 | }; |
| 8081 | for (BuiltinCandidateTypeSet::iterator |
| 8082 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 8083 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 8084 | Ptr != PtrEnd; ++Ptr) { |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 8085 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 8086 | if (!PointeeTy->isObjectType()) |
| 8087 | continue; |
| 8088 | |
| Eric Christopher | 9207a52 | 2015-08-21 16:24:01 +0000 | [diff] [blame] | 8089 | AsymmetricParamTypes[Arg] = *Ptr; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8090 | if (Arg == 0 || Op == OO_Plus) { |
| 8091 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 8092 | // T* operator+(ptrdiff_t, T*); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8093 | S.AddBuiltinCandidate(AsymmetricParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8094 | } |
| 8095 | if (Op == OO_Minus) { |
| 8096 | // ptrdiff_t operator-(T, T); |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8097 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8098 | continue; |
| 8099 | |
| 8100 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8101 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8102 | } |
| 8103 | } |
| 8104 | } |
| 8105 | } |
| 8106 | |
| 8107 | // C++ [over.built]p12: |
| 8108 | // |
| 8109 | // For every pair of promoted arithmetic types L and R, there |
| 8110 | // exist candidate operator functions of the form |
| 8111 | // |
| 8112 | // LR operator*(L, R); |
| 8113 | // LR operator/(L, R); |
| 8114 | // LR operator+(L, R); |
| 8115 | // LR operator-(L, R); |
| 8116 | // bool operator<(L, R); |
| 8117 | // bool operator>(L, R); |
| 8118 | // bool operator<=(L, R); |
| 8119 | // bool operator>=(L, R); |
| 8120 | // bool operator==(L, R); |
| 8121 | // bool operator!=(L, R); |
| 8122 | // |
| 8123 | // where LR is the result of the usual arithmetic conversions |
| 8124 | // between types L and R. |
| 8125 | // |
| 8126 | // C++ [over.built]p24: |
| 8127 | // |
| 8128 | // For every pair of promoted arithmetic types L and R, there exist |
| 8129 | // candidate operator functions of the form |
| 8130 | // |
| 8131 | // LR operator?(bool, L, R); |
| 8132 | // |
| 8133 | // where LR is the result of the usual arithmetic conversions |
| 8134 | // between types L and R. |
| 8135 | // Our candidates ignore the first parameter. |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8136 | void addGenericBinaryArithmeticOverloads() { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8137 | if (!HasArithmeticOrEnumeralCandidateType) |
| 8138 | return; |
| 8139 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8140 | for (unsigned Left = FirstPromotedArithmeticType; |
| 8141 | Left < LastPromotedArithmeticType; ++Left) { |
| 8142 | for (unsigned Right = FirstPromotedArithmeticType; |
| 8143 | Right < LastPromotedArithmeticType; ++Right) { |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8144 | QualType LandR[2] = { ArithmeticTypes[Left], |
| 8145 | ArithmeticTypes[Right] }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8146 | S.AddBuiltinCandidate(LandR, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8147 | } |
| 8148 | } |
| 8149 | |
| 8150 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 8151 | // conditional operator for vector types. |
| 8152 | for (BuiltinCandidateTypeSet::iterator |
| 8153 | Vec1 = CandidateTypes[0].vector_begin(), |
| 8154 | Vec1End = CandidateTypes[0].vector_end(); |
| 8155 | Vec1 != Vec1End; ++Vec1) { |
| 8156 | for (BuiltinCandidateTypeSet::iterator |
| 8157 | Vec2 = CandidateTypes[1].vector_begin(), |
| 8158 | Vec2End = CandidateTypes[1].vector_end(); |
| 8159 | Vec2 != Vec2End; ++Vec2) { |
| 8160 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8161 | S.AddBuiltinCandidate(LandR, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8162 | } |
| 8163 | } |
| 8164 | } |
| 8165 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8166 | // C++2a [over.built]p14: |
| 8167 | // |
| 8168 | // For every integral type T there exists a candidate operator function |
| 8169 | // of the form |
| 8170 | // |
| 8171 | // std::strong_ordering operator<=>(T, T) |
| 8172 | // |
| 8173 | // C++2a [over.built]p15: |
| 8174 | // |
| 8175 | // For every pair of floating-point types L and R, there exists a candidate |
| 8176 | // operator function of the form |
| 8177 | // |
| 8178 | // std::partial_ordering operator<=>(L, R); |
| 8179 | // |
| 8180 | // FIXME: The current specification for integral types doesn't play nice with |
| 8181 | // the direction of p0946r0, which allows mixed integral and unscoped-enum |
| 8182 | // comparisons. Under the current spec this can lead to ambiguity during |
| 8183 | // overload resolution. For example: |
| 8184 | // |
| 8185 | // enum A : int {a}; |
| 8186 | // auto x = (a <=> (long)42); |
| 8187 | // |
| 8188 | // error: call is ambiguous for arguments 'A' and 'long'. |
| 8189 | // note: candidate operator<=>(int, int) |
| 8190 | // note: candidate operator<=>(long, long) |
| 8191 | // |
| 8192 | // To avoid this error, this function deviates from the specification and adds |
| 8193 | // the mixed overloads `operator<=>(L, R)` where L and R are promoted |
| 8194 | // arithmetic types (the same as the generic relational overloads). |
| 8195 | // |
| 8196 | // For now this function acts as a placeholder. |
| 8197 | void addThreeWayArithmeticOverloads() { |
| 8198 | addGenericBinaryArithmeticOverloads(); |
| 8199 | } |
| 8200 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8201 | // C++ [over.built]p17: |
| 8202 | // |
| 8203 | // For every pair of promoted integral types L and R, there |
| 8204 | // exist candidate operator functions of the form |
| 8205 | // |
| 8206 | // LR operator%(L, R); |
| 8207 | // LR operator&(L, R); |
| 8208 | // LR operator^(L, R); |
| 8209 | // LR operator|(L, R); |
| 8210 | // L operator<<(L, R); |
| 8211 | // L operator>>(L, R); |
| 8212 | // |
| 8213 | // where LR is the result of the usual arithmetic conversions |
| 8214 | // between types L and R. |
| 8215 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8216 | if (!HasArithmeticOrEnumeralCandidateType) |
| 8217 | return; |
| 8218 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8219 | for (unsigned Left = FirstPromotedIntegralType; |
| 8220 | Left < LastPromotedIntegralType; ++Left) { |
| 8221 | for (unsigned Right = FirstPromotedIntegralType; |
| 8222 | Right < LastPromotedIntegralType; ++Right) { |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8223 | QualType LandR[2] = { ArithmeticTypes[Left], |
| 8224 | ArithmeticTypes[Right] }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8225 | S.AddBuiltinCandidate(LandR, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8226 | } |
| 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | // C++ [over.built]p20: |
| 8231 | // |
| 8232 | // For every pair (T, VQ), where T is an enumeration or |
| 8233 | // pointer to member type and VQ is either volatile or |
| 8234 | // empty, there exist candidate operator functions of the form |
| 8235 | // |
| 8236 | // VQ T& operator=(VQ T&, T); |
| 8237 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 8238 | /// Set of (canonical) types that we've already handled. |
| 8239 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 8240 | |
| 8241 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 8242 | for (BuiltinCandidateTypeSet::iterator |
| 8243 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 8244 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 8245 | Enum != EnumEnd; ++Enum) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8246 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8247 | continue; |
| 8248 | |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8249 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | for (BuiltinCandidateTypeSet::iterator |
| 8253 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 8254 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 8255 | MemPtr != MemPtrEnd; ++MemPtr) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8256 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8257 | continue; |
| 8258 | |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8259 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8260 | } |
| 8261 | } |
| 8262 | } |
| 8263 | |
| 8264 | // C++ [over.built]p19: |
| 8265 | // |
| 8266 | // For every pair (T, VQ), where T is any type and VQ is either |
| 8267 | // volatile or empty, there exist candidate operator functions |
| 8268 | // of the form |
| 8269 | // |
| 8270 | // T*VQ& operator=(T*VQ&, T*); |
| 8271 | // |
| 8272 | // C++ [over.built]p21: |
| 8273 | // |
| 8274 | // For every pair (T, VQ), where T is a cv-qualified or |
| 8275 | // cv-unqualified object type and VQ is either volatile or |
| 8276 | // empty, there exist candidate operator functions of the form |
| 8277 | // |
| 8278 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 8279 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 8280 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 8281 | /// Set of (canonical) types that we've already handled. |
| 8282 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 8283 | |
| 8284 | for (BuiltinCandidateTypeSet::iterator |
| 8285 | Ptr = CandidateTypes[0].pointer_begin(), |
| 8286 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 8287 | Ptr != PtrEnd; ++Ptr) { |
| 8288 | // If this is operator=, keep track of the builtin candidates we added. |
| 8289 | if (isEqualOp) |
| 8290 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 8291 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 8292 | continue; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8293 | |
| 8294 | // non-volatile version |
| 8295 | QualType ParamTypes[2] = { |
| 8296 | S.Context.getLValueReferenceType(*Ptr), |
| 8297 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 8298 | }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8299 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8300 | /*IsAssigmentOperator=*/ isEqualOp); |
| 8301 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8302 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 8303 | VisibleTypeConversionsQuals.hasVolatile(); |
| 8304 | if (NeedVolatile) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8305 | // volatile version |
| 8306 | ParamTypes[0] = |
| 8307 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8308 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8309 | /*IsAssigmentOperator=*/isEqualOp); |
| 8310 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 8311 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8312 | if (!(*Ptr).isRestrictQualified() && |
| 8313 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 8314 | // restrict version |
| 8315 | ParamTypes[0] |
| 8316 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8317 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8318 | /*IsAssigmentOperator=*/isEqualOp); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 8319 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8320 | if (NeedVolatile) { |
| 8321 | // volatile restrict version |
| 8322 | ParamTypes[0] |
| 8323 | = S.Context.getLValueReferenceType( |
| 8324 | S.Context.getCVRQualifiedType(*Ptr, |
| 8325 | (Qualifiers::Volatile | |
| 8326 | Qualifiers::Restrict))); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8327 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8328 | /*IsAssigmentOperator=*/isEqualOp); |
| 8329 | } |
| 8330 | } |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
| 8333 | if (isEqualOp) { |
| 8334 | for (BuiltinCandidateTypeSet::iterator |
| 8335 | Ptr = CandidateTypes[1].pointer_begin(), |
| 8336 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 8337 | Ptr != PtrEnd; ++Ptr) { |
| 8338 | // Make sure we don't add the same candidate twice. |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8339 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8340 | continue; |
| 8341 | |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8342 | QualType ParamTypes[2] = { |
| 8343 | S.Context.getLValueReferenceType(*Ptr), |
| 8344 | *Ptr, |
| 8345 | }; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8346 | |
| 8347 | // non-volatile version |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8348 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8349 | /*IsAssigmentOperator=*/true); |
| 8350 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8351 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 8352 | VisibleTypeConversionsQuals.hasVolatile(); |
| 8353 | if (NeedVolatile) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8354 | // volatile version |
| 8355 | ParamTypes[0] = |
| 8356 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8357 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8358 | /*IsAssigmentOperator=*/true); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8359 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 8360 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8361 | if (!(*Ptr).isRestrictQualified() && |
| 8362 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 8363 | // restrict version |
| 8364 | ParamTypes[0] |
| 8365 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8366 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8367 | /*IsAssigmentOperator=*/true); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 8368 | |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8369 | if (NeedVolatile) { |
| 8370 | // volatile restrict version |
| 8371 | ParamTypes[0] |
| 8372 | = S.Context.getLValueReferenceType( |
| 8373 | S.Context.getCVRQualifiedType(*Ptr, |
| 8374 | (Qualifiers::Volatile | |
| 8375 | Qualifiers::Restrict))); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8376 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8377 | /*IsAssigmentOperator=*/true); |
| Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 8378 | } |
| 8379 | } |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8380 | } |
| 8381 | } |
| 8382 | } |
| 8383 | |
| 8384 | // C++ [over.built]p18: |
| 8385 | // |
| 8386 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 8387 | // VQ is either volatile or empty, and R is a promoted |
| 8388 | // arithmetic type, there exist candidate operator functions of |
| 8389 | // the form |
| 8390 | // |
| 8391 | // VQ L& operator=(VQ L&, R); |
| 8392 | // VQ L& operator*=(VQ L&, R); |
| 8393 | // VQ L& operator/=(VQ L&, R); |
| 8394 | // VQ L& operator+=(VQ L&, R); |
| 8395 | // VQ L& operator-=(VQ L&, R); |
| 8396 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8397 | if (!HasArithmeticOrEnumeralCandidateType) |
| 8398 | return; |
| 8399 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8400 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 8401 | for (unsigned Right = FirstPromotedArithmeticType; |
| 8402 | Right < LastPromotedArithmeticType; ++Right) { |
| 8403 | QualType ParamTypes[2]; |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8404 | ParamTypes[1] = ArithmeticTypes[Right]; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8405 | |
| 8406 | // Add this built-in operator as a candidate (VQ is empty). |
| 8407 | ParamTypes[0] = |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8408 | S.Context.getLValueReferenceType(ArithmeticTypes[Left]); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8409 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8410 | /*IsAssigmentOperator=*/isEqualOp); |
| 8411 | |
| 8412 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 8413 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 8414 | ParamTypes[0] = |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8415 | S.Context.getVolatileType(ArithmeticTypes[Left]); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8416 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8417 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8418 | /*IsAssigmentOperator=*/isEqualOp); |
| 8419 | } |
| 8420 | } |
| 8421 | } |
| 8422 | |
| 8423 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 8424 | for (BuiltinCandidateTypeSet::iterator |
| 8425 | Vec1 = CandidateTypes[0].vector_begin(), |
| 8426 | Vec1End = CandidateTypes[0].vector_end(); |
| 8427 | Vec1 != Vec1End; ++Vec1) { |
| 8428 | for (BuiltinCandidateTypeSet::iterator |
| 8429 | Vec2 = CandidateTypes[1].vector_begin(), |
| 8430 | Vec2End = CandidateTypes[1].vector_end(); |
| 8431 | Vec2 != Vec2End; ++Vec2) { |
| 8432 | QualType ParamTypes[2]; |
| 8433 | ParamTypes[1] = *Vec2; |
| 8434 | // Add this built-in operator as a candidate (VQ is empty). |
| 8435 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8436 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8437 | /*IsAssigmentOperator=*/isEqualOp); |
| 8438 | |
| 8439 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 8440 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 8441 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 8442 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8443 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8444 | /*IsAssigmentOperator=*/isEqualOp); |
| 8445 | } |
| 8446 | } |
| 8447 | } |
| 8448 | } |
| 8449 | |
| 8450 | // C++ [over.built]p22: |
| 8451 | // |
| 8452 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 8453 | // is either volatile or empty, and R is a promoted integral |
| 8454 | // type, there exist candidate operator functions of the form |
| 8455 | // |
| 8456 | // VQ L& operator%=(VQ L&, R); |
| 8457 | // VQ L& operator<<=(VQ L&, R); |
| 8458 | // VQ L& operator>>=(VQ L&, R); |
| 8459 | // VQ L& operator&=(VQ L&, R); |
| 8460 | // VQ L& operator^=(VQ L&, R); |
| 8461 | // VQ L& operator|=(VQ L&, R); |
| 8462 | void addAssignmentIntegralOverloads() { |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8463 | if (!HasArithmeticOrEnumeralCandidateType) |
| 8464 | return; |
| 8465 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8466 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 8467 | for (unsigned Right = FirstPromotedIntegralType; |
| 8468 | Right < LastPromotedIntegralType; ++Right) { |
| 8469 | QualType ParamTypes[2]; |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8470 | ParamTypes[1] = ArithmeticTypes[Right]; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8471 | |
| 8472 | // Add this built-in operator as a candidate (VQ is empty). |
| 8473 | ParamTypes[0] = |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8474 | S.Context.getLValueReferenceType(ArithmeticTypes[Left]); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8475 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8476 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 8477 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| Hans Wennborg | 8237141 | 2017-11-15 17:11:53 +0000 | [diff] [blame] | 8478 | ParamTypes[0] = ArithmeticTypes[Left]; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8479 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 8480 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8481 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8482 | } |
| 8483 | } |
| 8484 | } |
| 8485 | } |
| 8486 | |
| 8487 | // C++ [over.operator]p23: |
| 8488 | // |
| 8489 | // There also exist candidate operator functions of the form |
| 8490 | // |
| 8491 | // bool operator!(bool); |
| 8492 | // bool operator&&(bool, bool); |
| 8493 | // bool operator||(bool, bool); |
| 8494 | void addExclaimOverload() { |
| 8495 | QualType ParamTy = S.Context.BoolTy; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8496 | S.AddBuiltinCandidate(&ParamTy, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8497 | /*IsAssignmentOperator=*/false, |
| 8498 | /*NumContextualBoolArguments=*/1); |
| 8499 | } |
| 8500 | void addAmpAmpOrPipePipeOverload() { |
| 8501 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8502 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8503 | /*IsAssignmentOperator=*/false, |
| 8504 | /*NumContextualBoolArguments=*/2); |
| 8505 | } |
| 8506 | |
| 8507 | // C++ [over.built]p13: |
| 8508 | // |
| 8509 | // For every cv-qualified or cv-unqualified object type T there |
| 8510 | // exist candidate operator functions of the form |
| 8511 | // |
| 8512 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 8513 | // T& operator[](T*, ptrdiff_t); |
| 8514 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 8515 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 8516 | // T& operator[](ptrdiff_t, T*); |
| 8517 | void addSubscriptOverloads() { |
| 8518 | for (BuiltinCandidateTypeSet::iterator |
| 8519 | Ptr = CandidateTypes[0].pointer_begin(), |
| 8520 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 8521 | Ptr != PtrEnd; ++Ptr) { |
| 8522 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 8523 | QualType PointeeType = (*Ptr)->getPointeeType(); |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 8524 | if (!PointeeType->isObjectType()) |
| 8525 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8526 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8527 | // T& operator[](T*, ptrdiff_t) |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8528 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8529 | } |
| 8530 | |
| 8531 | for (BuiltinCandidateTypeSet::iterator |
| 8532 | Ptr = CandidateTypes[1].pointer_begin(), |
| 8533 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 8534 | Ptr != PtrEnd; ++Ptr) { |
| 8535 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 8536 | QualType PointeeType = (*Ptr)->getPointeeType(); |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 8537 | if (!PointeeType->isObjectType()) |
| 8538 | continue; |
| 8539 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8540 | // T& operator[](ptrdiff_t, T*) |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8541 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8542 | } |
| 8543 | } |
| 8544 | |
| 8545 | // C++ [over.built]p11: |
| 8546 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 8547 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 8548 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 8549 | // there exist candidate operator functions of the form |
| 8550 | // |
| 8551 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 8552 | // |
| 8553 | // where CV12 is the union of CV1 and CV2. |
| 8554 | void addArrowStarOverloads() { |
| 8555 | for (BuiltinCandidateTypeSet::iterator |
| 8556 | Ptr = CandidateTypes[0].pointer_begin(), |
| 8557 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 8558 | Ptr != PtrEnd; ++Ptr) { |
| 8559 | QualType C1Ty = (*Ptr); |
| 8560 | QualType C1; |
| 8561 | QualifierCollector Q1; |
| 8562 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 8563 | if (!isa<RecordType>(C1)) |
| 8564 | continue; |
| 8565 | // heuristic to reduce number of builtin candidates in the set. |
| 8566 | // Add volatile/restrict version only if there are conversions to a |
| 8567 | // volatile/restrict type. |
| 8568 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 8569 | continue; |
| 8570 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 8571 | continue; |
| 8572 | for (BuiltinCandidateTypeSet::iterator |
| 8573 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 8574 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 8575 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 8576 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 8577 | QualType C2 = QualType(mptr->getClass(), 0); |
| 8578 | C2 = C2.getUnqualifiedType(); |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 8579 | if (C1 != C2 && !S.IsDerivedFrom(CandidateSet.getLocation(), C1, C2)) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8580 | break; |
| 8581 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 8582 | // build CV12 T& |
| 8583 | QualType T = mptr->getPointeeType(); |
| 8584 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 8585 | T.isVolatileQualified()) |
| 8586 | continue; |
| 8587 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 8588 | T.isRestrictQualified()) |
| 8589 | continue; |
| 8590 | T = Q1.apply(S.Context, T); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8591 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8592 | } |
| 8593 | } |
| 8594 | } |
| 8595 | |
| 8596 | // Note that we don't consider the first argument, since it has been |
| 8597 | // contextually converted to bool long ago. The candidates below are |
| 8598 | // therefore added as binary. |
| 8599 | // |
| 8600 | // C++ [over.built]p25: |
| 8601 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 8602 | // enumeration type, there exist candidate operator functions of the form |
| 8603 | // |
| 8604 | // T operator?(bool, T, T); |
| 8605 | // |
| 8606 | void addConditionalOperatorOverloads() { |
| 8607 | /// Set of (canonical) types that we've already handled. |
| 8608 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 8609 | |
| 8610 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 8611 | for (BuiltinCandidateTypeSet::iterator |
| 8612 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 8613 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 8614 | Ptr != PtrEnd; ++Ptr) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8615 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8616 | continue; |
| 8617 | |
| 8618 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8619 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
| 8622 | for (BuiltinCandidateTypeSet::iterator |
| 8623 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 8624 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 8625 | MemPtr != MemPtrEnd; ++MemPtr) { |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8626 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8627 | continue; |
| 8628 | |
| 8629 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8630 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8633 | if (S.getLangOpts().CPlusPlus11) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8634 | for (BuiltinCandidateTypeSet::iterator |
| 8635 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 8636 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 8637 | Enum != EnumEnd; ++Enum) { |
| 8638 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 8639 | continue; |
| 8640 | |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 8641 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum)).second) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8642 | continue; |
| 8643 | |
| 8644 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8645 | S.AddBuiltinCandidate(ParamTypes, Args, CandidateSet); |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8646 | } |
| 8647 | } |
| 8648 | } |
| 8649 | } |
| 8650 | }; |
| 8651 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8652 | } // end anonymous namespace |
| 8653 | |
| 8654 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 8655 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 8656 | /// on the operator @p Op and the arguments given. For example, if the |
| 8657 | /// operator is a binary '+', this routine might add "int |
| 8658 | /// operator+(int, int)" to cover integer addition. |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 8659 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 8660 | SourceLocation OpLoc, |
| 8661 | ArrayRef<Expr *> Args, |
| 8662 | OverloadCandidateSet &CandidateSet) { |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8663 | // Find all of the types that the arguments can convert to, but only |
| 8664 | // if the operator we're looking at has built-in operator candidates |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8665 | // that make use of these types. Also record whether we encounter non-record |
| 8666 | // candidate types or either arithmetic or enumeral candidate types. |
| Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 8667 | Qualifiers VisibleTypeConversionsQuals; |
| 8668 | VisibleTypeConversionsQuals.addConst(); |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8669 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
| Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 8670 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8671 | |
| 8672 | bool HasNonRecordCandidateType = false; |
| 8673 | bool HasArithmeticOrEnumeralCandidateType = false; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8674 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8675 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Benjamin Kramer | 57dddd48 | 2015-02-17 21:55:18 +0000 | [diff] [blame] | 8676 | CandidateTypes.emplace_back(*this); |
| Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 8677 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 8678 | OpLoc, |
| 8679 | true, |
| 8680 | (Op == OO_Exclaim || |
| 8681 | Op == OO_AmpAmp || |
| 8682 | Op == OO_PipePipe), |
| 8683 | VisibleTypeConversionsQuals); |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8684 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 8685 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 8686 | HasArithmeticOrEnumeralCandidateType = |
| 8687 | HasArithmeticOrEnumeralCandidateType || |
| 8688 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
| Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 8689 | } |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8690 | |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8691 | // Exit early when no non-record types have been added to the candidate set |
| 8692 | // for any of the arguments to the operator. |
| Douglas Gregor | 877d4eb | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 8693 | // |
| 8694 | // We can't exit early for !, ||, or &&, since there we have always have |
| 8695 | // 'bool' overloads. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8696 | if (!HasNonRecordCandidateType && |
| Douglas Gregor | 877d4eb | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 8697 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8698 | return; |
| 8699 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8700 | // Setup an object to manage the common state for building overloads. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8701 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8702 | VisibleTypeConversionsQuals, |
| Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 8703 | HasArithmeticOrEnumeralCandidateType, |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8704 | CandidateTypes, CandidateSet); |
| 8705 | |
| 8706 | // Dispatch over the operation to add in only those overloads which apply. |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8707 | switch (Op) { |
| 8708 | case OO_None: |
| 8709 | case NUM_OVERLOADED_OPERATORS: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8710 | llvm_unreachable("Expected an overloaded operator"); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8711 | |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8712 | case OO_New: |
| 8713 | case OO_Delete: |
| 8714 | case OO_Array_New: |
| 8715 | case OO_Array_Delete: |
| 8716 | case OO_Call: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8717 | llvm_unreachable( |
| 8718 | "Special operators don't use AddBuiltinOperatorCandidates"); |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8719 | |
| 8720 | case OO_Comma: |
| 8721 | case OO_Arrow: |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 8722 | case OO_Coawait: |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8723 | // C++ [over.match.oper]p3: |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 8724 | // -- For the operator ',', the unary operator '&', the |
| 8725 | // operator '->', or the operator 'co_await', the |
| 8726 | // built-in candidates set is empty. |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8727 | break; |
| 8728 | |
| 8729 | case OO_Plus: // '+' is either unary or binary |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8730 | if (Args.size() == 1) |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8731 | OpBuilder.addUnaryPlusPointerOverloads(); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8732 | LLVM_FALLTHROUGH; |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8733 | |
| 8734 | case OO_Minus: // '-' is either unary or binary |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8735 | if (Args.size() == 1) { |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8736 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
| Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 8737 | } else { |
| 8738 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8739 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
| Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 8740 | } |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8741 | break; |
| 8742 | |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8743 | case OO_Star: // '*' is either unary or binary |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8744 | if (Args.size() == 1) |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8745 | OpBuilder.addUnaryStarPointerOverloads(); |
| 8746 | else |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8747 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8748 | break; |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8749 | |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8750 | case OO_Slash: |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8751 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
| Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 8752 | break; |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8753 | |
| 8754 | case OO_PlusPlus: |
| 8755 | case OO_MinusMinus: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8756 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 8757 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8758 | break; |
| 8759 | |
| Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 8760 | case OO_EqualEqual: |
| 8761 | case OO_ExclaimEqual: |
| Richard Smith | 5e9746f | 2016-10-21 22:00:42 +0000 | [diff] [blame] | 8762 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOrNullptrOverloads(); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8763 | LLVM_FALLTHROUGH; |
| Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 8764 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8765 | case OO_Less: |
| 8766 | case OO_Greater: |
| 8767 | case OO_LessEqual: |
| 8768 | case OO_GreaterEqual: |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8769 | OpBuilder.addGenericBinaryPointerOrEnumeralOverloads(); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8770 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
| Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 8771 | break; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8772 | |
| Richard Smith | d30b23d | 2017-12-01 02:13:10 +0000 | [diff] [blame] | 8773 | case OO_Spaceship: |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8774 | OpBuilder.addGenericBinaryPointerOrEnumeralOverloads(); |
| 8775 | OpBuilder.addThreeWayArithmeticOverloads(); |
| 8776 | break; |
| Richard Smith | d30b23d | 2017-12-01 02:13:10 +0000 | [diff] [blame] | 8777 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8778 | case OO_Percent: |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8779 | case OO_Caret: |
| 8780 | case OO_Pipe: |
| 8781 | case OO_LessLess: |
| 8782 | case OO_GreaterGreater: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8783 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8784 | break; |
| 8785 | |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8786 | case OO_Amp: // '&' is either unary or binary |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8787 | if (Args.size() == 1) |
| Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8788 | // C++ [over.match.oper]p3: |
| 8789 | // -- For the operator ',', the unary operator '&', or the |
| 8790 | // operator '->', the built-in candidates set is empty. |
| 8791 | break; |
| 8792 | |
| 8793 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 8794 | break; |
| 8795 | |
| 8796 | case OO_Tilde: |
| 8797 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 8798 | break; |
| 8799 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8800 | case OO_Equal: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8801 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8802 | LLVM_FALLTHROUGH; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8803 | |
| 8804 | case OO_PlusEqual: |
| 8805 | case OO_MinusEqual: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8806 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8807 | LLVM_FALLTHROUGH; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8808 | |
| 8809 | case OO_StarEqual: |
| 8810 | case OO_SlashEqual: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8811 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8812 | break; |
| 8813 | |
| 8814 | case OO_PercentEqual: |
| 8815 | case OO_LessLessEqual: |
| 8816 | case OO_GreaterGreaterEqual: |
| 8817 | case OO_AmpEqual: |
| 8818 | case OO_CaretEqual: |
| 8819 | case OO_PipeEqual: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8820 | OpBuilder.addAssignmentIntegralOverloads(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8821 | break; |
| 8822 | |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8823 | case OO_Exclaim: |
| 8824 | OpBuilder.addExclaimOverload(); |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8825 | break; |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8826 | |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8827 | case OO_AmpAmp: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8828 | case OO_PipePipe: |
| 8829 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8830 | break; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8831 | |
| 8832 | case OO_Subscript: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8833 | OpBuilder.addSubscriptOverloads(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8834 | break; |
| 8835 | |
| 8836 | case OO_ArrowStar: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8837 | OpBuilder.addArrowStarOverloads(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8838 | break; |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8839 | |
| 8840 | case OO_Conditional: |
| Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8841 | OpBuilder.addConditionalOperatorOverloads(); |
| George Burgess IV | c07c389 | 2017-06-08 18:19:25 +0000 | [diff] [blame] | 8842 | OpBuilder.addGenericBinaryArithmeticOverloads(); |
| Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 8843 | break; |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8844 | } |
| 8845 | } |
| 8846 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8847 | /// Add function candidates found via argument-dependent lookup |
| Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8848 | /// to the set of overloading candidates. |
| 8849 | /// |
| 8850 | /// This routine performs argument-dependent name lookup based on the |
| 8851 | /// given function name (which may also be an operator name) and adds |
| 8852 | /// all of the overload candidates found by ADL to the overload |
| 8853 | /// candidate set (C++ [basic.lookup.argdep]). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8854 | void |
| Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8855 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 8856 | SourceLocation Loc, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8857 | ArrayRef<Expr *> Args, |
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8858 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8859 | OverloadCandidateSet& CandidateSet, |
| Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 8860 | bool PartialOverloading) { |
| John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8861 | ADLResult Fns; |
| Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8862 | |
| John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 8863 | // FIXME: This approach for uniquing ADL results (and removing |
| 8864 | // redundant candidates from the set) relies on pointer-equality, |
| 8865 | // which means we need to key off the canonical decl. However, |
| 8866 | // always going back to the canonical decl might not get us the |
| 8867 | // right set of default arguments. What default arguments are |
| 8868 | // we supposed to consider on ADL candidates, anyway? |
| 8869 | |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8870 | // FIXME: Pass in the explicit template arguments? |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 8871 | ArgumentDependentLookup(Name, Loc, Args, Fns); |
| Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8872 | |
| Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8873 | // Erase all of the candidates we already knew about. |
| Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8874 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 8875 | CandEnd = CandidateSet.end(); |
| 8876 | Cand != CandEnd; ++Cand) |
| Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8877 | if (Cand->Function) { |
| John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8878 | Fns.erase(Cand->Function); |
| Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8879 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8880 | Fns.erase(FunTmpl); |
| Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8881 | } |
| Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8882 | |
| 8883 | // For each of the ADL candidates we found, add it to the overload |
| 8884 | // set. |
| John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8885 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8886 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8887 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8888 | if (ExplicitTemplateArgs) |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8889 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8890 | |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8891 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 8892 | PartialOverloading); |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8893 | } else |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8894 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8895 | FoundDecl, ExplicitTemplateArgs, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 8896 | Args, CandidateSet, PartialOverloading); |
| Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8897 | } |
| Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
| George Burgess IV | 3dc16691 | 2016-05-10 01:59:34 +0000 | [diff] [blame] | 8900 | namespace { |
| 8901 | enum class Comparison { Equal, Better, Worse }; |
| 8902 | } |
| 8903 | |
| 8904 | /// Compares the enable_if attributes of two FunctionDecls, for the purposes of |
| 8905 | /// overload resolution. |
| 8906 | /// |
| 8907 | /// Cand1's set of enable_if attributes are said to be "better" than Cand2's iff |
| 8908 | /// Cand1's first N enable_if attributes have precisely the same conditions as |
| 8909 | /// Cand2's first N enable_if attributes (where N = the number of enable_if |
| 8910 | /// attributes on Cand2), and Cand1 has more than N enable_if attributes. |
| 8911 | /// |
| 8912 | /// Note that you can have a pair of candidates such that Cand1's enable_if |
| 8913 | /// attributes are worse than Cand2's, and Cand2's enable_if attributes are |
| 8914 | /// worse than Cand1's. |
| 8915 | static Comparison compareEnableIfAttrs(const Sema &S, const FunctionDecl *Cand1, |
| 8916 | const FunctionDecl *Cand2) { |
| 8917 | // Common case: One (or both) decls don't have enable_if attrs. |
| 8918 | bool Cand1Attr = Cand1->hasAttr<EnableIfAttr>(); |
| 8919 | bool Cand2Attr = Cand2->hasAttr<EnableIfAttr>(); |
| 8920 | if (!Cand1Attr || !Cand2Attr) { |
| 8921 | if (Cand1Attr == Cand2Attr) |
| 8922 | return Comparison::Equal; |
| 8923 | return Cand1Attr ? Comparison::Better : Comparison::Worse; |
| 8924 | } |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 8925 | |
| Michael Kruse | ea31f0e | 2018-06-19 23:46:52 +0000 | [diff] [blame] | 8926 | auto Cand1Attrs = Cand1->specific_attrs<EnableIfAttr>(); |
| 8927 | auto Cand2Attrs = Cand2->specific_attrs<EnableIfAttr>(); |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 8928 | |
| 8929 | auto Cand1I = Cand1Attrs.begin(); |
| 8930 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; |
| Michael Kruse | ea31f0e | 2018-06-19 23:46:52 +0000 | [diff] [blame] | 8931 | for (auto Cand2A : Cand2Attrs) { |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 8932 | Cand1ID.clear(); |
| 8933 | Cand2ID.clear(); |
| 8934 | |
| Michael Kruse | ea31f0e | 2018-06-19 23:46:52 +0000 | [diff] [blame] | 8935 | // It's impossible for Cand1 to be better than (or equal to) Cand2 if Cand1 |
| 8936 | // has fewer enable_if attributes than Cand2. |
| 8937 | if (Cand1I == Cand1Attrs.end()) |
| 8938 | return Comparison::Worse; |
| 8939 | auto Cand1A = Cand1I++; |
| 8940 | |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 8941 | Cand1A->getCond()->Profile(Cand1ID, S.getASTContext(), true); |
| 8942 | Cand2A->getCond()->Profile(Cand2ID, S.getASTContext(), true); |
| 8943 | if (Cand1ID != Cand2ID) |
| George Burgess IV | 3dc16691 | 2016-05-10 01:59:34 +0000 | [diff] [blame] | 8944 | return Comparison::Worse; |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 8945 | } |
| 8946 | |
| George Burgess IV | 3dc16691 | 2016-05-10 01:59:34 +0000 | [diff] [blame] | 8947 | return Cand1I == Cand1Attrs.end() ? Comparison::Equal : Comparison::Better; |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8950 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 8951 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 8952 | bool clang::isBetterOverloadCandidate( |
| 8953 | Sema &S, const OverloadCandidate &Cand1, const OverloadCandidate &Cand2, |
| 8954 | SourceLocation Loc, OverloadCandidateSet::CandidateSetKind Kind) { |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8955 | // Define viable functions to be better candidates than non-viable |
| 8956 | // functions. |
| 8957 | if (!Cand2.Viable) |
| 8958 | return Cand1.Viable; |
| 8959 | else if (!Cand1.Viable) |
| 8960 | return false; |
| 8961 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8962 | // C++ [over.match.best]p1: |
| 8963 | // |
| 8964 | // -- if F is a static member function, ICS1(F) is defined such |
| 8965 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 8966 | // any function G, and, symmetrically, ICS1(G) is neither |
| 8967 | // better nor worse than ICS1(F). |
| 8968 | unsigned StartArg = 0; |
| 8969 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 8970 | StartArg = 1; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8971 | |
| George Burgess IV | fbad5b2 | 2016-09-07 20:03:19 +0000 | [diff] [blame] | 8972 | auto IsIllFormedConversion = [&](const ImplicitConversionSequence &ICS) { |
| 8973 | // We don't allow incompatible pointer conversions in C++. |
| 8974 | if (!S.getLangOpts().CPlusPlus) |
| 8975 | return ICS.isStandard() && |
| 8976 | ICS.Standard.Second == ICK_Incompatible_Pointer_Conversion; |
| 8977 | |
| 8978 | // The only ill-formed conversion we allow in C++ is the string literal to |
| 8979 | // char* conversion, which is only considered ill-formed after C++11. |
| 8980 | return S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
| 8981 | hasDeprecatedStringLiteralToCharPtrConversion(ICS); |
| 8982 | }; |
| 8983 | |
| 8984 | // Define functions that don't require ill-formed conversions for a given |
| 8985 | // argument to be better candidates than functions that do. |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 8986 | unsigned NumArgs = Cand1.Conversions.size(); |
| 8987 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| George Burgess IV | fbad5b2 | 2016-09-07 20:03:19 +0000 | [diff] [blame] | 8988 | bool HasBetterConversion = false; |
| 8989 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
| 8990 | bool Cand1Bad = IsIllFormedConversion(Cand1.Conversions[ArgIdx]); |
| 8991 | bool Cand2Bad = IsIllFormedConversion(Cand2.Conversions[ArgIdx]); |
| 8992 | if (Cand1Bad != Cand2Bad) { |
| 8993 | if (Cand1Bad) |
| 8994 | return false; |
| 8995 | HasBetterConversion = true; |
| 8996 | } |
| 8997 | } |
| 8998 | |
| 8999 | if (HasBetterConversion) |
| 9000 | return true; |
| 9001 | |
| Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 9002 | // C++ [over.match.best]p1: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9003 | // A viable function F1 is defined to be a better function than another |
| 9004 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
| Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 9005 | // conversion sequence than ICSi(F2), and then... |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9006 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 9007 | switch (CompareImplicitConversionSequences(S, Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9008 | Cand1.Conversions[ArgIdx], |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9009 | Cand2.Conversions[ArgIdx])) { |
| 9010 | case ImplicitConversionSequence::Better: |
| 9011 | // Cand1 has a better conversion sequence. |
| 9012 | HasBetterConversion = true; |
| 9013 | break; |
| 9014 | |
| 9015 | case ImplicitConversionSequence::Worse: |
| 9016 | // Cand1 can't be better than Cand2. |
| 9017 | return false; |
| 9018 | |
| 9019 | case ImplicitConversionSequence::Indistinguishable: |
| 9020 | // Do nothing. |
| 9021 | break; |
| 9022 | } |
| 9023 | } |
| 9024 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9025 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
| Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 9026 | // ICSj(F2), or, if not that, |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9027 | if (HasBetterConversion) |
| 9028 | return true; |
| 9029 | |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 9030 | // -- the context is an initialization by user-defined conversion |
| 9031 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 9032 | // from the return type of F1 to the destination type (i.e., |
| 9033 | // the type of the entity being initialized) is a better |
| 9034 | // conversion sequence than the standard conversion sequence |
| 9035 | // from the return type of F2 to the destination type. |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9036 | if (Kind == OverloadCandidateSet::CSK_InitByUserDefinedConversion && |
| 9037 | Cand1.Function && Cand2.Function && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9038 | isa<CXXConversionDecl>(Cand1.Function) && |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 9039 | isa<CXXConversionDecl>(Cand2.Function)) { |
| Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 9040 | // First check whether we prefer one of the conversion functions over the |
| 9041 | // other. This only distinguishes the results in non-standard, extension |
| 9042 | // cases such as the conversion from a lambda closure type to a function |
| 9043 | // pointer or block. |
| Richard Smith | ec2748a | 2014-05-17 04:36:39 +0000 | [diff] [blame] | 9044 | ImplicitConversionSequence::CompareKind Result = |
| 9045 | compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 9046 | if (Result == ImplicitConversionSequence::Indistinguishable) |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 9047 | Result = CompareStandardConversionSequences(S, Loc, |
| Richard Smith | ec2748a | 2014-05-17 04:36:39 +0000 | [diff] [blame] | 9048 | Cand1.FinalConversion, |
| 9049 | Cand2.FinalConversion); |
| Richard Smith | 6fdeaab | 2014-05-17 01:58:45 +0000 | [diff] [blame] | 9050 | |
| Richard Smith | ec2748a | 2014-05-17 04:36:39 +0000 | [diff] [blame] | 9051 | if (Result != ImplicitConversionSequence::Indistinguishable) |
| 9052 | return Result == ImplicitConversionSequence::Better; |
| Richard Smith | 6fdeaab | 2014-05-17 01:58:45 +0000 | [diff] [blame] | 9053 | |
| 9054 | // FIXME: Compare kind of reference binding if conversion functions |
| 9055 | // convert to a reference type used in direct reference binding, per |
| 9056 | // C++14 [over.match.best]p1 section 2 bullet 3. |
| 9057 | } |
| 9058 | |
| Richard Smith | 5173136 | 2017-11-01 01:37:11 +0000 | [diff] [blame] | 9059 | // FIXME: Work around a defect in the C++17 guaranteed copy elision wording, |
| 9060 | // as combined with the resolution to CWG issue 243. |
| 9061 | // |
| 9062 | // When the context is initialization by constructor ([over.match.ctor] or |
| 9063 | // either phase of [over.match.list]), a constructor is preferred over |
| 9064 | // a conversion function. |
| 9065 | if (Kind == OverloadCandidateSet::CSK_InitByConstructor && NumArgs == 1 && |
| 9066 | Cand1.Function && Cand2.Function && |
| 9067 | isa<CXXConstructorDecl>(Cand1.Function) != |
| 9068 | isa<CXXConstructorDecl>(Cand2.Function)) |
| 9069 | return isa<CXXConstructorDecl>(Cand1.Function); |
| 9070 | |
| Richard Smith | 6fdeaab | 2014-05-17 01:58:45 +0000 | [diff] [blame] | 9071 | // -- F1 is a non-template function and F2 is a function template |
| 9072 | // specialization, or, if not that, |
| 9073 | bool Cand1IsSpecialization = Cand1.Function && |
| 9074 | Cand1.Function->getPrimaryTemplate(); |
| 9075 | bool Cand2IsSpecialization = Cand2.Function && |
| 9076 | Cand2.Function->getPrimaryTemplate(); |
| 9077 | if (Cand1IsSpecialization != Cand2IsSpecialization) |
| 9078 | return Cand2IsSpecialization; |
| 9079 | |
| 9080 | // -- F1 and F2 are function template specializations, and the function |
| 9081 | // template for F1 is more specialized than the template for F2 |
| 9082 | // according to the partial ordering rules described in 14.5.5.2, or, |
| 9083 | // if not that, |
| 9084 | if (Cand1IsSpecialization && Cand2IsSpecialization) { |
| 9085 | if (FunctionTemplateDecl *BetterTemplate |
| 9086 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 9087 | Cand2.Function->getPrimaryTemplate(), |
| 9088 | Loc, |
| 9089 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 9090 | : TPOC_Call, |
| 9091 | Cand1.ExplicitCallArguments, |
| 9092 | Cand2.ExplicitCallArguments)) |
| 9093 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
| Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9096 | // FIXME: Work around a defect in the C++17 inheriting constructor wording. |
| 9097 | // A derived-class constructor beats an (inherited) base class constructor. |
| 9098 | bool Cand1IsInherited = |
| 9099 | dyn_cast_or_null<ConstructorUsingShadowDecl>(Cand1.FoundDecl.getDecl()); |
| 9100 | bool Cand2IsInherited = |
| 9101 | dyn_cast_or_null<ConstructorUsingShadowDecl>(Cand2.FoundDecl.getDecl()); |
| 9102 | if (Cand1IsInherited != Cand2IsInherited) |
| 9103 | return Cand2IsInherited; |
| 9104 | else if (Cand1IsInherited) { |
| 9105 | assert(Cand2IsInherited); |
| 9106 | auto *Cand1Class = cast<CXXRecordDecl>(Cand1.Function->getDeclContext()); |
| 9107 | auto *Cand2Class = cast<CXXRecordDecl>(Cand2.Function->getDeclContext()); |
| 9108 | if (Cand1Class->isDerivedFrom(Cand2Class)) |
| 9109 | return true; |
| 9110 | if (Cand2Class->isDerivedFrom(Cand1Class)) |
| 9111 | return false; |
| 9112 | // Inherited from sibling base classes: still ambiguous. |
| 9113 | } |
| 9114 | |
| Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 9115 | // Check C++17 tie-breakers for deduction guides. |
| 9116 | { |
| 9117 | auto *Guide1 = dyn_cast_or_null<CXXDeductionGuideDecl>(Cand1.Function); |
| 9118 | auto *Guide2 = dyn_cast_or_null<CXXDeductionGuideDecl>(Cand2.Function); |
| 9119 | if (Guide1 && Guide2) { |
| 9120 | // -- F1 is generated from a deduction-guide and F2 is not |
| 9121 | if (Guide1->isImplicit() != Guide2->isImplicit()) |
| 9122 | return Guide2->isImplicit(); |
| 9123 | |
| 9124 | // -- F1 is the copy deduction candidate(16.3.1.8) and F2 is not |
| 9125 | if (Guide1->isCopyDeductionCandidate()) |
| 9126 | return true; |
| 9127 | } |
| 9128 | } |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9129 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9130 | // Check for enable_if value-based overload resolution. |
| George Burgess IV | 3dc16691 | 2016-05-10 01:59:34 +0000 | [diff] [blame] | 9131 | if (Cand1.Function && Cand2.Function) { |
| 9132 | Comparison Cmp = compareEnableIfAttrs(S, Cand1.Function, Cand2.Function); |
| 9133 | if (Cmp != Comparison::Equal) |
| 9134 | return Cmp == Comparison::Better; |
| 9135 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9136 | |
| Justin Lebar | 25c4a81 | 2016-03-29 16:24:16 +0000 | [diff] [blame] | 9137 | if (S.getLangOpts().CUDA && Cand1.Function && Cand2.Function) { |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 9138 | FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext); |
| 9139 | return S.IdentifyCUDAPreference(Caller, Cand1.Function) > |
| 9140 | S.IdentifyCUDAPreference(Caller, Cand2.Function); |
| 9141 | } |
| 9142 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9143 | bool HasPS1 = Cand1.Function != nullptr && |
| 9144 | functionHasPassObjectSizeParams(Cand1.Function); |
| 9145 | bool HasPS2 = Cand2.Function != nullptr && |
| 9146 | functionHasPassObjectSizeParams(Cand2.Function); |
| 9147 | return HasPS1 != HasPS2 && HasPS1; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9148 | } |
| 9149 | |
| Richard Smith | 2dbe404 | 2015-11-04 19:26:32 +0000 | [diff] [blame] | 9150 | /// Determine whether two declarations are "equivalent" for the purposes of |
| Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 9151 | /// name lookup and overload resolution. This applies when the same internal/no |
| 9152 | /// linkage entity is defined by two modules (probably by textually including |
| Richard Smith | 2dbe404 | 2015-11-04 19:26:32 +0000 | [diff] [blame] | 9153 | /// the same header). In such a case, we don't consider the declarations to |
| 9154 | /// declare the same entity, but we also don't want lookups with both |
| 9155 | /// declarations visible to be ambiguous in some cases (this happens when using |
| 9156 | /// a modularized libstdc++). |
| 9157 | bool Sema::isEquivalentInternalLinkageDeclaration(const NamedDecl *A, |
| 9158 | const NamedDecl *B) { |
| Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 9159 | auto *VA = dyn_cast_or_null<ValueDecl>(A); |
| 9160 | auto *VB = dyn_cast_or_null<ValueDecl>(B); |
| 9161 | if (!VA || !VB) |
| 9162 | return false; |
| 9163 | |
| 9164 | // The declarations must be declaring the same name as an internal linkage |
| 9165 | // entity in different modules. |
| 9166 | if (!VA->getDeclContext()->getRedeclContext()->Equals( |
| 9167 | VB->getDeclContext()->getRedeclContext()) || |
| 9168 | getOwningModule(const_cast<ValueDecl *>(VA)) == |
| 9169 | getOwningModule(const_cast<ValueDecl *>(VB)) || |
| 9170 | VA->isExternallyVisible() || VB->isExternallyVisible()) |
| 9171 | return false; |
| 9172 | |
| 9173 | // Check that the declarations appear to be equivalent. |
| 9174 | // |
| 9175 | // FIXME: Checking the type isn't really enough to resolve the ambiguity. |
| 9176 | // For constants and functions, we should check the initializer or body is |
| 9177 | // the same. For non-constant variables, we shouldn't allow it at all. |
| 9178 | if (Context.hasSameType(VA->getType(), VB->getType())) |
| 9179 | return true; |
| 9180 | |
| 9181 | // Enum constants within unnamed enumerations will have different types, but |
| 9182 | // may still be similar enough to be interchangeable for our purposes. |
| 9183 | if (auto *EA = dyn_cast<EnumConstantDecl>(VA)) { |
| 9184 | if (auto *EB = dyn_cast<EnumConstantDecl>(VB)) { |
| 9185 | // Only handle anonymous enums. If the enumerations were named and |
| 9186 | // equivalent, they would have been merged to the same type. |
| 9187 | auto *EnumA = cast<EnumDecl>(EA->getDeclContext()); |
| 9188 | auto *EnumB = cast<EnumDecl>(EB->getDeclContext()); |
| 9189 | if (EnumA->hasNameForLinkage() || EnumB->hasNameForLinkage() || |
| 9190 | !Context.hasSameType(EnumA->getIntegerType(), |
| 9191 | EnumB->getIntegerType())) |
| 9192 | return false; |
| 9193 | // Allow this only if the value is the same for both enumerators. |
| 9194 | return llvm::APSInt::isSameValue(EA->getInitVal(), EB->getInitVal()); |
| 9195 | } |
| 9196 | } |
| 9197 | |
| 9198 | // Nothing else is sufficiently similar. |
| 9199 | return false; |
| Richard Smith | 2dbe404 | 2015-11-04 19:26:32 +0000 | [diff] [blame] | 9200 | } |
| 9201 | |
| 9202 | void Sema::diagnoseEquivalentInternalLinkageDeclarations( |
| 9203 | SourceLocation Loc, const NamedDecl *D, ArrayRef<const NamedDecl *> Equiv) { |
| 9204 | Diag(Loc, diag::ext_equivalent_internal_linkage_decl_in_modules) << D; |
| 9205 | |
| 9206 | Module *M = getOwningModule(const_cast<NamedDecl*>(D)); |
| 9207 | Diag(D->getLocation(), diag::note_equivalent_internal_linkage_decl) |
| 9208 | << !M << (M ? M->getFullModuleName() : ""); |
| 9209 | |
| 9210 | for (auto *E : Equiv) { |
| 9211 | Module *M = getOwningModule(const_cast<NamedDecl*>(E)); |
| 9212 | Diag(E->getLocation(), diag::note_equivalent_internal_linkage_decl) |
| 9213 | << !M << (M ? M->getFullModuleName() : ""); |
| 9214 | } |
| Richard Smith | 896c66e | 2015-10-21 07:13:52 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9217 | /// Computes the best viable function (C++ 13.3.3) |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9218 | /// within an overload candidate set. |
| 9219 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 9220 | /// \param Loc The location of the function name (or operator symbol) for |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9221 | /// which overload resolution occurs. |
| 9222 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 9223 | /// \param Best If overload resolution was successful or found a deleted |
| 9224 | /// function, \p Best points to the candidate function found. |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9225 | /// |
| 9226 | /// \returns The result of overload resolution. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9227 | OverloadingResult |
| 9228 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9229 | iterator &Best) { |
| Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 9230 | llvm::SmallVector<OverloadCandidate *, 16> Candidates; |
| 9231 | std::transform(begin(), end(), std::back_inserter(Candidates), |
| 9232 | [](OverloadCandidate &Cand) { return &Cand; }); |
| 9233 | |
| Justin Lebar | 66a2ab9 | 2016-08-10 00:40:43 +0000 | [diff] [blame] | 9234 | // [CUDA] HD->H or HD->D calls are technically not allowed by CUDA but |
| 9235 | // are accepted by both clang and NVCC. However, during a particular |
| Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 9236 | // compilation mode only one call variant is viable. We need to |
| 9237 | // exclude non-viable overload candidates from consideration based |
| 9238 | // only on their host/device attributes. Specifically, if one |
| 9239 | // candidate call is WrongSide and the other is SameSide, we ignore |
| 9240 | // the WrongSide candidate. |
| Justin Lebar | 25c4a81 | 2016-03-29 16:24:16 +0000 | [diff] [blame] | 9241 | if (S.getLangOpts().CUDA) { |
| Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 9242 | const FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext); |
| 9243 | bool ContainsSameSideCandidate = |
| 9244 | llvm::any_of(Candidates, [&](OverloadCandidate *Cand) { |
| 9245 | return Cand->Function && |
| 9246 | S.IdentifyCUDAPreference(Caller, Cand->Function) == |
| 9247 | Sema::CFP_SameSide; |
| 9248 | }); |
| 9249 | if (ContainsSameSideCandidate) { |
| 9250 | auto IsWrongSideCandidate = [&](OverloadCandidate *Cand) { |
| 9251 | return Cand->Function && |
| 9252 | S.IdentifyCUDAPreference(Caller, Cand->Function) == |
| 9253 | Sema::CFP_WrongSide; |
| 9254 | }; |
| George Burgess IV | 8684b03 | 2017-01-04 19:16:29 +0000 | [diff] [blame] | 9255 | llvm::erase_if(Candidates, IsWrongSideCandidate); |
| Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 9256 | } |
| 9257 | } |
| 9258 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9259 | // Find the best viable function. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9260 | Best = end(); |
| Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 9261 | for (auto *Cand : Candidates) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9262 | if (Cand->Viable) |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9263 | if (Best == end() || |
| 9264 | isBetterOverloadCandidate(S, *Cand, *Best, Loc, Kind)) |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9265 | Best = Cand; |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9266 | |
| 9267 | // If we didn't find any viable functions, abort. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9268 | if (Best == end()) |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9269 | return OR_No_Viable_Function; |
| 9270 | |
| Richard Smith | 2dbe404 | 2015-11-04 19:26:32 +0000 | [diff] [blame] | 9271 | llvm::SmallVector<const NamedDecl *, 4> EquivalentCands; |
| Richard Smith | 896c66e | 2015-10-21 07:13:52 +0000 | [diff] [blame] | 9272 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9273 | // Make sure that this function is better than every other viable |
| 9274 | // function. If not, we have an ambiguity. |
| Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 9275 | for (auto *Cand : Candidates) { |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 9276 | if (Cand->Viable && Cand != Best && |
| 9277 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, Kind)) { |
| Richard Smith | 2dbe404 | 2015-11-04 19:26:32 +0000 | [diff] [blame] | 9278 | if (S.isEquivalentInternalLinkageDeclaration(Best->Function, |
| 9279 | Cand->Function)) { |
| 9280 | EquivalentCands.push_back(Cand->Function); |
| Richard Smith | 896c66e | 2015-10-21 07:13:52 +0000 | [diff] [blame] | 9281 | continue; |
| 9282 | } |
| 9283 | |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9284 | Best = end(); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9285 | return OR_Ambiguous; |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9286 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9287 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9288 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9289 | // Best is the best viable function. |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9290 | if (Best->Function && |
| Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 9291 | (Best->Function->isDeleted() || |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 9292 | S.isFunctionConsideredUnavailable(Best->Function))) |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9293 | return OR_Deleted; |
| 9294 | |
| Richard Smith | 2dbe404 | 2015-11-04 19:26:32 +0000 | [diff] [blame] | 9295 | if (!EquivalentCands.empty()) |
| 9296 | S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function, |
| 9297 | EquivalentCands); |
| Richard Smith | 896c66e | 2015-10-21 07:13:52 +0000 | [diff] [blame] | 9298 | |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9299 | return OR_Success; |
| 9300 | } |
| 9301 | |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9302 | namespace { |
| 9303 | |
| 9304 | enum OverloadCandidateKind { |
| 9305 | oc_function, |
| 9306 | oc_method, |
| 9307 | oc_constructor, |
| 9308 | oc_implicit_default_constructor, |
| 9309 | oc_implicit_copy_constructor, |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9310 | oc_implicit_move_constructor, |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9311 | oc_implicit_copy_assignment, |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9312 | oc_implicit_move_assignment, |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9313 | oc_inherited_constructor |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9314 | }; |
| 9315 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9316 | enum OverloadCandidateSelect { |
| 9317 | ocs_non_template, |
| 9318 | ocs_template, |
| 9319 | ocs_described_template, |
| 9320 | }; |
| 9321 | |
| 9322 | static std::pair<OverloadCandidateKind, OverloadCandidateSelect> |
| George Burgess IV | d66d37c | 2016-10-28 21:42:06 +0000 | [diff] [blame] | 9323 | ClassifyOverloadCandidate(Sema &S, NamedDecl *Found, FunctionDecl *Fn, |
| 9324 | std::string &Description) { |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9325 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9326 | bool isTemplate = Fn->isTemplateDecl() || Found->isTemplateDecl(); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9327 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 9328 | isTemplate = true; |
| 9329 | Description = S.getTemplateArgumentBindingsText( |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9330 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9331 | } |
| John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 9332 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9333 | OverloadCandidateSelect Select = [&]() { |
| 9334 | if (!Description.empty()) |
| 9335 | return ocs_described_template; |
| 9336 | return isTemplate ? ocs_template : ocs_non_template; |
| 9337 | }(); |
| 9338 | |
| 9339 | OverloadCandidateKind Kind = [&]() { |
| 9340 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
| 9341 | if (!Ctor->isImplicit()) { |
| 9342 | if (isa<ConstructorUsingShadowDecl>(Found)) |
| 9343 | return oc_inherited_constructor; |
| 9344 | else |
| 9345 | return oc_constructor; |
| 9346 | } |
| 9347 | |
| 9348 | if (Ctor->isDefaultConstructor()) |
| 9349 | return oc_implicit_default_constructor; |
| 9350 | |
| 9351 | if (Ctor->isMoveConstructor()) |
| 9352 | return oc_implicit_move_constructor; |
| 9353 | |
| 9354 | assert(Ctor->isCopyConstructor() && |
| 9355 | "unexpected sort of implicit constructor"); |
| 9356 | return oc_implicit_copy_constructor; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9357 | } |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9358 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9359 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 9360 | // This actually gets spelled 'candidate function' for now, but |
| 9361 | // it doesn't hurt to split it out. |
| 9362 | if (!Meth->isImplicit()) |
| 9363 | return oc_method; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9364 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9365 | if (Meth->isMoveAssignmentOperator()) |
| 9366 | return oc_implicit_move_assignment; |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9367 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9368 | if (Meth->isCopyAssignmentOperator()) |
| 9369 | return oc_implicit_copy_assignment; |
| John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 9370 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9371 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 9372 | return oc_method; |
| 9373 | } |
| John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 9374 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9375 | return oc_function; |
| 9376 | }(); |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9377 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9378 | return std::make_pair(Kind, Select); |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9379 | } |
| 9380 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9381 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *FoundDecl) { |
| 9382 | // FIXME: It'd be nice to only emit a note once per using-decl per overload |
| 9383 | // set. |
| 9384 | if (auto *Shadow = dyn_cast<ConstructorUsingShadowDecl>(FoundDecl)) |
| 9385 | S.Diag(FoundDecl->getLocation(), |
| 9386 | diag::note_ovl_candidate_inherited_constructor) |
| 9387 | << Shadow->getNominatedBaseClass(); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9388 | } |
| 9389 | |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9390 | } // end anonymous namespace |
| 9391 | |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 9392 | static bool isFunctionAlwaysEnabled(const ASTContext &Ctx, |
| 9393 | const FunctionDecl *FD) { |
| 9394 | for (auto *EnableIf : FD->specific_attrs<EnableIfAttr>()) { |
| 9395 | bool AlwaysTrue; |
| 9396 | if (!EnableIf->getCond()->EvaluateAsBooleanCondition(AlwaysTrue, Ctx)) |
| 9397 | return false; |
| 9398 | if (!AlwaysTrue) |
| 9399 | return false; |
| 9400 | } |
| 9401 | return true; |
| 9402 | } |
| 9403 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9404 | /// Returns true if we can take the address of the function. |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9405 | /// |
| 9406 | /// \param Complain - If true, we'll emit a diagnostic |
| 9407 | /// \param InOverloadResolution - For the purposes of emitting a diagnostic, are |
| 9408 | /// we in overload resolution? |
| 9409 | /// \param Loc - The location of the statement we're complaining about. Ignored |
| 9410 | /// if we're not complaining, or if we're in overload resolution. |
| 9411 | static bool checkAddressOfFunctionIsAvailable(Sema &S, const FunctionDecl *FD, |
| 9412 | bool Complain, |
| 9413 | bool InOverloadResolution, |
| 9414 | SourceLocation Loc) { |
| 9415 | if (!isFunctionAlwaysEnabled(S.Context, FD)) { |
| 9416 | if (Complain) { |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9417 | if (InOverloadResolution) |
| 9418 | S.Diag(FD->getLocStart(), |
| 9419 | diag::note_addrof_ovl_candidate_disabled_by_enable_if_attr); |
| 9420 | else |
| 9421 | S.Diag(Loc, diag::err_addrof_function_disabled_by_enable_if_attr) << FD; |
| 9422 | } |
| 9423 | return false; |
| 9424 | } |
| 9425 | |
| George Burgess IV | 2108136 | 2016-07-24 23:12:40 +0000 | [diff] [blame] | 9426 | auto I = llvm::find_if(FD->parameters(), [](const ParmVarDecl *P) { |
| 9427 | return P->hasAttr<PassObjectSizeAttr>(); |
| 9428 | }); |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9429 | if (I == FD->param_end()) |
| 9430 | return true; |
| 9431 | |
| 9432 | if (Complain) { |
| 9433 | // Add one to ParamNo because it's user-facing |
| 9434 | unsigned ParamNo = std::distance(FD->param_begin(), I) + 1; |
| 9435 | if (InOverloadResolution) |
| 9436 | S.Diag(FD->getLocation(), |
| 9437 | diag::note_ovl_candidate_has_pass_object_size_params) |
| 9438 | << ParamNo; |
| 9439 | else |
| 9440 | S.Diag(Loc, diag::err_address_of_function_with_pass_object_size_params) |
| 9441 | << FD << ParamNo; |
| 9442 | } |
| 9443 | return false; |
| 9444 | } |
| 9445 | |
| 9446 | static bool checkAddressOfCandidateIsAvailable(Sema &S, |
| 9447 | const FunctionDecl *FD) { |
| 9448 | return checkAddressOfFunctionIsAvailable(S, FD, /*Complain=*/true, |
| 9449 | /*InOverloadResolution=*/true, |
| 9450 | /*Loc=*/SourceLocation()); |
| 9451 | } |
| 9452 | |
| 9453 | bool Sema::checkAddressOfFunctionIsAvailable(const FunctionDecl *Function, |
| 9454 | bool Complain, |
| 9455 | SourceLocation Loc) { |
| 9456 | return ::checkAddressOfFunctionIsAvailable(*this, Function, Complain, |
| 9457 | /*InOverloadResolution=*/false, |
| 9458 | Loc); |
| 9459 | } |
| 9460 | |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9461 | // Notes the location of an overload candidate. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9462 | void Sema::NoteOverloadCandidate(NamedDecl *Found, FunctionDecl *Fn, |
| 9463 | QualType DestType, bool TakingAddress) { |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9464 | if (TakingAddress && !checkAddressOfCandidateIsAvailable(*this, Fn)) |
| 9465 | return; |
| Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 9466 | if (Fn->isMultiVersion() && !Fn->getAttr<TargetAttr>()->isDefaultVersion()) |
| 9467 | return; |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9468 | |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9469 | std::string FnDesc; |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9470 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> KSPair = |
| 9471 | ClassifyOverloadCandidate(*this, Found, Fn, FnDesc); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9472 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9473 | << (unsigned)KSPair.first << (unsigned)KSPair.second |
| 9474 | << Fn << FnDesc; |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9475 | |
| 9476 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9477 | Diag(Fn->getLocation(), PD); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9478 | MaybeEmitInheritedConstructorNote(*this, Found); |
| John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 9479 | } |
| 9480 | |
| Nick Lewycky | ed4265c | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 9481 | // Notes the location of all overload candidates designated through |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9482 | // OverloadedExpr |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 9483 | void Sema::NoteAllOverloadCandidates(Expr *OverloadedExpr, QualType DestType, |
| 9484 | bool TakingAddress) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9485 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 9486 | |
| 9487 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 9488 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 9489 | |
| 9490 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9491 | IEnd = OvlExpr->decls_end(); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9492 | I != IEnd; ++I) { |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9493 | if (FunctionTemplateDecl *FunTmpl = |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9494 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9495 | NoteOverloadCandidate(*I, FunTmpl->getTemplatedDecl(), DestType, |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 9496 | TakingAddress); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9497 | } else if (FunctionDecl *Fun |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9498 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9499 | NoteOverloadCandidate(*I, Fun, DestType, TakingAddress); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9500 | } |
| 9501 | } |
| 9502 | } |
| 9503 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9504 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 9505 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 9506 | /// target types of the conversion. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9507 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 9508 | Sema &S, |
| 9509 | SourceLocation CaretLoc, |
| 9510 | const PartialDiagnostic &PDiag) const { |
| 9511 | S.Diag(CaretLoc, PDiag) |
| 9512 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
| Matt Beaumont-Gay | 641bd89 | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 9513 | // FIXME: The note limiting machinery is borrowed from |
| 9514 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 9515 | // refactoring here. |
| 9516 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9517 | unsigned CandsShown = 0; |
| 9518 | AmbiguousConversionSequence::const_iterator I, E; |
| 9519 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 9520 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9521 | break; |
| 9522 | ++CandsShown; |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9523 | S.NoteOverloadCandidate(I->first, I->second); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9524 | } |
| Matt Beaumont-Gay | 641bd89 | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 9525 | if (I != E) |
| 9526 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9527 | } |
| 9528 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 9529 | static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9530 | unsigned I, bool TakingCandidateAddress) { |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9531 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 9532 | assert(Conv.isBad()); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9533 | assert(Cand->Function && "for now, candidate must be a function"); |
| 9534 | FunctionDecl *Fn = Cand->Function; |
| 9535 | |
| 9536 | // There's a conversion slot for the object argument if this is a |
| 9537 | // non-constructor method. Note that 'I' corresponds the |
| 9538 | // conversion-slot index. |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9539 | bool isObjectArgument = false; |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9540 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9541 | if (I == 0) |
| 9542 | isObjectArgument = true; |
| 9543 | else |
| 9544 | I--; |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9545 | } |
| 9546 | |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9547 | std::string FnDesc; |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9548 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9549 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, FnDesc); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9550 | |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9551 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 9552 | QualType FromTy = Conv.Bad.getFromType(); |
| 9553 | QualType ToTy = Conv.Bad.getToType(); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9554 | |
| John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 9555 | if (FromTy == S.Context.OverloadTy) { |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9556 | assert(FromExpr && "overload set argument came from implicit argument?"); |
| John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 9557 | Expr *E = FromExpr->IgnoreParens(); |
| 9558 | if (isa<UnaryOperator>(E)) |
| 9559 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
| John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9560 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
| John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 9561 | |
| 9562 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9563 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9564 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << ToTy |
| 9565 | << Name << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9566 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 9567 | return; |
| 9568 | } |
| 9569 | |
| John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 9570 | // Do some hand-waving analysis to see if the non-viability is due |
| 9571 | // to a qualifier mismatch. |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9572 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 9573 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 9574 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 9575 | CToTy = RT->getPointeeType(); |
| 9576 | else { |
| 9577 | // TODO: detect and diagnose the full richness of const mismatches. |
| 9578 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 9579 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) { |
| 9580 | CFromTy = FromPT->getPointeeType(); |
| 9581 | CToTy = ToPT->getPointeeType(); |
| 9582 | } |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9583 | } |
| 9584 | |
| 9585 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 9586 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9587 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 9588 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 9589 | |
| 9590 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 9591 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9592 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9593 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| Anastasia Stulova | bf549bf | 2018-06-22 15:45:08 +0000 | [diff] [blame] | 9594 | << ToTy << (unsigned)isObjectArgument << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9595 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9596 | return; |
| 9597 | } |
| 9598 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9599 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9600 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9601 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9602 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9603 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 9604 | << (unsigned)isObjectArgument << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9605 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9606 | return; |
| 9607 | } |
| 9608 | |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 9609 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 9610 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9611 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9612 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9613 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 9614 | << (unsigned)isObjectArgument << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9615 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 9616 | return; |
| 9617 | } |
| 9618 | |
| Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 9619 | if (FromQs.hasUnaligned() != ToQs.hasUnaligned()) { |
| 9620 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_unaligned) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9621 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9622 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9623 | << FromQs.hasUnaligned() << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9624 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 9625 | return; |
| 9626 | } |
| 9627 | |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9628 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 9629 | assert(CVR && "unexpected qualifiers mismatch"); |
| 9630 | |
| 9631 | if (isObjectArgument) { |
| 9632 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9633 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9634 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9635 | << (CVR - 1); |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9636 | } else { |
| 9637 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9638 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9639 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9640 | << (CVR - 1) << I + 1; |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9641 | } |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9642 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 9643 | return; |
| 9644 | } |
| 9645 | |
| Sebastian Redl | a72462c | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 9646 | // Special diagnostic for failure to convert an initializer list, since |
| 9647 | // telling the user that it has type void is not useful. |
| 9648 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 9649 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9650 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9651 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9652 | << ToTy << (unsigned)isObjectArgument << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9653 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| Sebastian Redl | a72462c | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 9654 | return; |
| 9655 | } |
| 9656 | |
| John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 9657 | // Diagnose references or pointers to incomplete types differently, |
| 9658 | // since it's far from impossible that the incompleteness triggered |
| 9659 | // the failure. |
| 9660 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 9661 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 9662 | TempFromTy = PTy->getPointeeType(); |
| 9663 | if (TempFromTy->isIncompleteType()) { |
| David Blaikie | ac92893 | 2016-03-04 22:29:11 +0000 | [diff] [blame] | 9664 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 9665 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9666 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9667 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9668 | << ToTy << (unsigned)isObjectArgument << I + 1 |
| 9669 | << (unsigned)(Cand->Fix.Kind); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9670 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9671 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 9672 | return; |
| 9673 | } |
| 9674 | |
| Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 9675 | // Diagnose base -> derived pointer conversions. |
| Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 9676 | unsigned BaseToDerivedConversion = 0; |
| Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 9677 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 9678 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 9679 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 9680 | FromPtrTy->getPointeeType()) && |
| 9681 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 9682 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 9683 | S.IsDerivedFrom(SourceLocation(), ToPtrTy->getPointeeType(), |
| Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 9684 | FromPtrTy->getPointeeType())) |
| Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 9685 | BaseToDerivedConversion = 1; |
| Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 9686 | } |
| 9687 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 9688 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 9689 | if (const ObjCObjectPointerType *ToPtrTy |
| 9690 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 9691 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 9692 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 9693 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 9694 | FromPtrTy->getPointeeType()) && |
| 9695 | FromIface->isSuperClassOf(ToIface)) |
| Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 9696 | BaseToDerivedConversion = 2; |
| 9697 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 9698 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 9699 | !FromTy->isIncompleteType() && |
| 9700 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 9701 | S.IsDerivedFrom(SourceLocation(), ToRefTy->getPointeeType(), FromTy)) { |
| Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 9702 | BaseToDerivedConversion = 3; |
| 9703 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 9704 | ToTy.getNonReferenceType().getCanonicalType() == |
| 9705 | FromTy.getNonReferenceType().getCanonicalType()) { |
| Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 9706 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9707 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9708 | << (unsigned)isObjectArgument << I + 1 |
| 9709 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9710 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 9711 | return; |
| Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 9712 | } |
| Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 9713 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9714 | |
| Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 9715 | if (BaseToDerivedConversion) { |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9716 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_base_to_derived_conv) |
| 9717 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9718 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 9719 | << (BaseToDerivedConversion - 1) << FromTy << ToTy << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9720 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 9721 | return; |
| 9722 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9723 | |
| Fariborz Jahanian | a644f9c | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 9724 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 9725 | isa<PointerType>(CToTy)) { |
| 9726 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 9727 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 9728 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 9729 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9730 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
| 9731 | << FnDesc << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 9732 | << FromTy << ToTy << (unsigned)isObjectArgument << I + 1; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9733 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| Fariborz Jahanian | a644f9c | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 9734 | return; |
| 9735 | } |
| 9736 | } |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9737 | |
| 9738 | if (TakingCandidateAddress && |
| 9739 | !checkAddressOfCandidateIsAvailable(S, Cand->Function)) |
| 9740 | return; |
| 9741 | |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9742 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 9743 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9744 | FDiag << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 9745 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) << FromTy |
| 9746 | << ToTy << (unsigned)isObjectArgument << I + 1 |
| 9747 | << (unsigned)(Cand->Fix.Kind); |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9748 | |
| 9749 | // If we can fix the conversion, suggest the FixIts. |
| Benjamin Kramer | 490afa6 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 9750 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 9751 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9752 | FDiag << *HI; |
| 9753 | S.Diag(Fn->getLocation(), FDiag); |
| 9754 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9755 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9756 | } |
| 9757 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9758 | /// Additional arity mismatch diagnosis specific to a function overload |
| 9759 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 9760 | /// over a candidate in any candidate set. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 9761 | static bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 9762 | unsigned NumArgs) { |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9763 | FunctionDecl *Fn = Cand->Function; |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9764 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9765 | |
| Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 9766 | // With invalid overloaded operators, it's possible that we think we |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9767 | // have an arity mismatch when in fact it looks like we have the |
| Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 9768 | // right number of arguments, because only overloaded operators have |
| 9769 | // the weird behavior of overloading member and non-member functions. |
| 9770 | // Just don't report anything. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9771 | if (Fn->isInvalidDecl() && |
| Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 9772 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9773 | return true; |
| 9774 | |
| 9775 | if (NumArgs < MinParams) { |
| 9776 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 9777 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 9778 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 9779 | } else { |
| 9780 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 9781 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 9782 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 9783 | } |
| 9784 | |
| 9785 | return false; |
| 9786 | } |
| 9787 | |
| 9788 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9789 | static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D, |
| 9790 | unsigned NumFormalArgs) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9791 | assert(isa<FunctionDecl>(D) && |
| 9792 | "The templated declaration should at least be a function" |
| 9793 | " when diagnosing bad template argument deduction due to too many" |
| 9794 | " or too few arguments"); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9795 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9796 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 9797 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9798 | // TODO: treat calls to a missing default constructor as a special case |
| 9799 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 9800 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 9801 | |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9802 | // at least / at most / exactly |
| 9803 | unsigned mode, modeCount; |
| 9804 | if (NumFormalArgs < MinParams) { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9805 | if (MinParams != FnTy->getNumParams() || FnTy->isVariadic() || |
| 9806 | FnTy->isTemplateVariadic()) |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9807 | mode = 0; // "at least" |
| 9808 | else |
| 9809 | mode = 2; // "exactly" |
| 9810 | modeCount = MinParams; |
| 9811 | } else { |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9812 | if (MinParams != FnTy->getNumParams()) |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9813 | mode = 1; // "at most" |
| 9814 | else |
| 9815 | mode = 2; // "exactly" |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9816 | modeCount = FnTy->getNumParams(); |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9817 | } |
| 9818 | |
| 9819 | std::string Description; |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9820 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9821 | ClassifyOverloadCandidate(S, Found, Fn, Description); |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9822 | |
| Richard Smith | 10ff50d | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 9823 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 9824 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9825 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
| 9826 | << Description << mode << Fn->getParamDecl(0) << NumFormalArgs; |
| Richard Smith | 10ff50d | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 9827 | else |
| 9828 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 9829 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second |
| 9830 | << Description << mode << modeCount << NumFormalArgs; |
| 9831 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9832 | MaybeEmitInheritedConstructorNote(S, Found); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9833 | } |
| 9834 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9835 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 9836 | static void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 9837 | unsigned NumFormalArgs) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9838 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9839 | DiagnoseArityMismatch(S, Cand->FoundDecl, Cand->Function, NumFormalArgs); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9840 | } |
| Larisse Voufo | 47c0845 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 9841 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 9842 | static TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 9843 | if (TemplateDecl *TD = Templated->getDescribedTemplate()) |
| 9844 | return TD; |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9845 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 9846 | " for bad deduction diagnosis"); |
| 9847 | } |
| 9848 | |
| 9849 | /// Diagnose a failed template-argument deduction. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9850 | static void DiagnoseBadDeduction(Sema &S, NamedDecl *Found, Decl *Templated, |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 9851 | DeductionFailureInfo &DeductionFailure, |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 9852 | unsigned NumArgs, |
| 9853 | bool TakingCandidateAddress) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9854 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9855 | NamedDecl *ParamD; |
| 9856 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 9857 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 9858 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9859 | switch (DeductionFailure.Result) { |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9860 | case Sema::TDK_Success: |
| 9861 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 9862 | |
| 9863 | case Sema::TDK_Incomplete: { |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9864 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9865 | S.Diag(Templated->getLocation(), |
| 9866 | diag::note_ovl_candidate_incomplete_deduction) |
| 9867 | << ParamD->getDeclName(); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9868 | MaybeEmitInheritedConstructorNote(S, Found); |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9869 | return; |
| 9870 | } |
| 9871 | |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9872 | case Sema::TDK_Underqualified: { |
| 9873 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 9874 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 9875 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9876 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9877 | |
| 9878 | // Param will have been canonicalized, but it should just be a |
| 9879 | // qualified version of ParamD, so move the qualifiers to that. |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 9880 | QualifierCollector Qs; |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9881 | Qs.strip(Param); |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 9882 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9883 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 9884 | |
| 9885 | // Arg has also been canonicalized, but there's nothing we can do |
| 9886 | // about that. It also doesn't matter as much, because it won't |
| 9887 | // have any template parameters in it (because deduction isn't |
| 9888 | // done on dependent types). |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9889 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9890 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9891 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 9892 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9893 | MaybeEmitInheritedConstructorNote(S, Found); |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 9894 | return; |
| 9895 | } |
| 9896 | |
| 9897 | case Sema::TDK_Inconsistent: { |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9898 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9899 | int which = 0; |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9900 | if (isa<TemplateTypeParmDecl>(ParamD)) |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9901 | which = 0; |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 9902 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) { |
| 9903 | // Deduction might have failed because we deduced arguments of two |
| 9904 | // different types for a non-type template parameter. |
| 9905 | // FIXME: Use a different TDK value for this. |
| 9906 | QualType T1 = |
| 9907 | DeductionFailure.getFirstArg()->getNonTypeTemplateArgumentType(); |
| 9908 | QualType T2 = |
| 9909 | DeductionFailure.getSecondArg()->getNonTypeTemplateArgumentType(); |
| 9910 | if (!S.Context.hasSameType(T1, T2)) { |
| 9911 | S.Diag(Templated->getLocation(), |
| 9912 | diag::note_ovl_candidate_inconsistent_deduction_types) |
| 9913 | << ParamD->getDeclName() << *DeductionFailure.getFirstArg() << T1 |
| 9914 | << *DeductionFailure.getSecondArg() << T2; |
| 9915 | MaybeEmitInheritedConstructorNote(S, Found); |
| 9916 | return; |
| 9917 | } |
| 9918 | |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9919 | which = 1; |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 9920 | } else { |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9921 | which = 2; |
| 9922 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9923 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9924 | S.Diag(Templated->getLocation(), |
| 9925 | diag::note_ovl_candidate_inconsistent_deduction) |
| 9926 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 9927 | << *DeductionFailure.getSecondArg(); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9928 | MaybeEmitInheritedConstructorNote(S, Found); |
| Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 9929 | return; |
| 9930 | } |
| Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 9931 | |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9932 | case Sema::TDK_InvalidExplicitArguments: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9933 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9934 | if (ParamD->getDeclName()) |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9935 | S.Diag(Templated->getLocation(), |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9936 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9937 | << ParamD->getDeclName(); |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9938 | else { |
| 9939 | int index = 0; |
| 9940 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 9941 | index = TTP->getIndex(); |
| 9942 | else if (NonTypeTemplateParmDecl *NTTP |
| 9943 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 9944 | index = NTTP->getIndex(); |
| 9945 | else |
| 9946 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9947 | S.Diag(Templated->getLocation(), |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9948 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9949 | << (index + 1); |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9950 | } |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9951 | MaybeEmitInheritedConstructorNote(S, Found); |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 9952 | return; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9953 | |
| Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 9954 | case Sema::TDK_TooManyArguments: |
| 9955 | case Sema::TDK_TooFewArguments: |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 9956 | DiagnoseArityMismatch(S, Found, Templated, NumArgs); |
| Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 9957 | return; |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 9958 | |
| 9959 | case Sema::TDK_InstantiationDepth: |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9960 | S.Diag(Templated->getLocation(), |
| 9961 | diag::note_ovl_candidate_instantiation_depth); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9962 | MaybeEmitInheritedConstructorNote(S, Found); |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 9963 | return; |
| 9964 | |
| 9965 | case Sema::TDK_SubstitutionFailure: { |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9966 | // Format the template argument list into the argument string. |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9967 | SmallString<128> TemplateArgString; |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9968 | if (TemplateArgumentList *Args = |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9969 | DeductionFailure.getTemplateArgumentList()) { |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9970 | TemplateArgString = " "; |
| 9971 | TemplateArgString += S.getTemplateArgumentBindingsText( |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9972 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9973 | } |
| 9974 | |
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9975 | // If this candidate was disabled by enable_if, say so. |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9976 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 9977 | if (PDiag && PDiag->second.getDiagID() == |
| 9978 | diag::err_typename_nested_not_found_enable_if) { |
| 9979 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 9980 | // name of the enable_if template. These are both present in PDiag. |
| 9981 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 9982 | << "'enable_if'" << TemplateArgString; |
| 9983 | return; |
| 9984 | } |
| 9985 | |
| Douglas Gregor | 00fa10b | 2017-07-05 20:20:14 +0000 | [diff] [blame] | 9986 | // We found a specific requirement that disabled the enable_if. |
| 9987 | if (PDiag && PDiag->second.getDiagID() == |
| 9988 | diag::err_typename_nested_not_found_requirement) { |
| 9989 | S.Diag(Templated->getLocation(), |
| 9990 | diag::note_ovl_candidate_disabled_by_requirement) |
| 9991 | << PDiag->second.getStringArg(0) << TemplateArgString; |
| 9992 | return; |
| 9993 | } |
| 9994 | |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9995 | // Format the SFINAE diagnostic into the argument string. |
| 9996 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 9997 | // formatted message in another diagnostic. |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9998 | SmallString<128> SFINAEArgString; |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 9999 | SourceRange R; |
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 10000 | if (PDiag) { |
| Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 10001 | SFINAEArgString = ": "; |
| 10002 | R = SourceRange(PDiag->first, PDiag->first); |
| 10003 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 10004 | } |
| 10005 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10006 | S.Diag(Templated->getLocation(), |
| 10007 | diag::note_ovl_candidate_substitution_failure) |
| 10008 | << TemplateArgString << SFINAEArgString << R; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10009 | MaybeEmitInheritedConstructorNote(S, Found); |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 10010 | return; |
| 10011 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10012 | |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 10013 | case Sema::TDK_DeducedMismatch: |
| 10014 | case Sema::TDK_DeducedMismatchNested: { |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 10015 | // Format the template argument list into the argument string. |
| 10016 | SmallString<128> TemplateArgString; |
| 10017 | if (TemplateArgumentList *Args = |
| 10018 | DeductionFailure.getTemplateArgumentList()) { |
| 10019 | TemplateArgString = " "; |
| 10020 | TemplateArgString += S.getTemplateArgumentBindingsText( |
| 10021 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
| 10022 | } |
| 10023 | |
| 10024 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_deduced_mismatch) |
| 10025 | << (*DeductionFailure.getCallArgIndex() + 1) |
| 10026 | << *DeductionFailure.getFirstArg() << *DeductionFailure.getSecondArg() |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 10027 | << TemplateArgString |
| 10028 | << (DeductionFailure.Result == Sema::TDK_DeducedMismatchNested); |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 10029 | break; |
| 10030 | } |
| 10031 | |
| Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 10032 | case Sema::TDK_NonDeducedMismatch: { |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 10033 | // FIXME: Provide a source location to indicate what we couldn't match. |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10034 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 10035 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
| Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 10036 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 10037 | SecondTA.getKind() == TemplateArgument::Template) { |
| 10038 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 10039 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 10040 | if (FirstTN.getKind() == TemplateName::Template && |
| 10041 | SecondTN.getKind() == TemplateName::Template) { |
| 10042 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 10043 | SecondTN.getAsTemplateDecl()->getName()) { |
| 10044 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 10045 | // the same. This particular case is a bit difficult since: |
| 10046 | // 1) It is passed as a string to the diagnostic printer. |
| 10047 | // 2) The diagnostic printer only attempts to find a better |
| 10048 | // name for types, not decls. |
| 10049 | // Ideally, this should folded into the diagnostic printer. |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10050 | S.Diag(Templated->getLocation(), |
| Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 10051 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 10052 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 10053 | return; |
| 10054 | } |
| 10055 | } |
| 10056 | } |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10057 | |
| 10058 | if (TakingCandidateAddress && isa<FunctionDecl>(Templated) && |
| 10059 | !checkAddressOfCandidateIsAvailable(S, cast<FunctionDecl>(Templated))) |
| 10060 | return; |
| 10061 | |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 10062 | // FIXME: For generic lambda parameters, check if the function is a lambda |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10063 | // call operator, and if so, emit a prettier and more informative |
| 10064 | // diagnostic that mentions 'auto' and lambda in addition to |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 10065 | // (or instead of?) the canonical template type parameters. |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10066 | S.Diag(Templated->getLocation(), |
| 10067 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 10068 | << FirstTA << SecondTA; |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 10069 | return; |
| Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 10070 | } |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 10071 | // TODO: diagnose these individually, then kill off |
| 10072 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 10073 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10074 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10075 | MaybeEmitInheritedConstructorNote(S, Found); |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 10076 | return; |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 10077 | case Sema::TDK_CUDATargetMismatch: |
| 10078 | S.Diag(Templated->getLocation(), |
| 10079 | diag::note_cuda_ovl_candidate_target_mismatch); |
| 10080 | return; |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 10081 | } |
| 10082 | } |
| 10083 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10084 | /// Diagnose a failed template-argument deduction, for function calls. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10085 | static void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10086 | unsigned NumArgs, |
| 10087 | bool TakingCandidateAddress) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10088 | unsigned TDK = Cand->DeductionFailure.Result; |
| 10089 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 10090 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 10091 | return; |
| 10092 | } |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10093 | DiagnoseBadDeduction(S, Cand->FoundDecl, Cand->Function, // pattern |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10094 | Cand->DeductionFailure, NumArgs, TakingCandidateAddress); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10095 | } |
| 10096 | |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10097 | /// CUDA: diagnose an invalid call across targets. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10098 | static void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10099 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 10100 | FunctionDecl *Callee = Cand->Function; |
| 10101 | |
| 10102 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 10103 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 10104 | |
| 10105 | std::string FnDesc; |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 10106 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10107 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Callee, FnDesc); |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10108 | |
| 10109 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 10110 | << (unsigned)FnKindPair.first << (unsigned)ocs_non_template |
| 10111 | << FnDesc /* Ignored */ |
| 10112 | << CalleeTarget << CallerTarget; |
| Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 10113 | |
| 10114 | // This could be an implicit constructor for which we could not infer the |
| 10115 | // target due to a collsion. Diagnose that case. |
| 10116 | CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Callee); |
| 10117 | if (Meth != nullptr && Meth->isImplicit()) { |
| 10118 | CXXRecordDecl *ParentClass = Meth->getParent(); |
| 10119 | Sema::CXXSpecialMember CSM; |
| 10120 | |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 10121 | switch (FnKindPair.first) { |
| Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 10122 | default: |
| 10123 | return; |
| 10124 | case oc_implicit_default_constructor: |
| 10125 | CSM = Sema::CXXDefaultConstructor; |
| 10126 | break; |
| 10127 | case oc_implicit_copy_constructor: |
| 10128 | CSM = Sema::CXXCopyConstructor; |
| 10129 | break; |
| 10130 | case oc_implicit_move_constructor: |
| 10131 | CSM = Sema::CXXMoveConstructor; |
| 10132 | break; |
| 10133 | case oc_implicit_copy_assignment: |
| 10134 | CSM = Sema::CXXCopyAssignment; |
| 10135 | break; |
| 10136 | case oc_implicit_move_assignment: |
| 10137 | CSM = Sema::CXXMoveAssignment; |
| 10138 | break; |
| 10139 | }; |
| 10140 | |
| 10141 | bool ConstRHS = false; |
| 10142 | if (Meth->getNumParams()) { |
| 10143 | if (const ReferenceType *RT = |
| 10144 | Meth->getParamDecl(0)->getType()->getAs<ReferenceType>()) { |
| 10145 | ConstRHS = RT->getPointeeType().isConstQualified(); |
| 10146 | } |
| 10147 | } |
| 10148 | |
| 10149 | S.inferCUDATargetForImplicitSpecialMember(ParentClass, CSM, Meth, |
| 10150 | /* ConstRHS */ ConstRHS, |
| 10151 | /* Diagnose */ true); |
| 10152 | } |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10153 | } |
| 10154 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10155 | static void DiagnoseFailedEnableIfAttr(Sema &S, OverloadCandidate *Cand) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10156 | FunctionDecl *Callee = Cand->Function; |
| 10157 | EnableIfAttr *Attr = static_cast<EnableIfAttr*>(Cand->DeductionFailure.Data); |
| 10158 | |
| 10159 | S.Diag(Callee->getLocation(), |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10160 | diag::note_ovl_candidate_disabled_by_function_cond_attr) |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10161 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); |
| 10162 | } |
| 10163 | |
| Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 10164 | static void DiagnoseOpenCLExtensionDisabled(Sema &S, OverloadCandidate *Cand) { |
| 10165 | FunctionDecl *Callee = Cand->Function; |
| 10166 | |
| 10167 | S.Diag(Callee->getLocation(), |
| 10168 | diag::note_ovl_candidate_disabled_by_extension); |
| 10169 | } |
| 10170 | |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 10171 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 10172 | /// already generated a primary error at the call site. |
| 10173 | /// |
| 10174 | /// It really does need to be a single diagnostic with its caret |
| 10175 | /// pointed at the candidate declaration. Yes, this creates some |
| 10176 | /// major challenges of technical writing. Yes, this makes pointing |
| 10177 | /// out problems with specific arguments quite awkward. It's still |
| 10178 | /// better than generating twenty screens of text for every failed |
| 10179 | /// overload. |
| 10180 | /// |
| 10181 | /// It would be great to be able to express per-candidate problems |
| 10182 | /// more richly for those diagnostic clients that cared, but we'd |
| 10183 | /// still have to be just as careful with the default diagnostics. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10184 | static void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10185 | unsigned NumArgs, |
| 10186 | bool TakingCandidateAddress) { |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 10187 | FunctionDecl *Fn = Cand->Function; |
| 10188 | |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10189 | // Note deleted candidates, but only if they're viable. |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10190 | if (Cand->Viable) { |
| 10191 | if (Fn->isDeleted() || S.isFunctionConsideredUnavailable(Fn)) { |
| 10192 | std::string FnDesc; |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 10193 | std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair = |
| 10194 | ClassifyOverloadCandidate(S, Cand->FoundDecl, Fn, FnDesc); |
| John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 10195 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10196 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 10197 | << (unsigned)FnKindPair.first << (unsigned)FnKindPair.second << FnDesc |
| 10198 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10199 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| 10200 | return; |
| 10201 | } |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10202 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10203 | // We don't really have anything else to say about viable candidates. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10204 | S.NoteOverloadCandidate(Cand->FoundDecl, Fn); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 10205 | return; |
| 10206 | } |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10207 | |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 10208 | switch (Cand->FailureKind) { |
| 10209 | case ovl_fail_too_many_arguments: |
| 10210 | case ovl_fail_too_few_arguments: |
| 10211 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 10212 | |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 10213 | case ovl_fail_bad_deduction: |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10214 | return DiagnoseBadDeduction(S, Cand, NumArgs, |
| 10215 | TakingCandidateAddress); |
| John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 10216 | |
| John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 10217 | case ovl_fail_illegal_constructor: { |
| 10218 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_illegal_constructor) |
| 10219 | << (Fn->getPrimaryTemplate() ? 1 : 0); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10220 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 10221 | return; |
| 10222 | } |
| 10223 | |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10224 | case ovl_fail_trivial_conversion: |
| 10225 | case ovl_fail_bad_final_conversion: |
| Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 10226 | case ovl_fail_final_conversion_not_exact: |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10227 | return S.NoteOverloadCandidate(Cand->FoundDecl, Fn); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 10228 | |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 10229 | case ovl_fail_bad_conversion: { |
| 10230 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10231 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 10232 | if (Cand->Conversions[I].isBad()) |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10233 | return DiagnoseBadConversion(S, Cand, I, TakingCandidateAddress); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10234 | |
| John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 10235 | // FIXME: this currently happens when we're called from SemaInit |
| 10236 | // when user-conversion overload fails. Figure out how to handle |
| 10237 | // those conditions and diagnose them well. |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10238 | return S.NoteOverloadCandidate(Cand->FoundDecl, Fn); |
| John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 10239 | } |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10240 | |
| 10241 | case ovl_fail_bad_target: |
| 10242 | return DiagnoseBadTarget(S, Cand); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10243 | |
| 10244 | case ovl_fail_enable_if: |
| 10245 | return DiagnoseFailedEnableIfAttr(S, Cand); |
| George Burgess IV | 7204ed9 | 2016-01-07 02:26:57 +0000 | [diff] [blame] | 10246 | |
| Yaxun Liu | 5b74665 | 2016-12-18 05:18:55 +0000 | [diff] [blame] | 10247 | case ovl_fail_ext_disabled: |
| 10248 | return DiagnoseOpenCLExtensionDisabled(S, Cand); |
| 10249 | |
| Richard Smith | f9c59b7 | 2017-01-08 21:45:44 +0000 | [diff] [blame] | 10250 | case ovl_fail_inhctor_slice: |
| Richard Smith | 836a3b4 | 2017-01-13 20:46:54 +0000 | [diff] [blame] | 10251 | // It's generally not interesting to note copy/move constructors here. |
| 10252 | if (cast<CXXConstructorDecl>(Fn)->isCopyOrMoveConstructor()) |
| 10253 | return; |
| Richard Smith | f9c59b7 | 2017-01-08 21:45:44 +0000 | [diff] [blame] | 10254 | S.Diag(Fn->getLocation(), |
| Richard Smith | 836a3b4 | 2017-01-13 20:46:54 +0000 | [diff] [blame] | 10255 | diag::note_ovl_candidate_inherited_constructor_slice) |
| 10256 | << (Fn->getPrimaryTemplate() ? 1 : 0) |
| 10257 | << Fn->getParamDecl(0)->getType()->isRValueReferenceType(); |
| Richard Smith | f9c59b7 | 2017-01-08 21:45:44 +0000 | [diff] [blame] | 10258 | MaybeEmitInheritedConstructorNote(S, Cand->FoundDecl); |
| 10259 | return; |
| 10260 | |
| George Burgess IV | 7204ed9 | 2016-01-07 02:26:57 +0000 | [diff] [blame] | 10261 | case ovl_fail_addr_not_available: { |
| 10262 | bool Available = checkAddressOfCandidateIsAvailable(S, Cand->Function); |
| 10263 | (void)Available; |
| 10264 | assert(!Available); |
| 10265 | break; |
| 10266 | } |
| Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 10267 | case ovl_non_default_multiversion_function: |
| 10268 | // Do nothing, these should simply be ignored. |
| 10269 | break; |
| John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 10270 | } |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10271 | } |
| 10272 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10273 | static void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10274 | // Desugar the type of the surrogate down to a function type, |
| 10275 | // retaining as many typedefs as possible while still showing |
| 10276 | // the function type (and, therefore, its parameter types). |
| 10277 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 10278 | bool isLValueReference = false; |
| 10279 | bool isRValueReference = false; |
| 10280 | bool isPointer = false; |
| 10281 | if (const LValueReferenceType *FnTypeRef = |
| 10282 | FnType->getAs<LValueReferenceType>()) { |
| 10283 | FnType = FnTypeRef->getPointeeType(); |
| 10284 | isLValueReference = true; |
| 10285 | } else if (const RValueReferenceType *FnTypeRef = |
| 10286 | FnType->getAs<RValueReferenceType>()) { |
| 10287 | FnType = FnTypeRef->getPointeeType(); |
| 10288 | isRValueReference = true; |
| 10289 | } |
| 10290 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 10291 | FnType = FnTypePtr->getPointeeType(); |
| 10292 | isPointer = true; |
| 10293 | } |
| 10294 | // Desugar down to a function type. |
| 10295 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 10296 | // Reconstruct the pointer/reference as appropriate. |
| 10297 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 10298 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 10299 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 10300 | |
| 10301 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 10302 | << FnType; |
| 10303 | } |
| 10304 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10305 | static void NoteBuiltinOperatorCandidate(Sema &S, StringRef Opc, |
| 10306 | SourceLocation OpLoc, |
| 10307 | OverloadCandidate *Cand) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10308 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10309 | std::string TypeStr("operator"); |
| 10310 | TypeStr += Opc; |
| 10311 | TypeStr += "("; |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 10312 | TypeStr += Cand->BuiltinParamTypes[0].getAsString(); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10313 | if (Cand->Conversions.size() == 1) { |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10314 | TypeStr += ")"; |
| 10315 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 10316 | } else { |
| 10317 | TypeStr += ", "; |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 10318 | TypeStr += Cand->BuiltinParamTypes[1].getAsString(); |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10319 | TypeStr += ")"; |
| 10320 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 10321 | } |
| 10322 | } |
| 10323 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10324 | static void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 10325 | OverloadCandidate *Cand) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10326 | for (const ImplicitConversionSequence &ICS : Cand->Conversions) { |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10327 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 10328 | if (!ICS.isAmbiguous()) continue; |
| 10329 | |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10330 | ICS.DiagnoseAmbiguousConversion( |
| 10331 | S, OpLoc, S.PDiag(diag::note_ambiguous_type_conversion)); |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10332 | } |
| 10333 | } |
| 10334 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10335 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10336 | if (Cand->Function) |
| 10337 | return Cand->Function->getLocation(); |
| John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 10338 | if (Cand->IsSurrogate) |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10339 | return Cand->Surrogate->getLocation(); |
| 10340 | return SourceLocation(); |
| 10341 | } |
| 10342 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10343 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
| Chandler Carruth | 73fddfe | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 10344 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10345 | case Sema::TDK_Success: |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10346 | case Sema::TDK_NonDependentConversionFailure: |
| 10347 | llvm_unreachable("non-deduction failure while diagnosing bad deduction"); |
| Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 10348 | |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 10349 | case Sema::TDK_Invalid: |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10350 | case Sema::TDK_Incomplete: |
| 10351 | return 1; |
| 10352 | |
| 10353 | case Sema::TDK_Underqualified: |
| 10354 | case Sema::TDK_Inconsistent: |
| 10355 | return 2; |
| 10356 | |
| 10357 | case Sema::TDK_SubstitutionFailure: |
| Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 10358 | case Sema::TDK_DeducedMismatch: |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 10359 | case Sema::TDK_DeducedMismatchNested: |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10360 | case Sema::TDK_NonDeducedMismatch: |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 10361 | case Sema::TDK_MiscellaneousDeductionFailure: |
| Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame] | 10362 | case Sema::TDK_CUDATargetMismatch: |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10363 | return 3; |
| 10364 | |
| 10365 | case Sema::TDK_InstantiationDepth: |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10366 | return 4; |
| 10367 | |
| 10368 | case Sema::TDK_InvalidExplicitArguments: |
| 10369 | return 5; |
| 10370 | |
| 10371 | case Sema::TDK_TooManyArguments: |
| 10372 | case Sema::TDK_TooFewArguments: |
| 10373 | return 6; |
| 10374 | } |
| Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 10375 | llvm_unreachable("Unhandled deduction result"); |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10376 | } |
| 10377 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10378 | namespace { |
| John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 10379 | struct CompareOverloadCandidatesForDisplay { |
| 10380 | Sema &S; |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 10381 | SourceLocation Loc; |
| Kaelyn Takata | b96b3be | 2014-05-01 21:15:24 +0000 | [diff] [blame] | 10382 | size_t NumArgs; |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 10383 | OverloadCandidateSet::CandidateSetKind CSK; |
| Kaelyn Takata | b96b3be | 2014-05-01 21:15:24 +0000 | [diff] [blame] | 10384 | |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 10385 | CompareOverloadCandidatesForDisplay( |
| 10386 | Sema &S, SourceLocation Loc, size_t NArgs, |
| 10387 | OverloadCandidateSet::CandidateSetKind CSK) |
| Richard Smith | fd54435 | 2017-09-26 21:33:43 +0000 | [diff] [blame] | 10388 | : S(S), NumArgs(NArgs), CSK(CSK) {} |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10389 | |
| 10390 | bool operator()(const OverloadCandidate *L, |
| 10391 | const OverloadCandidate *R) { |
| John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 10392 | // Fast-path this check. |
| 10393 | if (L == R) return false; |
| 10394 | |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10395 | // Order first by viability. |
| John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 10396 | if (L->Viable) { |
| 10397 | if (!R->Viable) return true; |
| 10398 | |
| 10399 | // TODO: introduce a tri-valued comparison for overload |
| 10400 | // candidates. Would be more worthwhile if we had a sort |
| 10401 | // that could exploit it. |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 10402 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation(), CSK)) |
| 10403 | return true; |
| 10404 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation(), CSK)) |
| 10405 | return false; |
| John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 10406 | } else if (R->Viable) |
| 10407 | return false; |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10408 | |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10409 | assert(L->Viable == R->Viable); |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10410 | |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10411 | // Criteria by which we can sort non-viable candidates: |
| 10412 | if (!L->Viable) { |
| 10413 | // 1. Arity mismatches come after other candidates. |
| 10414 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| Kaelyn Takata | b96b3be | 2014-05-01 21:15:24 +0000 | [diff] [blame] | 10415 | L->FailureKind == ovl_fail_too_few_arguments) { |
| 10416 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 10417 | R->FailureKind == ovl_fail_too_few_arguments) { |
| Kaelyn Takata | 50c4ffc | 2014-05-07 00:43:38 +0000 | [diff] [blame] | 10418 | int LDist = std::abs((int)L->getNumParams() - (int)NumArgs); |
| 10419 | int RDist = std::abs((int)R->getNumParams() - (int)NumArgs); |
| 10420 | if (LDist == RDist) { |
| 10421 | if (L->FailureKind == R->FailureKind) |
| 10422 | // Sort non-surrogates before surrogates. |
| 10423 | return !L->IsSurrogate && R->IsSurrogate; |
| 10424 | // Sort candidates requiring fewer parameters than there were |
| 10425 | // arguments given after candidates requiring more parameters |
| 10426 | // than there were arguments given. |
| 10427 | return L->FailureKind == ovl_fail_too_many_arguments; |
| 10428 | } |
| Kaelyn Takata | b96b3be | 2014-05-01 21:15:24 +0000 | [diff] [blame] | 10429 | return LDist < RDist; |
| 10430 | } |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10431 | return false; |
| Kaelyn Takata | b96b3be | 2014-05-01 21:15:24 +0000 | [diff] [blame] | 10432 | } |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10433 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 10434 | R->FailureKind == ovl_fail_too_few_arguments) |
| 10435 | return true; |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10436 | |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10437 | // 2. Bad conversions come first and are ordered by the number |
| 10438 | // of bad conversions and quality of good conversions. |
| 10439 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 10440 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 10441 | return true; |
| 10442 | |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 10443 | // The conversion that can be fixed with a smaller number of changes, |
| 10444 | // comes first. |
| 10445 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 10446 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 10447 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 10448 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
| Anna Zaks | 9ccf84e | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 10449 | if (numLFixes != numRFixes) { |
| David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 10450 | return numLFixes < numRFixes; |
| Anna Zaks | 9ccf84e | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 10451 | } |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 10452 | |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10453 | // If there's any ordering between the defined conversions... |
| 10454 | // FIXME: this might not be transitive. |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10455 | assert(L->Conversions.size() == R->Conversions.size()); |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10456 | |
| 10457 | int leftBetter = 0; |
| John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 10458 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10459 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
| Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 10460 | switch (CompareImplicitConversionSequences(S, Loc, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10461 | L->Conversions[I], |
| 10462 | R->Conversions[I])) { |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10463 | case ImplicitConversionSequence::Better: |
| 10464 | leftBetter++; |
| 10465 | break; |
| 10466 | |
| 10467 | case ImplicitConversionSequence::Worse: |
| 10468 | leftBetter--; |
| 10469 | break; |
| 10470 | |
| 10471 | case ImplicitConversionSequence::Indistinguishable: |
| 10472 | break; |
| 10473 | } |
| 10474 | } |
| 10475 | if (leftBetter > 0) return true; |
| 10476 | if (leftBetter < 0) return false; |
| 10477 | |
| 10478 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 10479 | return false; |
| 10480 | |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10481 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 10482 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 10483 | return true; |
| 10484 | |
| 10485 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 10486 | return RankDeductionFailure(L->DeductionFailure) |
| Eli Friedman | 1e7a0c6 | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 10487 | < RankDeductionFailure(R->DeductionFailure); |
| Eli Friedman | e2c600c | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 10488 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 10489 | return false; |
| Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 10490 | |
| John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 10491 | // TODO: others? |
| 10492 | } |
| 10493 | |
| 10494 | // Sort everything else by location. |
| 10495 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 10496 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 10497 | |
| 10498 | // Put candidates without locations (e.g. builtins) at the end. |
| 10499 | if (LLoc.isInvalid()) return false; |
| 10500 | if (RLoc.isInvalid()) return true; |
| 10501 | |
| 10502 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10503 | } |
| 10504 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 10505 | } |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10506 | |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10507 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10508 | /// computes up to the first bad conversion. Produces the FixIt set if |
| 10509 | /// possible. |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10510 | static void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 10511 | ArrayRef<Expr *> Args) { |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10512 | assert(!Cand->Viable); |
| 10513 | |
| 10514 | // Don't do anything on failures other than bad conversion. |
| 10515 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 10516 | |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 10517 | // We only want the FixIts if all the arguments can be corrected. |
| 10518 | bool Unfixable = false; |
| Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 10519 | // Use a implicit copy initialization to check conversion fixes. |
| 10520 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 10521 | |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10522 | // Attempt to fix the bad conversion. |
| 10523 | unsigned ConvCount = Cand->Conversions.size(); |
| 10524 | for (unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); /**/; |
| 10525 | ++ConvIdx) { |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10526 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10527 | if (Cand->Conversions[ConvIdx].isInitialized() && |
| 10528 | Cand->Conversions[ConvIdx].isBad()) { |
| 10529 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10530 | break; |
| Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 10531 | } |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10532 | } |
| 10533 | |
| Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 10534 | // FIXME: this should probably be preserved from the overload |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10535 | // operation somehow. |
| 10536 | bool SuppressUserConversions = false; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10537 | |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10538 | unsigned ConvIdx = 0; |
| 10539 | ArrayRef<QualType> ParamTypes; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10540 | |
| 10541 | if (Cand->IsSurrogate) { |
| 10542 | QualType ConvType |
| 10543 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 10544 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 10545 | ConvType = ConvPtrType->getPointeeType(); |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10546 | ParamTypes = ConvType->getAs<FunctionProtoType>()->getParamTypes(); |
| 10547 | // Conversion 0 is 'this', which doesn't have a corresponding argument. |
| 10548 | ConvIdx = 1; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10549 | } else if (Cand->Function) { |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10550 | ParamTypes = |
| 10551 | Cand->Function->getType()->getAs<FunctionProtoType>()->getParamTypes(); |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10552 | if (isa<CXXMethodDecl>(Cand->Function) && |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10553 | !isa<CXXConstructorDecl>(Cand->Function)) { |
| 10554 | // Conversion 0 is 'this', which doesn't have a corresponding argument. |
| 10555 | ConvIdx = 1; |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10556 | } |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10557 | } else { |
| 10558 | // Builtin operator. |
| 10559 | assert(ConvCount <= 3); |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 10560 | ParamTypes = Cand->BuiltinParamTypes; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10561 | } |
| 10562 | |
| 10563 | // Fill in the rest of the conversions. |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10564 | for (unsigned ArgIdx = 0; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10565 | if (Cand->Conversions[ConvIdx].isInitialized()) { |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10566 | // We've already checked this conversion. |
| 10567 | } else if (ArgIdx < ParamTypes.size()) { |
| 10568 | if (ParamTypes[ArgIdx]->isDependentType()) |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10569 | Cand->Conversions[ConvIdx].setAsIdentityConversion( |
| 10570 | Args[ArgIdx]->getType()); |
| 10571 | else { |
| 10572 | Cand->Conversions[ConvIdx] = |
| Richard Smith | 14ead30 | 2017-01-10 20:19:21 +0000 | [diff] [blame] | 10573 | TryCopyInitialization(S, Args[ArgIdx], ParamTypes[ArgIdx], |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 10574 | SuppressUserConversions, |
| 10575 | /*InOverloadResolution=*/true, |
| 10576 | /*AllowObjCWritebackConversion=*/ |
| 10577 | S.getLangOpts().ObjCAutoRefCount); |
| 10578 | // Store the FixIt in the candidate if it exists. |
| 10579 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
| 10580 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
| 10581 | } |
| 10582 | } else |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10583 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 10584 | } |
| 10585 | } |
| 10586 | |
| Erich Keane | c18cce4 | 2018-01-29 19:33:20 +0000 | [diff] [blame] | 10587 | /// When overload resolution fails, prints diagnostic messages containing the |
| 10588 | /// candidates in the candidate set. |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 10589 | void OverloadCandidateSet::NoteCandidates( |
| 10590 | Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args, |
| 10591 | StringRef Opc, SourceLocation OpLoc, |
| 10592 | llvm::function_ref<bool(OverloadCandidate &)> Filter) { |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10593 | // Sort the candidates by viability and position. Sorting directly would |
| 10594 | // be prohibitive, so we make a set of pointers and sort those. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10595 | SmallVector<OverloadCandidate*, 32> Cands; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10596 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 10597 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 10598 | if (!Filter(*Cand)) |
| 10599 | continue; |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10600 | if (Cand->Viable) |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10601 | Cands.push_back(Cand); |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10602 | else if (OCD == OCD_AllCandidates) { |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10603 | CompleteNonViableCandidate(S, Cand, Args); |
| Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 10604 | if (Cand->Function || Cand->IsSurrogate) |
| 10605 | Cands.push_back(Cand); |
| 10606 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 10607 | // want to list every possible builtin candidate. |
| John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 10608 | } |
| 10609 | } |
| 10610 | |
| Mandeep Singh Grang | e66d232 | 2017-11-14 00:22:24 +0000 | [diff] [blame] | 10611 | std::stable_sort(Cands.begin(), Cands.end(), |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 10612 | CompareOverloadCandidatesForDisplay(S, OpLoc, Args.size(), Kind)); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10613 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10614 | bool ReportedAmbiguousConversions = false; |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10615 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10616 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| Douglas Gregor | 7959178 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 10617 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 10618 | unsigned CandsShown = 0; |
| John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 10619 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 10620 | OverloadCandidate *Cand = *I; |
| Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 10621 | |
| Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 10622 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 10623 | // the user with. FIXME: This limit should depend on details of the |
| 10624 | // candidate list. |
| Douglas Gregor | 7959178 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 10625 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
| Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 10626 | break; |
| 10627 | } |
| 10628 | ++CandsShown; |
| 10629 | |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10630 | if (Cand->Function) |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10631 | NoteFunctionCandidate(S, Cand, Args.size(), |
| 10632 | /*TakingCandidateAddress=*/false); |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10633 | else if (Cand->IsSurrogate) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10634 | NoteSurrogateCandidate(S, Cand); |
| Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 10635 | else { |
| 10636 | assert(Cand->Viable && |
| 10637 | "Non-viable built-in candidates are not added to Cands."); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10638 | // Generally we only see ambiguities including viable builtin |
| 10639 | // operators if overload resolution got screwed up by an |
| 10640 | // ambiguous user-defined conversion. |
| 10641 | // |
| 10642 | // FIXME: It's quite possible for different conversions to see |
| 10643 | // different ambiguities, though. |
| 10644 | if (!ReportedAmbiguousConversions) { |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10645 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10646 | ReportedAmbiguousConversions = true; |
| 10647 | } |
| John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 10648 | |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10649 | // If this is a viable builtin, print it. |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10650 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 10651 | } |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 10652 | } |
| Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 10653 | |
| 10654 | if (I != E) |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10655 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
| Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 10656 | } |
| 10657 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10658 | static SourceLocation |
| 10659 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 10660 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 10661 | : SourceLocation(); |
| 10662 | } |
| 10663 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10664 | namespace { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10665 | struct CompareTemplateSpecCandidatesForDisplay { |
| 10666 | Sema &S; |
| 10667 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 10668 | |
| 10669 | bool operator()(const TemplateSpecCandidate *L, |
| 10670 | const TemplateSpecCandidate *R) { |
| 10671 | // Fast-path this check. |
| 10672 | if (L == R) |
| 10673 | return false; |
| 10674 | |
| 10675 | // Assuming that both candidates are not matches... |
| 10676 | |
| 10677 | // Sort by the ranking of deduction failures. |
| 10678 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 10679 | return RankDeductionFailure(L->DeductionFailure) < |
| 10680 | RankDeductionFailure(R->DeductionFailure); |
| 10681 | |
| 10682 | // Sort everything else by location. |
| 10683 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 10684 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 10685 | |
| 10686 | // Put candidates without locations (e.g. builtins) at the end. |
| 10687 | if (LLoc.isInvalid()) |
| 10688 | return false; |
| 10689 | if (RLoc.isInvalid()) |
| 10690 | return true; |
| 10691 | |
| 10692 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 10693 | } |
| 10694 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 10695 | } |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10696 | |
| 10697 | /// Diagnose a template argument deduction failure. |
| 10698 | /// We are treating these failures as overload failures due to bad |
| 10699 | /// deductions. |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10700 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S, |
| 10701 | bool ForTakingAddress) { |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10702 | DiagnoseBadDeduction(S, FoundDecl, Specialization, // pattern |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10703 | DeductionFailure, /*NumArgs=*/0, ForTakingAddress); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10704 | } |
| 10705 | |
| 10706 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 10707 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 10708 | i->DeductionFailure.Destroy(); |
| 10709 | } |
| 10710 | } |
| 10711 | |
| 10712 | void TemplateSpecCandidateSet::clear() { |
| 10713 | destroyCandidates(); |
| 10714 | Candidates.clear(); |
| 10715 | } |
| 10716 | |
| 10717 | /// NoteCandidates - When no template specialization match is found, prints |
| 10718 | /// diagnostic messages containing the non-matching specializations that form |
| 10719 | /// the candidate set. |
| 10720 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 10721 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 10722 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 10723 | // Sort the candidates by position (assuming no candidate is a match). |
| 10724 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 10725 | // and sort those. |
| 10726 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 10727 | Cands.reserve(size()); |
| 10728 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 10729 | if (Cand->Specialization) |
| 10730 | Cands.push_back(Cand); |
| Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 10731 | // Otherwise, this is a non-matching builtin candidate. We do not, |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10732 | // in general, want to list every possible builtin candidate. |
| 10733 | } |
| 10734 | |
| Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 10735 | llvm::sort(Cands.begin(), Cands.end(), |
| 10736 | CompareTemplateSpecCandidatesForDisplay(S)); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10737 | |
| 10738 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 10739 | // for generalization purposes (?). |
| 10740 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 10741 | |
| 10742 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 10743 | unsigned CandsShown = 0; |
| 10744 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 10745 | TemplateSpecCandidate *Cand = *I; |
| 10746 | |
| 10747 | // Set an arbitrary limit on the number of candidates we'll spam |
| 10748 | // the user with. FIXME: This limit should depend on details of the |
| 10749 | // candidate list. |
| 10750 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 10751 | break; |
| 10752 | ++CandsShown; |
| 10753 | |
| 10754 | assert(Cand->Specialization && |
| 10755 | "Non-matching built-in candidates are not added to Cands."); |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10756 | Cand->NoteDeductionFailure(S, ForTakingAddress); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10757 | } |
| 10758 | |
| 10759 | if (I != E) |
| 10760 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 10761 | } |
| 10762 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10763 | // [PossiblyAFunctionType] --> [Return] |
| 10764 | // NonFunctionType --> NonFunctionType |
| 10765 | // R (A) --> R(A) |
| 10766 | // R (*)(A) --> R (A) |
| 10767 | // R (&)(A) --> R (A) |
| 10768 | // R (S::*)(A) --> R (A) |
| 10769 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 10770 | QualType Ret = PossiblyAFunctionType; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10771 | if (const PointerType *ToTypePtr = |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10772 | PossiblyAFunctionType->getAs<PointerType>()) |
| 10773 | Ret = ToTypePtr->getPointeeType(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10774 | else if (const ReferenceType *ToTypeRef = |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10775 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 10776 | Ret = ToTypeRef->getPointeeType(); |
| Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 10777 | else if (const MemberPointerType *MemTypePtr = |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10778 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 10779 | Ret = MemTypePtr->getPointeeType(); |
| 10780 | Ret = |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10781 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 10782 | return Ret; |
| 10783 | } |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10784 | |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 10785 | static bool completeFunctionType(Sema &S, FunctionDecl *FD, SourceLocation Loc, |
| 10786 | bool Complain = true) { |
| 10787 | if (S.getLangOpts().CPlusPlus14 && FD->getReturnType()->isUndeducedType() && |
| 10788 | S.DeduceReturnType(FD, Loc, Complain)) |
| 10789 | return true; |
| 10790 | |
| 10791 | auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 10792 | if (S.getLangOpts().CPlusPlus17 && |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 10793 | isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) && |
| 10794 | !S.ResolveExceptionSpec(Loc, FPT)) |
| 10795 | return true; |
| 10796 | |
| 10797 | return false; |
| 10798 | } |
| 10799 | |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10800 | namespace { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10801 | // A helper class to help with address of function resolution |
| 10802 | // - allows us to avoid passing around all those ugly parameters |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 10803 | class AddressOfFunctionResolver { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10804 | Sema& S; |
| 10805 | Expr* SourceExpr; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10806 | const QualType& TargetType; |
| 10807 | QualType TargetFunctionType; // Extracted function type from target type |
| 10808 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10809 | bool Complain; |
| 10810 | //DeclAccessPair& ResultFunctionAccessPair; |
| 10811 | ASTContext& Context; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10812 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10813 | bool TargetTypeIsNonStaticMemberFunction; |
| 10814 | bool FoundNonTemplateFunction; |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10815 | bool StaticMemberFunctionFromBoundPointer; |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 10816 | bool HasComplained; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10817 | |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10818 | OverloadExpr::FindResult OvlExprInfo; |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10819 | OverloadExpr *OvlExpr; |
| 10820 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10821 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10822 | TemplateSpecCandidateSet FailedCandidates; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10823 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10824 | public: |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10825 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 10826 | const QualType &TargetType, bool Complain) |
| 10827 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 10828 | Complain(Complain), Context(S.getASTContext()), |
| 10829 | TargetTypeIsNonStaticMemberFunction( |
| 10830 | !!TargetType->getAs<MemberPointerType>()), |
| 10831 | FoundNonTemplateFunction(false), |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10832 | StaticMemberFunctionFromBoundPointer(false), |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 10833 | HasComplained(false), |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10834 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 10835 | OvlExpr(OvlExprInfo.Expression), |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10836 | FailedCandidates(OvlExpr->getNameLoc(), /*ForTakingAddress=*/true) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10837 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 10838 | |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10839 | if (TargetFunctionType->isFunctionType()) { |
| 10840 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 10841 | if (!UME->isImplicitAccess() && |
| 10842 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 10843 | StaticMemberFunctionFromBoundPointer = true; |
| 10844 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 10845 | DeclAccessPair dap; |
| 10846 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 10847 | OvlExpr, false, &dap)) { |
| 10848 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 10849 | if (!Method->isStatic()) { |
| 10850 | // If the target type is a non-function type and the function found |
| 10851 | // is a non-static member function, pretend as if that was the |
| 10852 | // target, it's the only possible type to end up with. |
| 10853 | TargetTypeIsNonStaticMemberFunction = true; |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 10854 | |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10855 | // And skip adding the function if its not in the proper form. |
| 10856 | // We'll diagnose this due to an empty set of functions. |
| 10857 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 10858 | return; |
| Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 10859 | } |
| 10860 | |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 10861 | Matches.push_back(std::make_pair(dap, Fn)); |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 10862 | } |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10863 | return; |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 10864 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10865 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10866 | if (OvlExpr->hasExplicitTemplateArgs()) |
| James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 10867 | OvlExpr->copyTemplateArgumentsInto(OvlExplicitTemplateArgs); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10868 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10869 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 10870 | // C++ [over.over]p4: |
| 10871 | // If more than one function is selected, [...] |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 10872 | if (Matches.size() > 1 && !eliminiateSuboptimalOverloadCandidates()) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10873 | if (FoundNonTemplateFunction) |
| 10874 | EliminateAllTemplateMatches(); |
| 10875 | else |
| 10876 | EliminateAllExceptMostSpecializedTemplate(); |
| 10877 | } |
| 10878 | } |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 10879 | |
| Justin Lebar | 25c4a81 | 2016-03-29 16:24:16 +0000 | [diff] [blame] | 10880 | if (S.getLangOpts().CUDA && Matches.size() > 1) |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 10881 | EliminateSuboptimalCudaMatches(); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10882 | } |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 10883 | |
| 10884 | bool hasComplained() const { return HasComplained; } |
| 10885 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10886 | private: |
| George Burgess IV | 6da4c20 | 2016-03-23 02:33:58 +0000 | [diff] [blame] | 10887 | bool candidateHasExactlyCorrectType(const FunctionDecl *FD) { |
| 10888 | QualType Discard; |
| 10889 | return Context.hasSameUnqualifiedType(TargetFunctionType, FD->getType()) || |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 10890 | S.IsFunctionConversion(FD->getType(), TargetFunctionType, Discard); |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 10891 | } |
| 10892 | |
| George Burgess IV | 6da4c20 | 2016-03-23 02:33:58 +0000 | [diff] [blame] | 10893 | /// \return true if A is considered a better overload candidate for the |
| 10894 | /// desired type than B. |
| 10895 | bool isBetterCandidate(const FunctionDecl *A, const FunctionDecl *B) { |
| 10896 | // If A doesn't have exactly the correct type, we don't want to classify it |
| 10897 | // as "better" than anything else. This way, the user is required to |
| 10898 | // disambiguate for us if there are multiple candidates and no exact match. |
| 10899 | return candidateHasExactlyCorrectType(A) && |
| 10900 | (!candidateHasExactlyCorrectType(B) || |
| George Burgess IV | 3dc16691 | 2016-05-10 01:59:34 +0000 | [diff] [blame] | 10901 | compareEnableIfAttrs(S, A, B) == Comparison::Better); |
| George Burgess IV | 6da4c20 | 2016-03-23 02:33:58 +0000 | [diff] [blame] | 10902 | } |
| 10903 | |
| 10904 | /// \return true if we were able to eliminate all but one overload candidate, |
| 10905 | /// false otherwise. |
| George Burgess IV | 2a6150d | 2015-10-16 01:17:38 +0000 | [diff] [blame] | 10906 | bool eliminiateSuboptimalOverloadCandidates() { |
| 10907 | // Same algorithm as overload resolution -- one pass to pick the "best", |
| 10908 | // another pass to be sure that nothing is better than the best. |
| 10909 | auto Best = Matches.begin(); |
| 10910 | for (auto I = Matches.begin()+1, E = Matches.end(); I != E; ++I) |
| 10911 | if (isBetterCandidate(I->second, Best->second)) |
| 10912 | Best = I; |
| 10913 | |
| 10914 | const FunctionDecl *BestFn = Best->second; |
| 10915 | auto IsBestOrInferiorToBest = [this, BestFn]( |
| 10916 | const std::pair<DeclAccessPair, FunctionDecl *> &Pair) { |
| 10917 | return BestFn == Pair.second || isBetterCandidate(BestFn, Pair.second); |
| 10918 | }; |
| 10919 | |
| 10920 | // Note: We explicitly leave Matches unmodified if there isn't a clear best |
| 10921 | // option, so we can potentially give the user a better error |
| 10922 | if (!std::all_of(Matches.begin(), Matches.end(), IsBestOrInferiorToBest)) |
| 10923 | return false; |
| 10924 | Matches[0] = *Best; |
| 10925 | Matches.resize(1); |
| 10926 | return true; |
| 10927 | } |
| 10928 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10929 | bool isTargetTypeAFunction() const { |
| 10930 | return TargetFunctionType->isFunctionType(); |
| 10931 | } |
| 10932 | |
| 10933 | // [ToType] [Return] |
| 10934 | |
| 10935 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 10936 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 10937 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 10938 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 10939 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 10940 | } |
| 10941 | |
| 10942 | // return true if any matching specializations were found |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10943 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10944 | const DeclAccessPair& CurAccessFunPair) { |
| 10945 | if (CXXMethodDecl *Method |
| 10946 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 10947 | // Skip non-static function templates when converting to pointer, and |
| 10948 | // static when converting to member pointer. |
| 10949 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 10950 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10951 | } |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10952 | else if (TargetTypeIsNonStaticMemberFunction) |
| 10953 | return false; |
| 10954 | |
| 10955 | // C++ [over.over]p2: |
| 10956 | // If the name is a function template, template argument deduction is |
| 10957 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 10958 | // resulting template argument list is used to generate a single |
| 10959 | // function template specialization, which is added to the set of |
| 10960 | // overloaded functions considered. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 10961 | FunctionDecl *Specialization = nullptr; |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10962 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10963 | if (Sema::TemplateDeductionResult Result |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10964 | = S.DeduceTemplateArguments(FunctionTemplate, |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10965 | &OvlExplicitTemplateArgs, |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10966 | TargetFunctionType, Specialization, |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 10967 | Info, /*IsAddressOfFunction*/true)) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10968 | // Make a note of the failed deduction for diagnostics. |
| 10969 | FailedCandidates.addCandidate() |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 10970 | .set(CurAccessFunPair, FunctionTemplate->getTemplatedDecl(), |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 10971 | MakeDeductionFailureInfo(Context, Result, Info)); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10972 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10973 | } |
| 10974 | |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 10975 | // Template argument deduction ensures that we have an exact match or |
| 10976 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10977 | // This function template specicalization works. |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 10978 | assert(S.isSameOrCompatibleFunctionType( |
| 10979 | Context.getCanonicalType(Specialization->getType()), |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10980 | Context.getCanonicalType(TargetFunctionType))); |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 10981 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10982 | if (!S.checkAddressOfFunctionIsAvailable(Specialization)) |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 10983 | return false; |
| 10984 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10985 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 10986 | return true; |
| 10987 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10988 | |
| 10989 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10990 | const DeclAccessPair& CurAccessFunPair) { |
| Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 10991 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 10992 | // Skip non-static functions when converting to pointer, and static |
| 10993 | // when converting to member pointer. |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10994 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 10995 | return false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 10996 | } |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10997 | else if (TargetTypeIsNonStaticMemberFunction) |
| 10998 | return false; |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10999 | |
| Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 11000 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11001 | if (S.getLangOpts().CUDA) |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 11002 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| Justin Lebar | b008003 | 2016-08-10 01:09:11 +0000 | [diff] [blame] | 11003 | if (!Caller->isImplicit() && !S.IsAllowedCUDACall(Caller, FunDecl)) |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 11004 | return false; |
| Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 11005 | if (FunDecl->isMultiVersion()) { |
| 11006 | const auto *TA = FunDecl->getAttr<TargetAttr>(); |
| 11007 | assert(TA && "Multiversioned functions require a target attribute"); |
| 11008 | if (!TA->isDefaultVersion()) |
| 11009 | return false; |
| 11010 | } |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 11011 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 11012 | // If any candidate has a placeholder return type, trigger its deduction |
| 11013 | // now. |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 11014 | if (completeFunctionType(S, FunDecl, SourceExpr->getLocStart(), |
| 11015 | Complain)) { |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 11016 | HasComplained |= Complain; |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 11017 | return false; |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 11018 | } |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 11019 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11020 | if (!S.checkAddressOfFunctionIsAvailable(FunDecl)) |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 11021 | return false; |
| 11022 | |
| George Burgess IV | 6da4c20 | 2016-03-23 02:33:58 +0000 | [diff] [blame] | 11023 | // If we're in C, we need to support types that aren't exactly identical. |
| 11024 | if (!S.getLangOpts().CPlusPlus || |
| 11025 | candidateHasExactlyCorrectType(FunDecl)) { |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 11026 | Matches.push_back(std::make_pair( |
| 11027 | CurAccessFunPair, cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
| Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 11028 | FoundNonTemplateFunction = true; |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11029 | return true; |
| Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 11030 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11031 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11032 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11033 | return false; |
| 11034 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11035 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11036 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 11037 | bool Ret = false; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11038 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11039 | // If the overload expression doesn't have the form of a pointer to |
| 11040 | // member, don't try to convert it to a pointer-to-member type. |
| 11041 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 11042 | return false; |
| 11043 | |
| 11044 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11045 | E = OvlExpr->decls_end(); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11046 | I != E; ++I) { |
| 11047 | // Look through any using declarations to find the underlying function. |
| 11048 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 11049 | |
| 11050 | // C++ [over.over]p3: |
| 11051 | // Non-member functions and static member functions match |
| 11052 | // targets of type "pointer-to-function" or "reference-to-function." |
| 11053 | // Nonstatic member functions match targets of |
| 11054 | // type "pointer-to-member-function." |
| 11055 | // Note that according to DR 247, the containing class does not matter. |
| 11056 | if (FunctionTemplateDecl *FunctionTemplate |
| 11057 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 11058 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 11059 | Ret = true; |
| 11060 | } |
| 11061 | // If we have explicit template arguments supplied, skip non-templates. |
| 11062 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 11063 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 11064 | Ret = true; |
| 11065 | } |
| 11066 | assert(Ret || Matches.empty()); |
| 11067 | return Ret; |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11068 | } |
| 11069 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11070 | void EliminateAllExceptMostSpecializedTemplate() { |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11071 | // [...] and any given function template specialization F1 is |
| 11072 | // eliminated if the set contains a second function template |
| 11073 | // specialization whose function template is more specialized |
| 11074 | // than the function template of F1 according to the partial |
| 11075 | // ordering rules of 14.5.5.2. |
| 11076 | |
| 11077 | // The algorithm specified above is quadratic. We instead use a |
| 11078 | // two-pass algorithm (similar to the one used to identify the |
| 11079 | // best viable function in an overload set) that identifies the |
| 11080 | // best function template (if it exists). |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11081 | |
| 11082 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 11083 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 11084 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11085 | |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11086 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 11087 | // here, since the no_viable diagnostic has index 0. |
| 11088 | UnresolvedSetIterator Result = S.getMostSpecialized( |
| Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 11089 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11090 | SourceExpr->getLocStart(), S.PDiag(), |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11091 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 11092 | << Matches[0].second->getDeclName(), |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11093 | S.PDiag(diag::note_ovl_candidate) |
| Eric Fiselier | 92e523b | 2018-05-30 01:00:41 +0000 | [diff] [blame] | 11094 | << (unsigned)oc_function << (unsigned)ocs_described_template, |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11095 | Complain, TargetFunctionType); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11096 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11097 | if (Result != MatchesCopy.end()) { |
| 11098 | // Make it the first and only element |
| 11099 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 11100 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 11101 | Matches.resize(1); |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 11102 | } else |
| 11103 | HasComplained |= Complain; |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 11104 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11105 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11106 | void EliminateAllTemplateMatches() { |
| 11107 | // [...] any function template specializations in the set are |
| 11108 | // eliminated if the set also contains a non-template function, [...] |
| 11109 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11110 | if (Matches[I].second->getPrimaryTemplate() == nullptr) |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11111 | ++I; |
| 11112 | else { |
| 11113 | Matches[I] = Matches[--N]; |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 11114 | Matches.resize(N); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11115 | } |
| 11116 | } |
| 11117 | } |
| 11118 | |
| Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 11119 | void EliminateSuboptimalCudaMatches() { |
| 11120 | S.EraseUnwantedCUDAMatches(dyn_cast<FunctionDecl>(S.CurContext), Matches); |
| 11121 | } |
| 11122 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11123 | public: |
| 11124 | void ComplainNoMatchesFound() const { |
| 11125 | assert(Matches.empty()); |
| 11126 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 11127 | << OvlExpr->getName() << TargetFunctionType |
| 11128 | << OvlExpr->getSourceRange(); |
| Richard Smith | 0d90547 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 11129 | if (FailedCandidates.empty()) |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 11130 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType, |
| 11131 | /*TakingAddress=*/true); |
| Richard Smith | 0d90547 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 11132 | else { |
| 11133 | // We have some deduction failure messages. Use them to diagnose |
| 11134 | // the function templates, and diagnose the non-template candidates |
| 11135 | // normally. |
| 11136 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 11137 | IEnd = OvlExpr->decls_end(); |
| 11138 | I != IEnd; ++I) |
| 11139 | if (FunctionDecl *Fun = |
| 11140 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11141 | if (!functionHasPassObjectSizeParams(Fun)) |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 11142 | S.NoteOverloadCandidate(*I, Fun, TargetFunctionType, |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11143 | /*TakingAddress=*/true); |
| Richard Smith | 0d90547 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 11144 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 11145 | } |
| 11146 | } |
| 11147 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11148 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 11149 | return TargetTypeIsNonStaticMemberFunction && |
| 11150 | !OvlExprInfo.HasFormOfMemberPointer; |
| 11151 | } |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 11152 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11153 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 11154 | // TODO: Should we condition this on whether any functions might |
| 11155 | // have matched, or is it more appropriate to do that in callers? |
| 11156 | // TODO: a fixit wouldn't hurt. |
| 11157 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 11158 | << TargetType << OvlExpr->getSourceRange(); |
| 11159 | } |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 11160 | |
| 11161 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 11162 | return StaticMemberFunctionFromBoundPointer; |
| 11163 | } |
| 11164 | |
| 11165 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 11166 | S.Diag(OvlExpr->getLocStart(), |
| 11167 | diag::err_invalid_form_pointer_member_function) |
| 11168 | << OvlExpr->getSourceRange(); |
| 11169 | } |
| 11170 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11171 | void ComplainOfInvalidConversion() const { |
| 11172 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 11173 | << OvlExpr->getName() << TargetType; |
| 11174 | } |
| 11175 | |
| 11176 | void ComplainMultipleMatchesFound() const { |
| 11177 | assert(Matches.size() > 1); |
| 11178 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 11179 | << OvlExpr->getName() |
| 11180 | << OvlExpr->getSourceRange(); |
| George Burgess IV | 5f21c71 | 2015-10-12 19:57:04 +0000 | [diff] [blame] | 11181 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType, |
| 11182 | /*TakingAddress=*/true); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11183 | } |
| Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 11184 | |
| 11185 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 11186 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11187 | int getNumMatches() const { return Matches.size(); } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11188 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11189 | FunctionDecl* getMatchingFunctionDecl() const { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11190 | if (Matches.size() != 1) return nullptr; |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11191 | return Matches[0].second; |
| 11192 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11193 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11194 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11195 | if (Matches.size() != 1) return nullptr; |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11196 | return &Matches[0].first; |
| 11197 | } |
| 11198 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 11199 | } |
| Richard Smith | 17c00b4 | 2014-11-12 01:24:00 +0000 | [diff] [blame] | 11200 | |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11201 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 11202 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 11203 | /// expression with overloaded function type and @p ToType is the type |
| 11204 | /// we're trying to resolve to. For example: |
| 11205 | /// |
| 11206 | /// @code |
| 11207 | /// int f(double); |
| 11208 | /// int f(int); |
| 11209 | /// |
| 11210 | /// int (*pfd)(double) = f; // selects f(double) |
| 11211 | /// @endcode |
| 11212 | /// |
| 11213 | /// This routine returns the resulting FunctionDecl if it could be |
| 11214 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 11215 | /// routine will emit diagnostics if there is an error. |
| 11216 | FunctionDecl * |
| Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 11217 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 11218 | QualType TargetType, |
| 11219 | bool Complain, |
| 11220 | DeclAccessPair &FoundResult, |
| 11221 | bool *pHadMultipleCandidates) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11222 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
| Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 11223 | |
| 11224 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 11225 | Complain); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11226 | int NumMatches = Resolver.getNumMatches(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11227 | FunctionDecl *Fn = nullptr; |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 11228 | bool ShouldComplain = Complain && !Resolver.hasComplained(); |
| 11229 | if (NumMatches == 0 && ShouldComplain) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11230 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 11231 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 11232 | else |
| 11233 | Resolver.ComplainNoMatchesFound(); |
| 11234 | } |
| George Burgess IV | 5f2ef45 | 2015-10-12 18:40:58 +0000 | [diff] [blame] | 11235 | else if (NumMatches > 1 && ShouldComplain) |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11236 | Resolver.ComplainMultipleMatchesFound(); |
| 11237 | else if (NumMatches == 1) { |
| 11238 | Fn = Resolver.getMatchingFunctionDecl(); |
| 11239 | assert(Fn); |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 11240 | if (auto *FPT = Fn->getType()->getAs<FunctionProtoType>()) |
| 11241 | ResolveExceptionSpec(AddressOfExpr->getExprLoc(), FPT); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11242 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 11243 | if (Complain) { |
| 11244 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 11245 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 11246 | else |
| 11247 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 11248 | } |
| Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 11249 | } |
| Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 11250 | |
| 11251 | if (pHadMultipleCandidates) |
| 11252 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11253 | return Fn; |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11254 | } |
| 11255 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11256 | /// Given an expression that refers to an overloaded function, try to |
| George Burgess IV | 3cde9bf | 2016-03-19 21:36:10 +0000 | [diff] [blame] | 11257 | /// resolve that function to a single function that can have its address taken. |
| 11258 | /// This will modify `Pair` iff it returns non-null. |
| 11259 | /// |
| 11260 | /// This routine can only realistically succeed if all but one candidates in the |
| 11261 | /// overload set for SrcExpr cannot have their addresses taken. |
| 11262 | FunctionDecl * |
| 11263 | Sema::resolveAddressOfOnlyViableOverloadCandidate(Expr *E, |
| 11264 | DeclAccessPair &Pair) { |
| 11265 | OverloadExpr::FindResult R = OverloadExpr::find(E); |
| 11266 | OverloadExpr *Ovl = R.Expression; |
| 11267 | FunctionDecl *Result = nullptr; |
| 11268 | DeclAccessPair DAP; |
| 11269 | // Don't use the AddressOfResolver because we're specifically looking for |
| 11270 | // cases where we have one overload candidate that lacks |
| 11271 | // enable_if/pass_object_size/... |
| 11272 | for (auto I = Ovl->decls_begin(), E = Ovl->decls_end(); I != E; ++I) { |
| 11273 | auto *FD = dyn_cast<FunctionDecl>(I->getUnderlyingDecl()); |
| 11274 | if (!FD) |
| 11275 | return nullptr; |
| 11276 | |
| 11277 | if (!checkAddressOfFunctionIsAvailable(FD)) |
| 11278 | continue; |
| 11279 | |
| 11280 | // We have more than one result; quit. |
| 11281 | if (Result) |
| 11282 | return nullptr; |
| 11283 | DAP = I.getPair(); |
| 11284 | Result = FD; |
| 11285 | } |
| 11286 | |
| 11287 | if (Result) |
| 11288 | Pair = DAP; |
| 11289 | return Result; |
| 11290 | } |
| 11291 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11292 | /// Given an overloaded function, tries to turn it into a non-overloaded |
| George Burgess IV | beca4a3 | 2016-06-08 00:34:22 +0000 | [diff] [blame] | 11293 | /// function reference using resolveAddressOfOnlyViableOverloadCandidate. This |
| 11294 | /// will perform access checks, diagnose the use of the resultant decl, and, if |
| George Burgess IV | 1dbfa85 | 2017-05-09 04:06:24 +0000 | [diff] [blame] | 11295 | /// requested, potentially perform a function-to-pointer decay. |
| George Burgess IV | beca4a3 | 2016-06-08 00:34:22 +0000 | [diff] [blame] | 11296 | /// |
| 11297 | /// Returns false if resolveAddressOfOnlyViableOverloadCandidate fails. |
| 11298 | /// Otherwise, returns true. This may emit diagnostics and return true. |
| 11299 | bool Sema::resolveAndFixAddressOfOnlyViableOverloadCandidate( |
| George Burgess IV | 1dbfa85 | 2017-05-09 04:06:24 +0000 | [diff] [blame] | 11300 | ExprResult &SrcExpr, bool DoFunctionPointerConverion) { |
| George Burgess IV | beca4a3 | 2016-06-08 00:34:22 +0000 | [diff] [blame] | 11301 | Expr *E = SrcExpr.get(); |
| 11302 | assert(E->getType() == Context.OverloadTy && "SrcExpr must be an overload"); |
| 11303 | |
| 11304 | DeclAccessPair DAP; |
| 11305 | FunctionDecl *Found = resolveAddressOfOnlyViableOverloadCandidate(E, DAP); |
| 11306 | if (!Found) |
| 11307 | return false; |
| 11308 | |
| 11309 | // Emitting multiple diagnostics for a function that is both inaccessible and |
| 11310 | // unavailable is consistent with our behavior elsewhere. So, always check |
| 11311 | // for both. |
| 11312 | DiagnoseUseOfDecl(Found, E->getExprLoc()); |
| 11313 | CheckAddressOfMemberAccess(E, DAP); |
| 11314 | Expr *Fixed = FixOverloadedFunctionReference(E, DAP, Found); |
| George Burgess IV | 1dbfa85 | 2017-05-09 04:06:24 +0000 | [diff] [blame] | 11315 | if (DoFunctionPointerConverion && Fixed->getType()->isFunctionType()) |
| George Burgess IV | beca4a3 | 2016-06-08 00:34:22 +0000 | [diff] [blame] | 11316 | SrcExpr = DefaultFunctionArrayConversion(Fixed, /*Diagnose=*/false); |
| 11317 | else |
| 11318 | SrcExpr = Fixed; |
| 11319 | return true; |
| 11320 | } |
| 11321 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11322 | /// Given an expression that refers to an overloaded function, try to |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11323 | /// resolve that overloaded function expression down to a single function. |
| 11324 | /// |
| 11325 | /// This routine can only resolve template-ids that refer to a single function |
| 11326 | /// template, where that template-id refers to a single template whose template |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11327 | /// arguments are either provided by the template-id or have defaults, |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11328 | /// as described in C++0x [temp.arg.explicit]p3. |
| Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 11329 | /// |
| 11330 | /// If no template-ids are found, no diagnostics are emitted and NULL is |
| 11331 | /// returned. |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11332 | FunctionDecl * |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11333 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11334 | bool Complain, |
| 11335 | DeclAccessPair *FoundResult) { |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11336 | // C++ [over.over]p1: |
| 11337 | // [...] [Note: any redundant set of parentheses surrounding the |
| 11338 | // overloaded function name is ignored (5.1). ] |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11339 | // C++ [over.over]p1: |
| 11340 | // [...] The overloaded function name can be preceded by the & |
| 11341 | // operator. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11342 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11343 | // If we didn't actually find any template-ids, we're done. |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11344 | if (!ovl->hasExplicitTemplateArgs()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11345 | return nullptr; |
| John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 11346 | |
| 11347 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 11348 | ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11349 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11350 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11351 | // Look through all of the overloaded functions, searching for one |
| 11352 | // whose type matches exactly. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11353 | FunctionDecl *Matched = nullptr; |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11354 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 11355 | E = ovl->decls_end(); I != E; ++I) { |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11356 | // C++0x [temp.arg.explicit]p3: |
| 11357 | // [...] In contexts where deduction is done and fails, or in contexts |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11358 | // where deduction is not done, if a template argument list is |
| 11359 | // specified and it, along with any default template arguments, |
| 11360 | // identifies a single function template specialization, then the |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11361 | // template-id is an lvalue for the function template specialization. |
| Douglas Gregor | eebe721 | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 11362 | FunctionTemplateDecl *FunctionTemplate |
| 11363 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11364 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11365 | // C++ [over.over]p2: |
| 11366 | // If the name is a function template, template argument deduction is |
| 11367 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 11368 | // resulting template argument list is used to generate a single |
| 11369 | // function template specialization, which is added to the set of |
| 11370 | // overloaded functions considered. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11371 | FunctionDecl *Specialization = nullptr; |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11372 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11373 | if (TemplateDeductionResult Result |
| 11374 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 11375 | Specialization, Info, |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 11376 | /*IsAddressOfFunction*/true)) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11377 | // Make a note of the failed deduction for diagnostics. |
| 11378 | // TODO: Actually use the failed-deduction info? |
| 11379 | FailedCandidates.addCandidate() |
| Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 11380 | .set(I.getPair(), FunctionTemplate->getTemplatedDecl(), |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 11381 | MakeDeductionFailureInfo(Context, Result, Info)); |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11382 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11383 | } |
| 11384 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11385 | assert(Specialization && "no specialization and no error?"); |
| 11386 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11387 | // Multiple matches; we can't resolve to a single declaration. |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11388 | if (Matched) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11389 | if (Complain) { |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11390 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 11391 | << ovl->getName(); |
| 11392 | NoteAllOverloadCandidates(ovl); |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 11393 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11394 | return nullptr; |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11395 | } |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11396 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11397 | Matched = Specialization; |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11398 | if (FoundResult) *FoundResult = I.getPair(); |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11399 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11400 | |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 11401 | if (Matched && |
| 11402 | completeFunctionType(*this, Matched, ovl->getExprLoc(), Complain)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11403 | return nullptr; |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 11404 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 11405 | return Matched; |
| 11406 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11407 | |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11408 | // Resolve and fix an overloaded expression that can be resolved |
| 11409 | // because it identifies a single function template specialization. |
| 11410 | // |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 11411 | // Last three arguments should only be supplied if Complain = true |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11412 | // |
| 11413 | // Return true if it was logically possible to so resolve the |
| 11414 | // expression, regardless of whether or not it succeeded. Always |
| 11415 | // returns true if 'complain' is set. |
| 11416 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 11417 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11418 | bool complain, SourceRange OpRangeForComplaining, |
| 11419 | QualType DestTypeForComplaining, |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11420 | unsigned DiagIDForComplaining) { |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11421 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 11422 | |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11423 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 11424 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11425 | DeclAccessPair found; |
| 11426 | ExprResult SingleFunctionExpression; |
| 11427 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 11428 | ovl.Expression, /*complain*/ false, &found)) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11429 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11430 | SrcExpr = ExprError(); |
| 11431 | return true; |
| 11432 | } |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11433 | |
| 11434 | // It is only correct to resolve to an instance method if we're |
| 11435 | // resolving a form that's permitted to be a pointer to member. |
| 11436 | // Otherwise we'll end up making a bound member expression, which |
| 11437 | // is illegal in all the contexts we resolve like this. |
| 11438 | if (!ovl.HasFormOfMemberPointer && |
| 11439 | isa<CXXMethodDecl>(fn) && |
| 11440 | cast<CXXMethodDecl>(fn)->isInstance()) { |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11441 | if (!complain) return false; |
| 11442 | |
| 11443 | Diag(ovl.Expression->getExprLoc(), |
| 11444 | diag::err_bound_member_function) |
| 11445 | << 0 << ovl.Expression->getSourceRange(); |
| 11446 | |
| 11447 | // TODO: I believe we only end up here if there's a mix of |
| 11448 | // static and non-static candidates (otherwise the expression |
| 11449 | // would have 'bound member' type, not 'overload' type). |
| 11450 | // Ideally we would note which candidate was chosen and why |
| 11451 | // the static candidates were rejected. |
| 11452 | SrcExpr = ExprError(); |
| 11453 | return true; |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 11454 | } |
| Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 11455 | |
| Sylvestre Ledru | a520266 | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 11456 | // Fix the expression to refer to 'fn'. |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11457 | SingleFunctionExpression = |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 11458 | FixOverloadedFunctionReference(SrcExpr.get(), found, fn); |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11459 | |
| 11460 | // If desired, do function-to-pointer decay. |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11461 | if (doFunctionPointerConverion) { |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11462 | SingleFunctionExpression = |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 11463 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.get()); |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11464 | if (SingleFunctionExpression.isInvalid()) { |
| 11465 | SrcExpr = ExprError(); |
| 11466 | return true; |
| 11467 | } |
| 11468 | } |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11469 | } |
| 11470 | |
| 11471 | if (!SingleFunctionExpression.isUsable()) { |
| 11472 | if (complain) { |
| 11473 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 11474 | << ovl.Expression->getName() |
| 11475 | << DestTypeForComplaining |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 11476 | << OpRangeForComplaining |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11477 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11478 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 11479 | |
| 11480 | SrcExpr = ExprError(); |
| 11481 | return true; |
| 11482 | } |
| 11483 | |
| 11484 | return false; |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11485 | } |
| 11486 | |
| John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11487 | SrcExpr = SingleFunctionExpression; |
| 11488 | return true; |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 11489 | } |
| 11490 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11491 | /// Add a single candidate to the overload set. |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11492 | static void AddOverloadedCallCandidate(Sema &S, |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11493 | DeclAccessPair FoundDecl, |
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 11494 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11495 | ArrayRef<Expr *> Args, |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11496 | OverloadCandidateSet &CandidateSet, |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11497 | bool PartialOverloading, |
| 11498 | bool KnownValid) { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11499 | NamedDecl *Callee = FoundDecl.getDecl(); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11500 | if (isa<UsingShadowDecl>(Callee)) |
| 11501 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 11502 | |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11503 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11504 | if (ExplicitTemplateArgs) { |
| 11505 | assert(!KnownValid && "Explicit template arguments?"); |
| 11506 | return; |
| 11507 | } |
| Bruno Cardoso Lopes | 3702963 | 2017-04-26 20:13:45 +0000 | [diff] [blame] | 11508 | // Prevent ill-formed function decls to be added as overload candidates. |
| 11509 | if (!dyn_cast<FunctionProtoType>(Func->getType()->getAs<FunctionType>())) |
| 11510 | return; |
| 11511 | |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 11512 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, |
| 11513 | /*SuppressUsedConversions=*/false, |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11514 | PartialOverloading); |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11515 | return; |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11516 | } |
| 11517 | |
| 11518 | if (FunctionTemplateDecl *FuncTemplate |
| 11519 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11520 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 11521 | ExplicitTemplateArgs, Args, CandidateSet, |
| 11522 | /*SuppressUsedConversions=*/false, |
| 11523 | PartialOverloading); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11524 | return; |
| 11525 | } |
| 11526 | |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11527 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11528 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11529 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11530 | /// Add the overload candidates named by callee and/or found by argument |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11531 | /// dependent lookup to the given overload set. |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11532 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11533 | ArrayRef<Expr *> Args, |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11534 | OverloadCandidateSet &CandidateSet, |
| 11535 | bool PartialOverloading) { |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11536 | |
| 11537 | #ifndef NDEBUG |
| 11538 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 11539 | // in C++0x [basic.lookup.argdep]p3: |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11540 | // |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11541 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 11542 | // and let Y be the lookup set produced by argument dependent |
| 11543 | // lookup (defined as follows). If X contains |
| 11544 | // |
| 11545 | // -- a declaration of a class member, or |
| 11546 | // |
| 11547 | // -- a block-scope function declaration that is not a |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11548 | // using-declaration, or |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11549 | // |
| 11550 | // -- a declaration that is neither a function or a function |
| 11551 | // template |
| 11552 | // |
| 11553 | // then Y is empty. |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11554 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11555 | if (ULE->requiresADL()) { |
| 11556 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 11557 | E = ULE->decls_end(); I != E; ++I) { |
| 11558 | assert(!(*I)->getDeclContext()->isRecord()); |
| 11559 | assert(isa<UsingShadowDecl>(*I) || |
| 11560 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 11561 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11562 | } |
| 11563 | } |
| 11564 | #endif |
| 11565 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11566 | // It would be nice to avoid this copy. |
| 11567 | TemplateArgumentListInfo TABuffer; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11568 | TemplateArgumentListInfo *ExplicitTemplateArgs = nullptr; |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11569 | if (ULE->hasExplicitTemplateArgs()) { |
| 11570 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 11571 | ExplicitTemplateArgs = &TABuffer; |
| 11572 | } |
| 11573 | |
| 11574 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 11575 | E = ULE->decls_end(); I != E; ++I) |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11576 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 11577 | CandidateSet, PartialOverloading, |
| 11578 | /*KnownValid*/ true); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11579 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11580 | if (ULE->requiresADL()) |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 11581 | AddArgumentDependentLookupCandidates(ULE->getName(), ULE->getExprLoc(), |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11582 | Args, ExplicitTemplateArgs, |
| Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11583 | CandidateSet, PartialOverloading); |
| Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 11584 | } |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11585 | |
| Richard Smith | 0603bbb | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 11586 | /// Determine whether a declaration with the specified name could be moved into |
| 11587 | /// a different namespace. |
| 11588 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 11589 | switch (Name.getCXXOverloadedOperator()) { |
| 11590 | case OO_New: case OO_Array_New: |
| 11591 | case OO_Delete: case OO_Array_Delete: |
| 11592 | return false; |
| 11593 | |
| 11594 | default: |
| 11595 | return true; |
| 11596 | } |
| 11597 | } |
| 11598 | |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11599 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 11600 | /// template, where the non-dependent name was declared after the template |
| 11601 | /// was defined. This is common in code written for a compilers which do not |
| 11602 | /// correctly implement two-stage name lookup. |
| 11603 | /// |
| 11604 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 11605 | static bool |
| 11606 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 11607 | const CXXScopeSpec &SS, LookupResult &R, |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 11608 | OverloadCandidateSet::CandidateSetKind CSK, |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11609 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| Hans Wennborg | 64937c6 | 2015-06-11 21:21:57 +0000 | [diff] [blame] | 11610 | ArrayRef<Expr *> Args, |
| 11611 | bool *DoDiagnoseEmptyLookup = nullptr) { |
| Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 11612 | if (!SemaRef.inTemplateInstantiation() || !SS.isEmpty()) |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11613 | return false; |
| 11614 | |
| 11615 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
| Nick Lewycky | fcd5e7a | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 11616 | if (DC->isTransparentContext()) |
| 11617 | continue; |
| 11618 | |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11619 | SemaRef.LookupQualifiedName(R, DC); |
| 11620 | |
| 11621 | if (!R.empty()) { |
| 11622 | R.suppressDiagnostics(); |
| 11623 | |
| 11624 | if (isa<CXXRecordDecl>(DC)) { |
| 11625 | // Don't diagnose names we find in classes; we get much better |
| 11626 | // diagnostics for these from DiagnoseEmptyLookup. |
| 11627 | R.clear(); |
| Hans Wennborg | 64937c6 | 2015-06-11 21:21:57 +0000 | [diff] [blame] | 11628 | if (DoDiagnoseEmptyLookup) |
| 11629 | *DoDiagnoseEmptyLookup = true; |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11630 | return false; |
| 11631 | } |
| 11632 | |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 11633 | OverloadCandidateSet Candidates(FnLoc, CSK); |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11634 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 11635 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11636 | ExplicitTemplateArgs, Args, |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11637 | Candidates, false, /*KnownValid*/ false); |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11638 | |
| 11639 | OverloadCandidateSet::iterator Best; |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11640 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11641 | // No viable functions. Don't bother the user with notes for functions |
| 11642 | // which don't work and shouldn't be found anyway. |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11643 | R.clear(); |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11644 | return false; |
| Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 11645 | } |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11646 | |
| 11647 | // Find the namespaces where ADL would have looked, and suggest |
| 11648 | // declaring the function there instead. |
| 11649 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 11650 | Sema::AssociatedClassSet AssociatedClasses; |
| John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 11651 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11652 | AssociatedNamespaces, |
| 11653 | AssociatedClasses); |
| Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 11654 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
| Richard Smith | 0603bbb | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 11655 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 11656 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 11657 | for (Sema::AssociatedNamespaceSet::iterator |
| 11658 | it = AssociatedNamespaces.begin(), |
| 11659 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 11660 | // Never suggest declaring a function within namespace 'std'. |
| 11661 | if (Std && Std->Encloses(*it)) |
| 11662 | continue; |
| Richard Smith | 21bae43 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 11663 | |
| Richard Smith | 0603bbb | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 11664 | // Never suggest declaring a function within a namespace with a |
| 11665 | // reserved name, like __gnu_cxx. |
| 11666 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 11667 | if (NS && |
| 11668 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 11669 | continue; |
| 11670 | |
| 11671 | SuggestedNamespaces.insert(*it); |
| 11672 | } |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11673 | } |
| 11674 | |
| 11675 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 11676 | << R.getLookupName(); |
| Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 11677 | if (SuggestedNamespaces.empty()) { |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11678 | SemaRef.Diag(Best->Function->getLocation(), |
| 11679 | diag::note_not_found_by_two_phase_lookup) |
| 11680 | << R.getLookupName() << 0; |
| Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 11681 | } else if (SuggestedNamespaces.size() == 1) { |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11682 | SemaRef.Diag(Best->Function->getLocation(), |
| 11683 | diag::note_not_found_by_two_phase_lookup) |
| Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 11684 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11685 | } else { |
| 11686 | // FIXME: It would be useful to list the associated namespaces here, |
| 11687 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 11688 | // a localized representation of a list of items. |
| 11689 | SemaRef.Diag(Best->Function->getLocation(), |
| 11690 | diag::note_not_found_by_two_phase_lookup) |
| 11691 | << R.getLookupName() << 2; |
| 11692 | } |
| 11693 | |
| 11694 | // Try to recover by calling this function. |
| 11695 | return true; |
| 11696 | } |
| 11697 | |
| 11698 | R.clear(); |
| 11699 | } |
| 11700 | |
| 11701 | return false; |
| 11702 | } |
| 11703 | |
| 11704 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 11705 | /// template, where the non-dependent operator was declared after the template |
| 11706 | /// was defined. |
| 11707 | /// |
| 11708 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 11709 | static bool |
| 11710 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 11711 | SourceLocation OpLoc, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11712 | ArrayRef<Expr *> Args) { |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11713 | DeclarationName OpName = |
| 11714 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 11715 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 11716 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 11717 | OverloadCandidateSet::CSK_Operator, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11718 | /*ExplicitTemplateArgs=*/nullptr, Args); |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11719 | } |
| 11720 | |
| Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 11721 | namespace { |
| Richard Smith | 88d67f3 | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 11722 | class BuildRecoveryCallExprRAII { |
| 11723 | Sema &SemaRef; |
| 11724 | public: |
| 11725 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 11726 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 11727 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 11728 | } |
| 11729 | |
| 11730 | ~BuildRecoveryCallExprRAII() { |
| 11731 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 11732 | } |
| 11733 | }; |
| 11734 | |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 11735 | } |
| Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 11736 | |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 11737 | static std::unique_ptr<CorrectionCandidateCallback> |
| 11738 | MakeValidator(Sema &SemaRef, MemberExpr *ME, size_t NumArgs, |
| 11739 | bool HasTemplateArgs, bool AllowTypoCorrection) { |
| 11740 | if (!AllowTypoCorrection) |
| 11741 | return llvm::make_unique<NoTypoCorrectionCCC>(); |
| 11742 | return llvm::make_unique<FunctionCallFilterCCC>(SemaRef, NumArgs, |
| 11743 | HasTemplateArgs, ME); |
| 11744 | } |
| 11745 | |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11746 | /// Attempts to recover from a call where no functions were found. |
| 11747 | /// |
| 11748 | /// Returns true if new candidates were found. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11749 | static ExprResult |
| Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 11750 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11751 | UnresolvedLookupExpr *ULE, |
| 11752 | SourceLocation LParenLoc, |
| Craig Topper | e3d2ecbe | 2014-06-28 23:22:33 +0000 | [diff] [blame] | 11753 | MutableArrayRef<Expr *> Args, |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11754 | SourceLocation RParenLoc, |
| Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 11755 | bool EmptyLookup, bool AllowTypoCorrection) { |
| Richard Smith | 88d67f3 | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 11756 | // Do not try to recover if it is already building a recovery call. |
| 11757 | // This stops infinite loops for template instantiations like |
| 11758 | // |
| 11759 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 11760 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 11761 | // |
| 11762 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 11763 | return ExprError(); |
| 11764 | BuildRecoveryCallExprRAII RCE(SemaRef); |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11765 | |
| 11766 | CXXScopeSpec SS; |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11767 | SS.Adopt(ULE->getQualifierLoc()); |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11768 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11769 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11770 | TemplateArgumentListInfo TABuffer; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 11771 | TemplateArgumentListInfo *ExplicitTemplateArgs = nullptr; |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11772 | if (ULE->hasExplicitTemplateArgs()) { |
| 11773 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 11774 | ExplicitTemplateArgs = &TABuffer; |
| 11775 | } |
| 11776 | |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11777 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 11778 | Sema::LookupOrdinaryName); |
| Hans Wennborg | 64937c6 | 2015-06-11 21:21:57 +0000 | [diff] [blame] | 11779 | bool DoDiagnoseEmptyLookup = EmptyLookup; |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11780 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 11781 | OverloadCandidateSet::CSK_Normal, |
| Hans Wennborg | 64937c6 | 2015-06-11 21:21:57 +0000 | [diff] [blame] | 11782 | ExplicitTemplateArgs, Args, |
| 11783 | &DoDiagnoseEmptyLookup) && |
| 11784 | (!DoDiagnoseEmptyLookup || SemaRef.DiagnoseEmptyLookup( |
| 11785 | S, SS, R, |
| 11786 | MakeValidator(SemaRef, dyn_cast<MemberExpr>(Fn), Args.size(), |
| 11787 | ExplicitTemplateArgs != nullptr, AllowTypoCorrection), |
| 11788 | ExplicitTemplateArgs, Args))) |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11789 | return ExprError(); |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11790 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11791 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 11792 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 11793 | // If recovery created an ambiguity, just bail out. |
| 11794 | if (R.isAmbiguous()) { |
| 11795 | R.suppressDiagnostics(); |
| 11796 | return ExprError(); |
| 11797 | } |
| 11798 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11799 | // Build an implicit member call if appropriate. Just drop the |
| 11800 | // casts and such from the call, we don't really care. |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11801 | ExprResult NewFn = ExprError(); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11802 | if ((*R.begin())->isCXXClassMember()) |
| Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 11803 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, R, |
| 11804 | ExplicitTemplateArgs, S); |
| Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 11805 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11806 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
| Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 11807 | ExplicitTemplateArgs); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11808 | else |
| 11809 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 11810 | |
| 11811 | if (NewFn.isInvalid()) |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11812 | return ExprError(); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11813 | |
| 11814 | // This shouldn't cause an infinite loop because we're giving it |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11815 | // an expression with viable lookup results, which should never |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11816 | // end up here. |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 11817 | return SemaRef.ActOnCallExpr(/*Scope*/ nullptr, NewFn.get(), LParenLoc, |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11818 | MultiExprArg(Args.data(), Args.size()), |
| 11819 | RParenLoc); |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11820 | } |
| Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 11821 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11822 | /// Constructs and populates an OverloadedCandidateSet from |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11823 | /// the given function. |
| 11824 | /// \returns true when an the ExprResult output parameter has been set. |
| 11825 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 11826 | UnresolvedLookupExpr *ULE, |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11827 | MultiExprArg Args, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11828 | SourceLocation RParenLoc, |
| 11829 | OverloadCandidateSet *CandidateSet, |
| 11830 | ExprResult *Result) { |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11831 | #ifndef NDEBUG |
| 11832 | if (ULE->requiresADL()) { |
| 11833 | // To do ADL, we must have found an unqualified name. |
| 11834 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 11835 | |
| 11836 | // We don't perform ADL for implicit declarations of builtins. |
| 11837 | // Verify that this was correctly set up. |
| 11838 | FunctionDecl *F; |
| 11839 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 11840 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 11841 | F->getBuiltinID() && F->isImplicit()) |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11842 | llvm_unreachable("performing ADL for builtin"); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11843 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11844 | // We don't perform ADL in C. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11845 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
| Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11846 | } |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11847 | #endif |
| 11848 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11849 | UnbridgedCastsSet UnbridgedCasts; |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11850 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11851 | *Result = ExprError(); |
| 11852 | return true; |
| 11853 | } |
| Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 11854 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11855 | // Add the functions denoted by the callee to the set of candidate |
| 11856 | // functions, including those from argument-dependent lookup. |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11857 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11858 | |
| Hans Wennborg | b274738 | 2015-06-12 21:23:23 +0000 | [diff] [blame] | 11859 | if (getLangOpts().MSVCCompat && |
| 11860 | CurContext->isDependentContext() && !isSFINAEContext() && |
| Hans Wennborg | 64937c6 | 2015-06-11 21:21:57 +0000 | [diff] [blame] | 11861 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
| 11862 | |
| 11863 | OverloadCandidateSet::iterator Best; |
| 11864 | if (CandidateSet->empty() || |
| 11865 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best) == |
| 11866 | OR_No_Viable_Function) { |
| 11867 | // In Microsoft mode, if we are inside a template class member function then |
| 11868 | // create a type dependent CallExpr. The goal is to postpone name lookup |
| 11869 | // to instantiation time to be able to search into type dependent base |
| 11870 | // classes. |
| 11871 | CallExpr *CE = new (Context) CallExpr( |
| 11872 | Context, Fn, Args, Context.DependentTy, VK_RValue, RParenLoc); |
| Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 11873 | CE->setTypeDependent(true); |
| Richard Smith | ed9b8f0 | 2015-09-23 21:30:47 +0000 | [diff] [blame] | 11874 | CE->setValueDependent(true); |
| 11875 | CE->setInstantiationDependent(true); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 11876 | *Result = CE; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11877 | return true; |
| Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 11878 | } |
| Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 11879 | } |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 11880 | |
| Hans Wennborg | 64937c6 | 2015-06-11 21:21:57 +0000 | [diff] [blame] | 11881 | if (CandidateSet->empty()) |
| 11882 | return false; |
| 11883 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11884 | UnbridgedCasts.restore(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11885 | return false; |
| 11886 | } |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11887 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11888 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 11889 | /// the completed call expression. If overload resolution fails, emits |
| 11890 | /// diagnostics and returns ExprError() |
| 11891 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 11892 | UnresolvedLookupExpr *ULE, |
| 11893 | SourceLocation LParenLoc, |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11894 | MultiExprArg Args, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11895 | SourceLocation RParenLoc, |
| 11896 | Expr *ExecConfig, |
| 11897 | OverloadCandidateSet *CandidateSet, |
| 11898 | OverloadCandidateSet::iterator *Best, |
| 11899 | OverloadingResult OverloadResult, |
| 11900 | bool AllowTypoCorrection) { |
| 11901 | if (CandidateSet->empty()) |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11902 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11903 | RParenLoc, /*EmptyLookup=*/true, |
| 11904 | AllowTypoCorrection); |
| 11905 | |
| 11906 | switch (OverloadResult) { |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11907 | case OR_Success: { |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11908 | FunctionDecl *FDecl = (*Best)->Function; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11909 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
| Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11910 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 11911 | return ExprError(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11912 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11913 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 11914 | ExecConfig); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11915 | } |
| Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 11916 | |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11917 | case OR_No_Viable_Function: { |
| 11918 | // Try to recover by looking for viable functions which the user might |
| 11919 | // have meant to call. |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11920 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11921 | Args, RParenLoc, |
| Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 11922 | /*EmptyLookup=*/false, |
| 11923 | AllowTypoCorrection); |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11924 | if (!Recovery.isInvalid()) |
| 11925 | return Recovery; |
| 11926 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11927 | // If the user passes in a function that we can't take the address of, we |
| 11928 | // generally end up emitting really bad error messages. Here, we attempt to |
| 11929 | // emit better ones. |
| 11930 | for (const Expr *Arg : Args) { |
| 11931 | if (!Arg->getType()->isFunctionType()) |
| 11932 | continue; |
| 11933 | if (auto *DRE = dyn_cast<DeclRefExpr>(Arg->IgnoreParenImpCasts())) { |
| 11934 | auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 11935 | if (FD && |
| 11936 | !SemaRef.checkAddressOfFunctionIsAvailable(FD, /*Complain=*/true, |
| 11937 | Arg->getExprLoc())) |
| 11938 | return ExprError(); |
| 11939 | } |
| 11940 | } |
| 11941 | |
| 11942 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_no_viable_function_in_call) |
| 11943 | << ULE->getName() << Fn->getSourceRange(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11944 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
| Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 11945 | break; |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11946 | } |
| Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 11947 | |
| 11948 | case OR_Ambiguous: |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11949 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11950 | << ULE->getName() << Fn->getSourceRange(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11951 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
| Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 11952 | break; |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11953 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11954 | case OR_Deleted: { |
| 11955 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 11956 | << (*Best)->Function->isDeleted() |
| 11957 | << ULE->getName() |
| 11958 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 11959 | << Fn->getSourceRange(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11960 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
| Argyrios Kyrtzidis | 3eaa22a | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 11961 | |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 11962 | // We emitted an error for the unavailable/deleted function call but keep |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11963 | // the call in the AST. |
| 11964 | FunctionDecl *FDecl = (*Best)->Function; |
| 11965 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11966 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 11967 | ExecConfig); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11968 | } |
| Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 11969 | } |
| 11970 | |
| Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11971 | // Overload resolution failed. |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 11972 | return ExprError(); |
| Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 11973 | } |
| 11974 | |
| George Burgess IV | 7204ed9 | 2016-01-07 02:26:57 +0000 | [diff] [blame] | 11975 | static void markUnaddressableCandidatesUnviable(Sema &S, |
| 11976 | OverloadCandidateSet &CS) { |
| 11977 | for (auto I = CS.begin(), E = CS.end(); I != E; ++I) { |
| 11978 | if (I->Viable && |
| 11979 | !S.checkAddressOfFunctionIsAvailable(I->Function, /*Complain=*/false)) { |
| 11980 | I->Viable = false; |
| 11981 | I->FailureKind = ovl_fail_addr_not_available; |
| 11982 | } |
| 11983 | } |
| 11984 | } |
| 11985 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11986 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 11987 | /// (which eventually refers to the declaration Func) and the call |
| 11988 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 11989 | /// to a specific function. If overload resolution succeeds, returns |
| 11990 | /// the call expression produced by overload resolution. |
| 11991 | /// Otherwise, emits diagnostics and returns ExprError. |
| 11992 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 11993 | UnresolvedLookupExpr *ULE, |
| 11994 | SourceLocation LParenLoc, |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11995 | MultiExprArg Args, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11996 | SourceLocation RParenLoc, |
| 11997 | Expr *ExecConfig, |
| George Burgess IV | 7204ed9 | 2016-01-07 02:26:57 +0000 | [diff] [blame] | 11998 | bool AllowTypoCorrection, |
| 11999 | bool CalleesAddressIsTaken) { |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 12000 | OverloadCandidateSet CandidateSet(Fn->getExprLoc(), |
| 12001 | OverloadCandidateSet::CSK_Normal); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12002 | ExprResult result; |
| 12003 | |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12004 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 12005 | &result)) |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12006 | return result; |
| 12007 | |
| George Burgess IV | 7204ed9 | 2016-01-07 02:26:57 +0000 | [diff] [blame] | 12008 | // If the user handed us something like `(&Foo)(Bar)`, we need to ensure that |
| 12009 | // functions that aren't addressible are considered unviable. |
| 12010 | if (CalleesAddressIsTaken) |
| 12011 | markUnaddressableCandidatesUnviable(*this, CandidateSet); |
| 12012 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12013 | OverloadCandidateSet::iterator Best; |
| 12014 | OverloadingResult OverloadResult = |
| 12015 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 12016 | |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12017 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12018 | RParenLoc, ExecConfig, &CandidateSet, |
| 12019 | &Best, OverloadResult, |
| 12020 | AllowTypoCorrection); |
| 12021 | } |
| 12022 | |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12023 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
| John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 12024 | return Functions.size() > 1 || |
| 12025 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 12026 | } |
| 12027 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 12028 | /// Create a unary operation that may resolve to an overloaded |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12029 | /// operator. |
| 12030 | /// |
| 12031 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 12032 | /// |
| Craig Topper | a92ffb0 | 2015-12-10 08:51:49 +0000 | [diff] [blame] | 12033 | /// \param Opc The UnaryOperatorKind that describes this operator. |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12034 | /// |
| James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 12035 | /// \param Fns The set of non-member functions that will be |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12036 | /// considered by overload resolution. The caller needs to build this |
| 12037 | /// set based on the context using, e.g., |
| 12038 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 12039 | /// set should not contain any member functions; those will be added |
| 12040 | /// by CreateOverloadedUnaryOp(). |
| 12041 | /// |
| James Dennett | 91738ff | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 12042 | /// \param Input The input argument. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12043 | ExprResult |
| Craig Topper | a92ffb0 | 2015-12-10 08:51:49 +0000 | [diff] [blame] | 12044 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc, |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12045 | const UnresolvedSetImpl &Fns, |
| Richard Smith | 91fc7d8 | 2017-10-05 19:35:51 +0000 | [diff] [blame] | 12046 | Expr *Input, bool PerformADL) { |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12047 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 12048 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 12049 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 12050 | // TODO: provide better source location info. |
| 12051 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12052 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12053 | if (checkPlaceholderForOverload(*this, Input)) |
| 12054 | return ExprError(); |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 12055 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12056 | Expr *Args[2] = { Input, nullptr }; |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12057 | unsigned NumArgs = 1; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12058 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12059 | // For post-increment and post-decrement, add the implicit '0' as |
| 12060 | // the second argument, so that we know this is a post-increment or |
| 12061 | // post-decrement. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12062 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12063 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
| Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 12064 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 12065 | SourceLocation()); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12066 | NumArgs = 2; |
| 12067 | } |
| 12068 | |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12069 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 12070 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12071 | if (Input->isTypeDependent()) { |
| Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 12072 | if (Fns.empty()) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12073 | return new (Context) UnaryOperator(Input, Opc, Context.DependentTy, |
| Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 12074 | VK_RValue, OK_Ordinary, OpLoc, false); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12075 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12076 | CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12077 | UnresolvedLookupExpr *Fn |
| Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 12078 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 12079 | NestedNameSpecifierLoc(), OpNameInfo, |
| Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 12080 | /*ADL*/ true, IsOverloaded(Fns), |
| 12081 | Fns.begin(), Fns.end()); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12082 | return new (Context) |
| 12083 | CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, Context.DependentTy, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12084 | VK_RValue, OpLoc, FPOptions()); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12085 | } |
| 12086 | |
| 12087 | // Build an empty overload set. |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 12088 | OverloadCandidateSet CandidateSet(OpLoc, OverloadCandidateSet::CSK_Operator); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12089 | |
| 12090 | // Add the candidates from the given function set. |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 12091 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12092 | |
| 12093 | // Add operator candidates that are member functions. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12094 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12095 | |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12096 | // Add candidates from ADL. |
| Richard Smith | 91fc7d8 | 2017-10-05 19:35:51 +0000 | [diff] [blame] | 12097 | if (PerformADL) { |
| 12098 | AddArgumentDependentLookupCandidates(OpName, OpLoc, ArgsArray, |
| 12099 | /*ExplicitTemplateArgs*/nullptr, |
| 12100 | CandidateSet); |
| 12101 | } |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12102 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12103 | // Add builtin operator candidates. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12104 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12105 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12106 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 12107 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12108 | // Perform overload resolution. |
| 12109 | OverloadCandidateSet::iterator Best; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12110 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12111 | case OR_Success: { |
| 12112 | // We found a built-in operator or an overloaded operator. |
| 12113 | FunctionDecl *FnDecl = Best->Function; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12114 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12115 | if (FnDecl) { |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12116 | Expr *Base = nullptr; |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12117 | // We matched an overloaded operator. Build a call to that |
| 12118 | // operator. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12119 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12120 | // Convert the arguments. |
| 12121 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12122 | CheckMemberOperatorAccess(OpLoc, Args[0], nullptr, Best->FoundDecl); |
| John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 12123 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12124 | ExprResult InputRes = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12125 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/nullptr, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12126 | Best->FoundDecl, Method); |
| 12127 | if (InputRes.isInvalid()) |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12128 | return ExprError(); |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12129 | Base = Input = InputRes.get(); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12130 | } else { |
| 12131 | // Convert the arguments. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12132 | ExprResult InputInit |
| Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 12133 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 12134 | Context, |
| Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 12135 | FnDecl->getParamDecl(0)), |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12136 | SourceLocation(), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12137 | Input); |
| Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 12138 | if (InputInit.isInvalid()) |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12139 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12140 | Input = InputInit.get(); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12141 | } |
| 12142 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12143 | // Build the actual expression node. |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 12144 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12145 | Base, HadMultipleCandidates, |
| 12146 | OpLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12147 | if (FnExpr.isInvalid()) |
| 12148 | return ExprError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12149 | |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 12150 | // Determine the result type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12151 | QualType ResultTy = FnDecl->getReturnType(); |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 12152 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 12153 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 12154 | |
| Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 12155 | Args[0] = Input; |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12156 | CallExpr *TheCall = |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12157 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.get(), ArgsArray, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12158 | ResultTy, VK, OpLoc, FPOptions()); |
| John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 12159 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12160 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, FnDecl)) |
| Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 12161 | return ExprError(); |
| 12162 | |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12163 | if (CheckFunctionCall(FnDecl, TheCall, |
| 12164 | FnDecl->getType()->castAs<FunctionProtoType>())) |
| 12165 | return ExprError(); |
| 12166 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12167 | return MaybeBindToTemporary(TheCall); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12168 | } else { |
| 12169 | // We matched a built-in operator. Convert the arguments, then |
| 12170 | // break out so that we will build the appropriate built-in |
| 12171 | // operator node. |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 12172 | ExprResult InputRes = PerformImplicitConversion( |
| 12173 | Input, Best->BuiltinParamTypes[0], Best->Conversions[0], AA_Passing); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12174 | if (InputRes.isInvalid()) |
| 12175 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12176 | Input = InputRes.get(); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12177 | break; |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12178 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12179 | } |
| 12180 | |
| 12181 | case OR_No_Viable_Function: |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 12182 | // This is an erroneous use of an operator which can be overloaded by |
| 12183 | // a non-member function. Check for non-member operators which were |
| 12184 | // defined too late to be candidates. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12185 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 12186 | // FIXME: Recover by calling the found function. |
| 12187 | return ExprError(); |
| 12188 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12189 | // No viable function; fall through to handling this as a |
| 12190 | // built-in operator, which will produce an error message for us. |
| 12191 | break; |
| 12192 | |
| 12193 | case OR_Ambiguous: |
| 12194 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 12195 | << UnaryOperator::getOpcodeStr(Opc) |
| 12196 | << Input->getType() |
| 12197 | << Input->getSourceRange(); |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12198 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12199 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 12200 | return ExprError(); |
| 12201 | |
| 12202 | case OR_Deleted: |
| 12203 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 12204 | << Best->Function->isDeleted() |
| 12205 | << UnaryOperator::getOpcodeStr(Opc) |
| 12206 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 12207 | << Input->getSourceRange(); |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12208 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
| Eli Friedman | 79b2d3a | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 12209 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12210 | return ExprError(); |
| 12211 | } |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12212 | |
| 12213 | // Either we found no viable overloaded operator or we matched a |
| 12214 | // built-in operator. In either case, fall through to trying to |
| 12215 | // build a built-in operation. |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12216 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12217 | } |
| 12218 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 12219 | /// Create a binary operation that may resolve to an overloaded |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12220 | /// operator. |
| 12221 | /// |
| 12222 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 12223 | /// |
| Craig Topper | a92ffb0 | 2015-12-10 08:51:49 +0000 | [diff] [blame] | 12224 | /// \param Opc The BinaryOperatorKind that describes this operator. |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12225 | /// |
| James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 12226 | /// \param Fns The set of non-member functions that will be |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12227 | /// considered by overload resolution. The caller needs to build this |
| 12228 | /// set based on the context using, e.g., |
| 12229 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 12230 | /// set should not contain any member functions; those will be added |
| 12231 | /// by CreateOverloadedBinOp(). |
| 12232 | /// |
| 12233 | /// \param LHS Left-hand argument. |
| 12234 | /// \param RHS Right-hand argument. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12235 | ExprResult |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12236 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
| Craig Topper | a92ffb0 | 2015-12-10 08:51:49 +0000 | [diff] [blame] | 12237 | BinaryOperatorKind Opc, |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12238 | const UnresolvedSetImpl &Fns, |
| Richard Smith | 91fc7d8 | 2017-10-05 19:35:51 +0000 | [diff] [blame] | 12239 | Expr *LHS, Expr *RHS, bool PerformADL) { |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12240 | Expr *Args[2] = { LHS, RHS }; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12241 | LHS=RHS=nullptr; // Please use only Args instead of LHS/RHS couple |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12242 | |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12243 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 12244 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 12245 | |
| 12246 | // If either side is type-dependent, create an appropriate dependent |
| 12247 | // expression. |
| Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 12248 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12249 | if (Fns.empty()) { |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12250 | // If there are no functions to store, just build a dependent |
| Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 12251 | // BinaryOperator or CompoundAssignment. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12252 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12253 | return new (Context) BinaryOperator( |
| 12254 | Args[0], Args[1], Opc, Context.DependentTy, VK_RValue, OK_Ordinary, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12255 | OpLoc, FPFeatures); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12256 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12257 | return new (Context) CompoundAssignOperator( |
| 12258 | Args[0], Args[1], Opc, Context.DependentTy, VK_LValue, OK_Ordinary, |
| 12259 | Context.DependentTy, Context.DependentTy, OpLoc, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12260 | FPFeatures); |
| Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 12261 | } |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12262 | |
| 12263 | // FIXME: save results of ADL from here? |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12264 | CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 12265 | // TODO: provide better source location info in DNLoc component. |
| 12266 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12267 | UnresolvedLookupExpr *Fn |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 12268 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 12269 | NestedNameSpecifierLoc(), OpNameInfo, |
| Richard Smith | 91fc7d8 | 2017-10-05 19:35:51 +0000 | [diff] [blame] | 12270 | /*ADL*/PerformADL, IsOverloaded(Fns), |
| Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 12271 | Fns.begin(), Fns.end()); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12272 | return new (Context) |
| 12273 | CXXOperatorCallExpr(Context, Op, Fn, Args, Context.DependentTy, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12274 | VK_RValue, OpLoc, FPFeatures); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12275 | } |
| 12276 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12277 | // Always do placeholder-like conversions on the RHS. |
| 12278 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 12279 | return ExprError(); |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 12280 | |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 12281 | // Do placeholder-like conversion on the LHS; note that we should |
| 12282 | // not get here with a PseudoObject LHS. |
| 12283 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12284 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 12285 | return ExprError(); |
| 12286 | |
| Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 12287 | // If this is the assignment operator, we only perform overload resolution |
| 12288 | // if the left-hand side is a class or enumeration type. This is actually |
| 12289 | // a hack. The standard requires that we do overload resolution between the |
| 12290 | // various built-in candidates, but as DR507 points out, this can lead to |
| 12291 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 12292 | // Note that we go the traditional code path for compound assignment forms. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12293 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
| Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 12294 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12295 | |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 12296 | // If this is the .* operator, which is not overloadable, just |
| 12297 | // create a built-in binary operator. |
| 12298 | if (Opc == BO_PtrMemD) |
| 12299 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 12300 | |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 12301 | // Build an empty overload set. |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 12302 | OverloadCandidateSet CandidateSet(OpLoc, OverloadCandidateSet::CSK_Operator); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12303 | |
| 12304 | // Add the candidates from the given function set. |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 12305 | AddFunctionCandidates(Fns, Args, CandidateSet); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12306 | |
| 12307 | // Add operator candidates that are member functions. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12308 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12309 | |
| Richard Smith | 0daabd7 | 2014-09-23 20:31:39 +0000 | [diff] [blame] | 12310 | // Add candidates from ADL. Per [over.match.oper]p2, this lookup is not |
| 12311 | // performed for an assignment operator (nor for operator[] nor operator->, |
| 12312 | // which don't get here). |
| Richard Smith | 91fc7d8 | 2017-10-05 19:35:51 +0000 | [diff] [blame] | 12313 | if (Opc != BO_Assign && PerformADL) |
| Richard Smith | 0daabd7 | 2014-09-23 20:31:39 +0000 | [diff] [blame] | 12314 | AddArgumentDependentLookupCandidates(OpName, OpLoc, Args, |
| 12315 | /*ExplicitTemplateArgs*/ nullptr, |
| 12316 | CandidateSet); |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 12317 | |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12318 | // Add builtin operator candidates. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12319 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12320 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12321 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 12322 | |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12323 | // Perform overload resolution. |
| 12324 | OverloadCandidateSet::iterator Best; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12325 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 12326 | case OR_Success: { |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12327 | // We found a built-in operator or an overloaded operator. |
| 12328 | FunctionDecl *FnDecl = Best->Function; |
| 12329 | |
| 12330 | if (FnDecl) { |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12331 | Expr *Base = nullptr; |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12332 | // We matched an overloaded operator. Build a call to that |
| 12333 | // operator. |
| 12334 | |
| 12335 | // Convert the arguments. |
| 12336 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 12337 | // Best->Access is only meaningful for class members. |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 12338 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
| John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 12339 | |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12340 | ExprResult Arg1 = |
| 12341 | PerformCopyInitialization( |
| 12342 | InitializedEntity::InitializeParameter(Context, |
| 12343 | FnDecl->getParamDecl(0)), |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12344 | SourceLocation(), Args[1]); |
| Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 12345 | if (Arg1.isInvalid()) |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12346 | return ExprError(); |
| Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 12347 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12348 | ExprResult Arg0 = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12349 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/nullptr, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12350 | Best->FoundDecl, Method); |
| 12351 | if (Arg0.isInvalid()) |
| Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 12352 | return ExprError(); |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12353 | Base = Args[0] = Arg0.getAs<Expr>(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12354 | Args[1] = RHS = Arg1.getAs<Expr>(); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12355 | } else { |
| 12356 | // Convert the arguments. |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12357 | ExprResult Arg0 = PerformCopyInitialization( |
| 12358 | InitializedEntity::InitializeParameter(Context, |
| 12359 | FnDecl->getParamDecl(0)), |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12360 | SourceLocation(), Args[0]); |
| Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 12361 | if (Arg0.isInvalid()) |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12362 | return ExprError(); |
| Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 12363 | |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12364 | ExprResult Arg1 = |
| 12365 | PerformCopyInitialization( |
| 12366 | InitializedEntity::InitializeParameter(Context, |
| 12367 | FnDecl->getParamDecl(1)), |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12368 | SourceLocation(), Args[1]); |
| Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 12369 | if (Arg1.isInvalid()) |
| 12370 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12371 | Args[0] = LHS = Arg0.getAs<Expr>(); |
| 12372 | Args[1] = RHS = Arg1.getAs<Expr>(); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12373 | } |
| 12374 | |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12375 | // Build the actual expression node. |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12376 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12377 | Best->FoundDecl, Base, |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12378 | HadMultipleCandidates, OpLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12379 | if (FnExpr.isInvalid()) |
| 12380 | return ExprError(); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12381 | |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 12382 | // Determine the result type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12383 | QualType ResultTy = FnDecl->getReturnType(); |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 12384 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 12385 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 12386 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12387 | CXXOperatorCallExpr *TheCall = |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12388 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.get(), |
| Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 12389 | Args, ResultTy, VK, OpLoc, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12390 | FPFeatures); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12391 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12392 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, |
| Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 12393 | FnDecl)) |
| 12394 | return ExprError(); |
| 12395 | |
| Nick Lewycky | d24d5f2 | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 12396 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12397 | const Expr *ImplicitThis = nullptr; |
| Nick Lewycky | d24d5f2 | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 12398 | // Cut off the implicit 'this'. |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12399 | if (isa<CXXMethodDecl>(FnDecl)) { |
| 12400 | ImplicitThis = ArgsArray[0]; |
| Nick Lewycky | d24d5f2 | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 12401 | ArgsArray = ArgsArray.slice(1); |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12402 | } |
| Richard Trieu | 36d0b2b | 2015-01-13 02:32:02 +0000 | [diff] [blame] | 12403 | |
| 12404 | // Check for a self move. |
| 12405 | if (Op == OO_Equal) |
| 12406 | DiagnoseSelfMove(Args[0], Args[1], OpLoc); |
| 12407 | |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12408 | checkCall(FnDecl, nullptr, ImplicitThis, ArgsArray, |
| 12409 | isa<CXXMethodDecl>(FnDecl), OpLoc, TheCall->getSourceRange(), |
| 12410 | VariadicDoesNotApply); |
| Nick Lewycky | d24d5f2 | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 12411 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12412 | return MaybeBindToTemporary(TheCall); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12413 | } else { |
| 12414 | // We matched a built-in operator. Convert the arguments, then |
| 12415 | // break out so that we will build the appropriate built-in |
| 12416 | // operator node. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12417 | ExprResult ArgsRes0 = |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 12418 | PerformImplicitConversion(Args[0], Best->BuiltinParamTypes[0], |
| 12419 | Best->Conversions[0], AA_Passing); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12420 | if (ArgsRes0.isInvalid()) |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12421 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12422 | Args[0] = ArgsRes0.get(); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12423 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12424 | ExprResult ArgsRes1 = |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 12425 | PerformImplicitConversion(Args[1], Best->BuiltinParamTypes[1], |
| 12426 | Best->Conversions[1], AA_Passing); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12427 | if (ArgsRes1.isInvalid()) |
| 12428 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12429 | Args[1] = ArgsRes1.get(); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12430 | break; |
| 12431 | } |
| 12432 | } |
| 12433 | |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12434 | case OR_No_Viable_Function: { |
| 12435 | // C++ [over.match.oper]p9: |
| 12436 | // If the operator is the operator , [...] and there are no |
| 12437 | // viable functions, then the operator is assumed to be the |
| 12438 | // built-in operator and interpreted according to clause 5. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12439 | if (Opc == BO_Comma) |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12440 | break; |
| 12441 | |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 12442 | // For class as left operand for assignment or compound assignment |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12443 | // operator do not fall through to handling in built-in, but report that |
| 12444 | // no overloaded assignment operator found |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12445 | ExprResult Result = ExprError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12446 | if (Args[0]->getType()->isRecordType() && |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12447 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
| Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 12448 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 12449 | << BinaryOperator::getOpcodeStr(Opc) |
| Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 12450 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| Eli Friedman | a31efa0 | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 12451 | if (Args[0]->getType()->isIncompleteType()) { |
| 12452 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 12453 | << Args[0]->getType() |
| 12454 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 12455 | } |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12456 | } else { |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 12457 | // This is an erroneous use of an operator which can be overloaded by |
| 12458 | // a non-member function. Check for non-member operators which were |
| 12459 | // defined too late to be candidates. |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12460 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
| Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 12461 | // FIXME: Recover by calling the found function. |
| 12462 | return ExprError(); |
| 12463 | |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12464 | // No viable function; try to create a built-in operation, which will |
| 12465 | // produce an error. Then, show the non-viable candidates. |
| 12466 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 12467 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12468 | assert(Result.isInvalid() && |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12469 | "C++ binary operator overloading is missing candidates!"); |
| 12470 | if (Result.isInvalid()) |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12471 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12472 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 12473 | return Result; |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12474 | } |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12475 | |
| 12476 | case OR_Ambiguous: |
| Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 12477 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12478 | << BinaryOperator::getOpcodeStr(Opc) |
| Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 12479 | << Args[0]->getType() << Args[1]->getType() |
| Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 12480 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12481 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12482 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12483 | return ExprError(); |
| 12484 | |
| 12485 | case OR_Deleted: |
| Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 12486 | if (isImplicitlyDeleted(Best->Function)) { |
| 12487 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 12488 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
| Richard Smith | de1a487 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 12489 | << Context.getRecordType(Method->getParent()) |
| 12490 | << getSpecialMember(Method); |
| Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 12491 | |
| Richard Smith | de1a487 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 12492 | // The user probably meant to call this special member. Just |
| 12493 | // explain why it's deleted. |
| 12494 | NoteDeletedFunction(Method); |
| 12495 | return ExprError(); |
| Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 12496 | } else { |
| 12497 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 12498 | << Best->Function->isDeleted() |
| 12499 | << BinaryOperator::getOpcodeStr(Opc) |
| 12500 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 12501 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 12502 | } |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12503 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
| Eli Friedman | 79b2d3a | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 12504 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12505 | return ExprError(); |
| John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 12506 | } |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12507 | |
| Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 12508 | // We matched a built-in operator; build it. |
| Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 12509 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 12510 | } |
| 12511 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12512 | ExprResult |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12513 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 12514 | SourceLocation RLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12515 | Expr *Base, Expr *Idx) { |
| 12516 | Expr *Args[2] = { Base, Idx }; |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12517 | DeclarationName OpName = |
| 12518 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 12519 | |
| 12520 | // If either side is type-dependent, create an appropriate dependent |
| 12521 | // expression. |
| 12522 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 12523 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12524 | CXXRecordDecl *NamingClass = nullptr; // lookup ignores member operators |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 12525 | // CHECKME: no 'operator' keyword? |
| 12526 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 12527 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12528 | UnresolvedLookupExpr *Fn |
| Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 12529 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 12530 | NestedNameSpecifierLoc(), OpNameInfo, |
| Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 12531 | /*ADL*/ true, /*Overloaded*/ false, |
| 12532 | UnresolvedSetIterator(), |
| 12533 | UnresolvedSetIterator()); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12534 | // Can't add any actual overloads yet |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12535 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12536 | return new (Context) |
| 12537 | CXXOperatorCallExpr(Context, OO_Subscript, Fn, Args, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12538 | Context.DependentTy, VK_RValue, RLoc, FPOptions()); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12539 | } |
| 12540 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12541 | // Handle placeholders on both operands. |
| 12542 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 12543 | return ExprError(); |
| 12544 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 12545 | return ExprError(); |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 12546 | |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12547 | // Build an empty overload set. |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 12548 | OverloadCandidateSet CandidateSet(LLoc, OverloadCandidateSet::CSK_Operator); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12549 | |
| 12550 | // Subscript can only be overloaded as a member function. |
| 12551 | |
| 12552 | // Add operator candidates that are member functions. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12553 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12554 | |
| 12555 | // Add builtin operator candidates. |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 12556 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12557 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12558 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 12559 | |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12560 | // Perform overload resolution. |
| 12561 | OverloadCandidateSet::iterator Best; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12562 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12563 | case OR_Success: { |
| 12564 | // We found a built-in operator or an overloaded operator. |
| 12565 | FunctionDecl *FnDecl = Best->Function; |
| 12566 | |
| 12567 | if (FnDecl) { |
| 12568 | // We matched an overloaded operator. Build a call to that |
| 12569 | // operator. |
| 12570 | |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 12571 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 12572 | |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12573 | // Convert the arguments. |
| 12574 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12575 | ExprResult Arg0 = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12576 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/nullptr, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12577 | Best->FoundDecl, Method); |
| 12578 | if (Arg0.isInvalid()) |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12579 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12580 | Args[0] = Arg0.get(); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12581 | |
| Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 12582 | // Convert the arguments. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12583 | ExprResult InputInit |
| Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 12584 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 12585 | Context, |
| Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 12586 | FnDecl->getParamDecl(0)), |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12587 | SourceLocation(), |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12588 | Args[1]); |
| Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 12589 | if (InputInit.isInvalid()) |
| 12590 | return ExprError(); |
| 12591 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12592 | Args[1] = InputInit.getAs<Expr>(); |
| Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 12593 | |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12594 | // Build the actual expression node. |
| Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 12595 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 12596 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12597 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 12598 | Best->FoundDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 12599 | Base, |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12600 | HadMultipleCandidates, |
| Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 12601 | OpLocInfo.getLoc(), |
| 12602 | OpLocInfo.getInfo()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12603 | if (FnExpr.isInvalid()) |
| 12604 | return ExprError(); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12605 | |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 12606 | // Determine the result type |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12607 | QualType ResultTy = FnDecl->getReturnType(); |
| Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 12608 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 12609 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 12610 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12611 | CXXOperatorCallExpr *TheCall = |
| 12612 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12613 | FnExpr.get(), Args, |
| Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 12614 | ResultTy, VK, RLoc, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 12615 | FPOptions()); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12616 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12617 | if (CheckCallReturnType(FnDecl->getReturnType(), LLoc, TheCall, FnDecl)) |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12618 | return ExprError(); |
| 12619 | |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12620 | if (CheckFunctionCall(Method, TheCall, |
| 12621 | Method->getType()->castAs<FunctionProtoType>())) |
| 12622 | return ExprError(); |
| 12623 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12624 | return MaybeBindToTemporary(TheCall); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12625 | } else { |
| 12626 | // We matched a built-in operator. Convert the arguments, then |
| 12627 | // break out so that we will build the appropriate built-in |
| 12628 | // operator node. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12629 | ExprResult ArgsRes0 = |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 12630 | PerformImplicitConversion(Args[0], Best->BuiltinParamTypes[0], |
| 12631 | Best->Conversions[0], AA_Passing); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12632 | if (ArgsRes0.isInvalid()) |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12633 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12634 | Args[0] = ArgsRes0.get(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12635 | |
| 12636 | ExprResult ArgsRes1 = |
| George Burgess IV | 5f6ab9a | 2017-06-08 20:55:21 +0000 | [diff] [blame] | 12637 | PerformImplicitConversion(Args[1], Best->BuiltinParamTypes[1], |
| 12638 | Best->Conversions[1], AA_Passing); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12639 | if (ArgsRes1.isInvalid()) |
| 12640 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12641 | Args[1] = ArgsRes1.get(); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12642 | |
| 12643 | break; |
| 12644 | } |
| 12645 | } |
| 12646 | |
| 12647 | case OR_No_Viable_Function: { |
| John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 12648 | if (CandidateSet.empty()) |
| 12649 | Diag(LLoc, diag::err_ovl_no_oper) |
| 12650 | << Args[0]->getType() << /*subscript*/ 0 |
| 12651 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 12652 | else |
| 12653 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 12654 | << Args[0]->getType() |
| 12655 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12656 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12657 | "[]", LLoc); |
| John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 12658 | return ExprError(); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12659 | } |
| 12660 | |
| 12661 | case OR_Ambiguous: |
| Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 12662 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12663 | << "[]" |
| Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 12664 | << Args[0]->getType() << Args[1]->getType() |
| 12665 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12666 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12667 | "[]", LLoc); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12668 | return ExprError(); |
| 12669 | |
| 12670 | case OR_Deleted: |
| 12671 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 12672 | << Best->Function->isDeleted() << "[]" |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 12673 | << getDeletedOrUnavailableSuffix(Best->Function) |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12674 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12675 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12676 | "[]", LLoc); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12677 | return ExprError(); |
| 12678 | } |
| 12679 | |
| 12680 | // We matched a built-in operator; build it. |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12681 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 12682 | } |
| 12683 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12684 | /// BuildCallToMemberFunction - Build a call to a member |
| 12685 | /// function. MemExpr is the expression that refers to the member |
| 12686 | /// function (and includes the object parameter), Args/NumArgs are the |
| 12687 | /// arguments to the function call (not including the object |
| 12688 | /// parameter). The caller needs to validate that the member |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12689 | /// expression refers to a non-static member function or an overloaded |
| 12690 | /// member function. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12691 | ExprResult |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12692 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12693 | SourceLocation LParenLoc, |
| 12694 | MultiExprArg Args, |
| 12695 | SourceLocation RParenLoc) { |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12696 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 12697 | MemExprE->getType() == Context.OverloadTy); |
| 12698 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12699 | // Dig out the member expression. This holds both the object |
| 12700 | // argument and the member function we're referring to. |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12701 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12702 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12703 | // Determine whether this is a call to a pointer-to-member function. |
| 12704 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 12705 | assert(op->getType() == Context.BoundMemberTy); |
| 12706 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 12707 | |
| 12708 | QualType fnType = |
| 12709 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 12710 | |
| 12711 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 12712 | QualType resultType = proto->getCallResultType(Context); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12713 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getReturnType()); |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12714 | |
| 12715 | // Check that the object type isn't more qualified than the |
| 12716 | // member function we're calling. |
| 12717 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 12718 | |
| 12719 | QualType objectType = op->getLHS()->getType(); |
| 12720 | if (op->getOpcode() == BO_PtrMemI) |
| 12721 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 12722 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 12723 | |
| 12724 | Qualifiers difference = objectQuals - funcQuals; |
| 12725 | difference.removeObjCGCAttr(); |
| 12726 | difference.removeAddressSpace(); |
| 12727 | if (difference) { |
| 12728 | std::string qualsString = difference.getAsString(); |
| 12729 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 12730 | << fnType.getUnqualifiedType() |
| 12731 | << qualsString |
| 12732 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 12733 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 12734 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12735 | CXXMemberCallExpr *call |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12736 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12737 | resultType, valueKind, RParenLoc); |
| 12738 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12739 | if (CheckCallReturnType(proto->getReturnType(), op->getRHS()->getLocStart(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12740 | call, nullptr)) |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12741 | return ExprError(); |
| 12742 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12743 | if (ConvertArgumentsForCall(call, op, nullptr, proto, Args, RParenLoc)) |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12744 | return ExprError(); |
| 12745 | |
| Richard Trieu | 9be9c68 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 12746 | if (CheckOtherCall(call, proto)) |
| 12747 | return ExprError(); |
| 12748 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 12749 | return MaybeBindToTemporary(call); |
| 12750 | } |
| 12751 | |
| David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 12752 | if (isa<CXXPseudoDestructorExpr>(NakedMemExpr)) |
| 12753 | return new (Context) |
| 12754 | CallExpr(Context, MemExprE, Args, Context.VoidTy, VK_RValue, RParenLoc); |
| 12755 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12756 | UnbridgedCastsSet UnbridgedCasts; |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12757 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12758 | return ExprError(); |
| 12759 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12760 | MemberExpr *MemExpr; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12761 | CXXMethodDecl *Method = nullptr; |
| 12762 | DeclAccessPair FoundDecl = DeclAccessPair::make(nullptr, AS_public); |
| 12763 | NestedNameSpecifier *Qualifier = nullptr; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12764 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 12765 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12766 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12767 | FoundDecl = MemExpr->getFoundDecl(); |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 12768 | Qualifier = MemExpr->getQualifier(); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12769 | UnbridgedCasts.restore(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12770 | } else { |
| 12771 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 12772 | Qualifier = UnresExpr->getQualifier(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12773 | |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 12774 | QualType ObjectType = UnresExpr->getBaseType(); |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 12775 | Expr::Classification ObjectClassification |
| 12776 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 12777 | : UnresExpr->getBase()->Classify(Context); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12778 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12779 | // Add overload candidates |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 12780 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc(), |
| 12781 | OverloadCandidateSet::CSK_Normal); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12782 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12783 | // FIXME: avoid copy. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 12784 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12785 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 12786 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 12787 | TemplateArgs = &TemplateArgsBuffer; |
| 12788 | } |
| 12789 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12790 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 12791 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 12792 | |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 12793 | NamedDecl *Func = *I; |
| 12794 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 12795 | if (isa<UsingShadowDecl>(Func)) |
| 12796 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 12797 | |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 12798 | |
| Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 12799 | // Microsoft supports direct constructor calls. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12800 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 12801 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12802 | Args, CandidateSet); |
| Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 12803 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
| Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 12804 | // If explicit template arguments were provided, we can't call a |
| 12805 | // non-template member function. |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12806 | if (TemplateArgs) |
| Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 12807 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12808 | |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 12809 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12810 | ObjectClassification, Args, CandidateSet, |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 12811 | /*SuppressUserConversions=*/false); |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 12812 | } else { |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 12813 | AddMethodTemplateCandidate( |
| 12814 | cast<FunctionTemplateDecl>(Func), I.getPair(), ActingDC, |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 12815 | TemplateArgs, ObjectType, ObjectClassification, Args, CandidateSet, |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 12816 | /*SuppressUsedConversions=*/false); |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 12817 | } |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 12818 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12819 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12820 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 12821 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12822 | UnbridgedCasts.restore(); |
| 12823 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12824 | OverloadCandidateSet::iterator Best; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 12825 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
| Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 12826 | Best)) { |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12827 | case OR_Success: |
| 12828 | Method = cast<CXXMethodDecl>(Best->Function); |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12829 | FoundDecl = Best->FoundDecl; |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 12830 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
| Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 12831 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 12832 | return ExprError(); |
| Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 12833 | // If FoundDecl is different from Method (such as if one is a template |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 12834 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 12835 | // called on both. |
| 12836 | // FIXME: This would be more comprehensively addressed by modifying |
| 12837 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 12838 | // being used. |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 12839 | if (Method != FoundDecl.getDecl() && |
| Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 12840 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 12841 | return ExprError(); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12842 | break; |
| 12843 | |
| 12844 | case OR_No_Viable_Function: |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12845 | Diag(UnresExpr->getMemberLoc(), |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12846 | diag::err_ovl_no_viable_member_function_in_call) |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 12847 | << DeclName << MemExprE->getSourceRange(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12848 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12849 | // FIXME: Leaking incoming expressions! |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12850 | return ExprError(); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12851 | |
| 12852 | case OR_Ambiguous: |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12853 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 12854 | << DeclName << MemExprE->getSourceRange(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12855 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12856 | // FIXME: Leaking incoming expressions! |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12857 | return ExprError(); |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 12858 | |
| 12859 | case OR_Deleted: |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12860 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 12861 | << Best->Function->isDeleted() |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 12862 | << DeclName |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 12863 | << getDeletedOrUnavailableSuffix(Best->Function) |
| Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 12864 | << MemExprE->getSourceRange(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12865 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 12866 | // FIXME: Leaking incoming expressions! |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12867 | return ExprError(); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12868 | } |
| 12869 | |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12870 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12871 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12872 | // If overload resolution picked a static member, build a |
| 12873 | // non-member call based on that function. |
| 12874 | if (Method->isStatic()) { |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12875 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 12876 | RParenLoc); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12877 | } |
| 12878 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12879 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12880 | } |
| 12881 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12882 | QualType ResultType = Method->getReturnType(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12883 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 12884 | ResultType = ResultType.getNonLValueExprType(Context); |
| 12885 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12886 | assert(Method && "Member call to something that isn't a method?"); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12887 | CXXMemberCallExpr *TheCall = |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12888 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12889 | ResultType, VK, RParenLoc); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12890 | |
| Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 12891 | // Check for a valid return type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12892 | if (CheckCallReturnType(Method->getReturnType(), MemExpr->getMemberLoc(), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12893 | TheCall, Method)) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12894 | return ExprError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12895 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12896 | // Convert the object argument (for a non-static member function call). |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12897 | // We only need to do this if there was actually an overload; otherwise |
| 12898 | // it was done at lookup. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12899 | if (!Method->isStatic()) { |
| 12900 | ExprResult ObjectArg = |
| 12901 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 12902 | FoundDecl, Method); |
| 12903 | if (ObjectArg.isInvalid()) |
| 12904 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 12905 | MemExpr->setBase(ObjectArg.get()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12906 | } |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12907 | |
| 12908 | // Convert the rest of the arguments |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 12909 | const FunctionProtoType *Proto = |
| 12910 | Method->getType()->getAs<FunctionProtoType>(); |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12911 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12912 | RParenLoc)) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12913 | return ExprError(); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12914 | |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12915 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
| Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 12916 | |
| Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 12917 | if (CheckFunctionCall(Method, TheCall, Proto)) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12918 | return ExprError(); |
| Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 12919 | |
| George Burgess IV | aea6ade | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 12920 | // In the case the method to call was not selected by the overloading |
| 12921 | // resolution process, we still need to handle the enable_if attribute. Do |
| George Burgess IV | 0d54653 | 2016-11-10 21:47:12 +0000 | [diff] [blame] | 12922 | // that here, so it will not hide previous -- and more relevant -- errors. |
| George Burgess IV | add6ab5 | 2016-11-16 21:31:25 +0000 | [diff] [blame] | 12923 | if (auto *MemE = dyn_cast<MemberExpr>(NakedMemExpr)) { |
| George Burgess IV | aea6ade | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 12924 | if (const EnableIfAttr *Attr = CheckEnableIf(Method, Args, true)) { |
| George Burgess IV | add6ab5 | 2016-11-16 21:31:25 +0000 | [diff] [blame] | 12925 | Diag(MemE->getMemberLoc(), |
| George Burgess IV | aea6ade | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 12926 | diag::err_ovl_no_viable_member_function_in_call) |
| 12927 | << Method << Method->getSourceRange(); |
| 12928 | Diag(Method->getLocation(), |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 12929 | diag::note_ovl_candidate_disabled_by_function_cond_attr) |
| George Burgess IV | aea6ade | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 12930 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); |
| 12931 | return ExprError(); |
| 12932 | } |
| 12933 | } |
| 12934 | |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 12935 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 12936 | isa<CXXDestructorDecl>(CurContext)) && |
| Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 12937 | TheCall->getMethodDecl()->isPure()) { |
| 12938 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 12939 | |
| Davide Italiano | ccb3738 | 2015-07-14 23:36:10 +0000 | [diff] [blame] | 12940 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts()) && |
| 12941 | MemExpr->performsVirtualDispatch(getLangOpts())) { |
| 12942 | Diag(MemExpr->getLocStart(), |
| Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 12943 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 12944 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 12945 | << MD->getParent()->getDeclName(); |
| 12946 | |
| 12947 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
| Davide Italiano | ccb3738 | 2015-07-14 23:36:10 +0000 | [diff] [blame] | 12948 | if (getLangOpts().AppleKext) |
| 12949 | Diag(MemExpr->getLocStart(), |
| 12950 | diag::note_pure_qualified_call_kext) |
| 12951 | << MD->getParent()->getDeclName() |
| 12952 | << MD->getDeclName(); |
| Chandler Carruth | 5925926 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 12953 | } |
| Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 12954 | } |
| Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 12955 | |
| 12956 | if (CXXDestructorDecl *DD = |
| 12957 | dyn_cast<CXXDestructorDecl>(TheCall->getMethodDecl())) { |
| 12958 | // a->A::f() doesn't go through the vtable, except in AppleKext mode. |
| Justin Lebar | d35f706 | 2016-07-12 23:23:01 +0000 | [diff] [blame] | 12959 | bool CallCanBeVirtual = !MemExpr->hasQualifier() || getLangOpts().AppleKext; |
| Nico Weber | 5a9259c | 2016-01-15 21:45:31 +0000 | [diff] [blame] | 12960 | CheckVirtualDtorCall(DD, MemExpr->getLocStart(), /*IsDelete=*/false, |
| 12961 | CallCanBeVirtual, /*WarnOnNonAbstractTypes=*/true, |
| 12962 | MemExpr->getMemberLoc()); |
| 12963 | } |
| 12964 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12965 | return MaybeBindToTemporary(TheCall); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 12966 | } |
| 12967 | |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12968 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 12969 | /// type (C++ [over.call.object]), which can end up invoking an |
| 12970 | /// overloaded function call operator (@c operator()) or performing a |
| 12971 | /// user-defined conversion on the object argument. |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 12972 | ExprResult |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12973 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
| Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 12974 | SourceLocation LParenLoc, |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 12975 | MultiExprArg Args, |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12976 | SourceLocation RParenLoc) { |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12977 | if (checkPlaceholderForOverload(*this, Obj)) |
| 12978 | return ExprError(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 12979 | ExprResult Object = Obj; |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12980 | |
| 12981 | UnbridgedCastsSet UnbridgedCasts; |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 12982 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 12983 | return ExprError(); |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 12984 | |
| Nico Weber | b58e51c | 2014-11-19 05:21:39 +0000 | [diff] [blame] | 12985 | assert(Object.get()->getType()->isRecordType() && |
| 12986 | "Requires object type argument"); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 12987 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12988 | |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12989 | // C++ [over.call.object]p1: |
| 12990 | // If the primary-expression E in the function call syntax |
| Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 12991 | // evaluates to a class object of type "cv T", then the set of |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 12992 | // candidate functions includes at least the function call |
| 12993 | // operators of T. The function call operators of T are obtained by |
| 12994 | // ordinary lookup of the name operator() in the context of |
| 12995 | // (E).operator(). |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 12996 | OverloadCandidateSet CandidateSet(LParenLoc, |
| 12997 | OverloadCandidateSet::CSK_Operator); |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 12998 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
| Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 12999 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13000 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 13001 | diag::err_incomplete_object_call, Object.get())) |
| Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 13002 | return true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13003 | |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 13004 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 13005 | LookupQualifiedName(R, Record->getDecl()); |
| 13006 | R.suppressDiagnostics(); |
| 13007 | |
| Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 13008 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 13009 | Oper != OperEnd; ++Oper) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13010 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 13011 | Object.get()->Classify(Context), Args, CandidateSet, |
| 13012 | /*SuppressUserConversions=*/false); |
| Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 13013 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13014 | |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13015 | // C++ [over.call.object]p2: |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 13016 | // In addition, for each (non-explicit in C++0x) conversion function |
| Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 13017 | // declared in T of the form |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13018 | // |
| 13019 | // operator conversion-type-id () cv-qualifier; |
| 13020 | // |
| 13021 | // where cv-qualifier is the same cv-qualification as, or a |
| 13022 | // greater cv-qualification than, cv, and where conversion-type-id |
| Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 13023 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 13024 | // R", or the type "reference to pointer to function of |
| 13025 | // (P1,...,Pn) returning R", or the type "reference to function |
| 13026 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13027 | // is also considered as a candidate function. Similarly, |
| 13028 | // surrogate call functions are added to the set of candidate |
| 13029 | // functions for each conversion function declared in an |
| 13030 | // accessible base class provided the function is not hidden |
| 13031 | // within T by another intervening declaration. |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 13032 | const auto &Conversions = |
| 13033 | cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
| 13034 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 13035 | NamedDecl *D = *I; |
| 13036 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 13037 | if (isa<UsingShadowDecl>(D)) |
| 13038 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13039 | |
| Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 13040 | // Skip over templated conversion functions; they aren't |
| 13041 | // surrogates. |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 13042 | if (isa<FunctionTemplateDecl>(D)) |
| Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 13043 | continue; |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 13044 | |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 13045 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 13046 | if (!Conv->isExplicit()) { |
| 13047 | // Strip the reference type (if any) and then the pointer type (if |
| 13048 | // any) to get down to what might be a function type. |
| 13049 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 13050 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 13051 | ConvType = ConvPtrType->getPointeeType(); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13052 | |
| Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 13053 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 13054 | { |
| 13055 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13056 | Object.get(), Args, CandidateSet); |
| Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 13057 | } |
| 13058 | } |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13059 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13060 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13061 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 13062 | |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13063 | // Perform overload resolution. |
| 13064 | OverloadCandidateSet::iterator Best; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13065 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 13066 | Best)) { |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13067 | case OR_Success: |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13068 | // Overload resolution succeeded; we'll build the appropriate call |
| 13069 | // below. |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13070 | break; |
| 13071 | |
| 13072 | case OR_No_Viable_Function: |
| John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 13073 | if (CandidateSet.empty()) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 13074 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13075 | << Object.get()->getType() << /*call*/ 1 |
| 13076 | << Object.get()->getSourceRange(); |
| John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 13077 | else |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 13078 | Diag(Object.get()->getLocStart(), |
| John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 13079 | diag::err_ovl_no_viable_object_call) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13080 | << Object.get()->getType() << Object.get()->getSourceRange(); |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13081 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13082 | break; |
| 13083 | |
| 13084 | case OR_Ambiguous: |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 13085 | Diag(Object.get()->getLocStart(), |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13086 | diag::err_ovl_ambiguous_object_call) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13087 | << Object.get()->getType() << Object.get()->getSourceRange(); |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13088 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13089 | break; |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 13090 | |
| 13091 | case OR_Deleted: |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 13092 | Diag(Object.get()->getLocStart(), |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 13093 | diag::err_ovl_deleted_object_call) |
| 13094 | << Best->Function->isDeleted() |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 13095 | << Object.get()->getType() |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 13096 | << getDeletedOrUnavailableSuffix(Best->Function) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13097 | << Object.get()->getSourceRange(); |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13098 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 13099 | break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13100 | } |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13101 | |
| Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 13102 | if (Best == CandidateSet.end()) |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13103 | return true; |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13104 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 13105 | UnbridgedCasts.restore(); |
| 13106 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13107 | if (Best->Function == nullptr) { |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13108 | // Since there is no function declaration, this is one of the |
| 13109 | // surrogate candidates. Dig out the conversion function. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13110 | CXXConversionDecl *Conv |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13111 | = cast<CXXConversionDecl>( |
| 13112 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 13113 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13114 | CheckMemberOperatorAccess(LParenLoc, Object.get(), nullptr, |
| 13115 | Best->FoundDecl); |
| Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 13116 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 13117 | return ExprError(); |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 13118 | assert(Conv == Best->FoundDecl.getDecl() && |
| Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 13119 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13120 | // We selected one of the surrogate functions that converts the |
| 13121 | // object parameter to a function pointer. Perform the conversion |
| 13122 | // on the object argument, then let ActOnCallExpr finish the job. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13123 | |
| Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 13124 | // Create an implicit member expr to refer to the conversion operator. |
| Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 13125 | // and then call it. |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13126 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 13127 | Conv, HadMultipleCandidates); |
| Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 13128 | if (Call.isInvalid()) |
| 13129 | return ExprError(); |
| Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 13130 | // Record usage of conversion in an implicit cast. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 13131 | Call = ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 13132 | CK_UserDefinedConversion, Call.get(), |
| 13133 | nullptr, VK_RValue); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13134 | |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13135 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13136 | } |
| 13137 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13138 | CheckMemberOperatorAccess(LParenLoc, Object.get(), nullptr, Best->FoundDecl); |
| John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 13139 | |
| Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 13140 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 13141 | // that calls this method, using Object for the implicit object |
| 13142 | // parameter and passing along the remaining arguments. |
| 13143 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| Nico Weber | 1fefe41 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 13144 | |
| 13145 | // An error diagnostic has already been printed when parsing the declaration. |
| Nico Weber | 9512d3f | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 13146 | if (Method->isInvalidDecl()) |
| Nico Weber | 1fefe41 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 13147 | return ExprError(); |
| 13148 | |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 13149 | const FunctionProtoType *Proto = |
| 13150 | Method->getType()->getAs<FunctionProtoType>(); |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13151 | |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 13152 | unsigned NumParams = Proto->getNumParams(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13153 | |
| Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 13154 | DeclarationNameInfo OpLocInfo( |
| 13155 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 13156 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 13157 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 13158 | Obj, HadMultipleCandidates, |
| Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 13159 | OpLocInfo.getLoc(), |
| 13160 | OpLocInfo.getInfo()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13161 | if (NewFn.isInvalid()) |
| 13162 | return true; |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13163 | |
| Benjamin Kramer | 8b1a6bd | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 13164 | // Build the full argument list for the method call (the implicit object |
| 13165 | // parameter is placed at the beginning of the list). |
| George Burgess IV | 215f6e7 | 2016-12-13 19:22:56 +0000 | [diff] [blame] | 13166 | SmallVector<Expr *, 8> MethodArgs(Args.size() + 1); |
| Benjamin Kramer | 8b1a6bd | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 13167 | MethodArgs[0] = Object.get(); |
| George Burgess IV | 215f6e7 | 2016-12-13 19:22:56 +0000 | [diff] [blame] | 13168 | std::copy(Args.begin(), Args.end(), MethodArgs.begin() + 1); |
| Benjamin Kramer | 8b1a6bd | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 13169 | |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13170 | // Once we've built TheCall, all of the expressions are properly |
| 13171 | // owned. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 13172 | QualType ResultTy = Method->getReturnType(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 13173 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 13174 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 13175 | |
| Benjamin Kramer | 8b1a6bd | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 13176 | CXXOperatorCallExpr *TheCall = new (Context) |
| George Burgess IV | 215f6e7 | 2016-12-13 19:22:56 +0000 | [diff] [blame] | 13177 | CXXOperatorCallExpr(Context, OO_Call, NewFn.get(), MethodArgs, ResultTy, |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 13178 | VK, RParenLoc, FPOptions()); |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13179 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 13180 | if (CheckCallReturnType(Method->getReturnType(), LParenLoc, TheCall, Method)) |
| Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 13181 | return true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13182 | |
| Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 13183 | // We may have default arguments. If so, we need to allocate more |
| 13184 | // slots in the call for them. |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 13185 | if (Args.size() < NumParams) |
| 13186 | TheCall->setNumArgs(Context, NumParams + 1); |
| Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 13187 | |
| Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 13188 | bool IsError = false; |
| 13189 | |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13190 | // Initialize the implicit object parameter. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13191 | ExprResult ObjRes = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13192 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/nullptr, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13193 | Best->FoundDecl, Method); |
| 13194 | if (ObjRes.isInvalid()) |
| 13195 | IsError = true; |
| 13196 | else |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 13197 | Object = ObjRes; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13198 | TheCall->setArg(0, Object.get()); |
| Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 13199 | |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13200 | // Check the argument types. |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 13201 | for (unsigned i = 0; i != NumParams; i++) { |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13202 | Expr *Arg; |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13203 | if (i < Args.size()) { |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13204 | Arg = Args[i]; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13205 | |
| Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 13206 | // Pass the argument. |
| Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 13207 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 13208 | ExprResult InputInit |
| Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 13209 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 13210 | Context, |
| Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 13211 | Method->getParamDecl(i)), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 13212 | SourceLocation(), Arg); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13213 | |
| Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 13214 | IsError |= InputInit.isInvalid(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13215 | Arg = InputInit.getAs<Expr>(); |
| Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 13216 | } else { |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 13217 | ExprResult DefArg |
| Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 13218 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 13219 | if (DefArg.isInvalid()) { |
| 13220 | IsError = true; |
| 13221 | break; |
| 13222 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13223 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13224 | Arg = DefArg.getAs<Expr>(); |
| Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 13225 | } |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13226 | |
| 13227 | TheCall->setArg(i + 1, Arg); |
| 13228 | } |
| 13229 | |
| 13230 | // If this is a variadic call, handle args passed through "...". |
| 13231 | if (Proto->isVariadic()) { |
| 13232 | // Promote the arguments (C99 6.5.2.2p7). |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 13233 | for (unsigned i = NumParams, e = Args.size(); i < e; i++) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13234 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
| 13235 | nullptr); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13236 | IsError |= Arg.isInvalid(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13237 | TheCall->setArg(i + 1, Arg.get()); |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13238 | } |
| 13239 | } |
| 13240 | |
| Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 13241 | if (IsError) return true; |
| 13242 | |
| Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 13243 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
| Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 13244 | |
| Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 13245 | if (CheckFunctionCall(Method, TheCall, Proto)) |
| Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 13246 | return true; |
| 13247 | |
| John McCall | e172be5 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 13248 | return MaybeBindToTemporary(TheCall); |
| Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 13249 | } |
| 13250 | |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13251 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13252 | /// (if one exists), where @c Base is an expression of class type and |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13253 | /// @c Member is the name of the member we're trying to find. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 13254 | ExprResult |
| Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 13255 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 13256 | bool *NoArrowOperatorFound) { |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 13257 | assert(Base->getType()->isRecordType() && |
| 13258 | "left-hand side must have class type"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 13259 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 13260 | if (checkPlaceholderForOverload(*this, Base)) |
| 13261 | return ExprError(); |
| John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 13262 | |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 13263 | SourceLocation Loc = Base->getExprLoc(); |
| 13264 | |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13265 | // C++ [over.ref]p1: |
| 13266 | // |
| 13267 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 13268 | // for a class object x of type T if T::operator->() exists and if |
| 13269 | // the operator is selected as the best match function by the |
| 13270 | // overload resolution mechanism (13.3). |
| Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 13271 | DeclarationName OpName = |
| 13272 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 13273 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Operator); |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 13274 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 13275 | |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 13276 | if (RequireCompleteType(Loc, Base->getType(), |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 13277 | diag::err_typecheck_incomplete_tag, Base)) |
| Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 13278 | return ExprError(); |
| 13279 | |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 13280 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 13281 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 13282 | R.suppressDiagnostics(); |
| Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 13283 | |
| 13284 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 13285 | Oper != OperEnd; ++Oper) { |
| Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 13286 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 13287 | None, CandidateSet, /*SuppressUserConversions=*/false); |
| John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 13288 | } |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13289 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13290 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 13291 | |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13292 | // Perform overload resolution. |
| 13293 | OverloadCandidateSet::iterator Best; |
| John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 13294 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13295 | case OR_Success: |
| 13296 | // Overload resolution succeeded; we'll build the call below. |
| 13297 | break; |
| 13298 | |
| 13299 | case OR_No_Viable_Function: |
| Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 13300 | if (CandidateSet.empty()) { |
| 13301 | QualType BaseType = Base->getType(); |
| Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 13302 | if (NoArrowOperatorFound) { |
| 13303 | // Report this specific error to the caller instead of emitting a |
| 13304 | // diagnostic, as requested. |
| 13305 | *NoArrowOperatorFound = true; |
| 13306 | return ExprError(); |
| 13307 | } |
| Kaelyn Uhrain | bad7fb0 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 13308 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 13309 | << BaseType << Base->getSourceRange(); |
| Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 13310 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
| Kaelyn Uhrain | bad7fb0 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 13311 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
| Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 13312 | << FixItHint::CreateReplacement(OpLoc, "."); |
| Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 13313 | } |
| 13314 | } else |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13315 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 13316 | << "operator->" << Base->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 13317 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 13318 | return ExprError(); |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13319 | |
| 13320 | case OR_Ambiguous: |
| Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 13321 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 13322 | << "->" << Base->getType() << Base->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 13323 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 13324 | return ExprError(); |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 13325 | |
| 13326 | case OR_Deleted: |
| 13327 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 13328 | << Best->Function->isDeleted() |
| Simon Pilgrim | fb9662a | 2017-06-01 18:17:18 +0000 | [diff] [blame] | 13329 | << "->" |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 13330 | << getDeletedOrUnavailableSuffix(Best->Function) |
| Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 13331 | << Base->getSourceRange(); |
| Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 13332 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 13333 | return ExprError(); |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13334 | } |
| 13335 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13336 | CheckMemberOperatorAccess(OpLoc, Base, nullptr, Best->FoundDecl); |
| John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 13337 | |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13338 | // Convert the object parameter. |
| 13339 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13340 | ExprResult BaseResult = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13341 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/nullptr, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13342 | Best->FoundDecl, Method); |
| 13343 | if (BaseResult.isInvalid()) |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 13344 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13345 | Base = BaseResult.get(); |
| Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 13346 | |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13347 | // Build the operator call. |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 13348 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 13349 | Base, HadMultipleCandidates, OpLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 13350 | if (FnExpr.isInvalid()) |
| 13351 | return ExprError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13352 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 13353 | QualType ResultTy = Method->getReturnType(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 13354 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 13355 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 13356 | CXXOperatorCallExpr *TheCall = |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13357 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.get(), |
| Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 13358 | Base, ResultTy, VK, OpLoc, FPOptions()); |
| Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 13359 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 13360 | if (CheckCallReturnType(Method->getReturnType(), OpLoc, TheCall, Method)) |
| George Burgess IV | ce6284b | 2017-01-28 02:19:40 +0000 | [diff] [blame] | 13361 | return ExprError(); |
| 13362 | |
| 13363 | if (CheckFunctionCall(Method, TheCall, |
| 13364 | Method->getType()->castAs<FunctionProtoType>())) |
| 13365 | return ExprError(); |
| Eli Friedman | 2d9c47e | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 13366 | |
| 13367 | return MaybeBindToTemporary(TheCall); |
| Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 13368 | } |
| 13369 | |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 13370 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 13371 | /// a literal operator described by the provided lookup results. |
| 13372 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 13373 | DeclarationNameInfo &SuffixInfo, |
| 13374 | ArrayRef<Expr*> Args, |
| 13375 | SourceLocation LitEndLoc, |
| 13376 | TemplateArgumentListInfo *TemplateArgs) { |
| 13377 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13378 | |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 13379 | OverloadCandidateSet CandidateSet(UDSuffixLoc, |
| 13380 | OverloadCandidateSet::CSK_Normal); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 13381 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, TemplateArgs, |
| 13382 | /*SuppressUserConversions=*/true); |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13383 | |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 13384 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 13385 | |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 13386 | // Perform overload resolution. This will usually be trivial, but might need |
| 13387 | // to perform substitutions for a literal operator template. |
| 13388 | OverloadCandidateSet::iterator Best; |
| 13389 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 13390 | case OR_Success: |
| 13391 | case OR_Deleted: |
| 13392 | break; |
| 13393 | |
| 13394 | case OR_No_Viable_Function: |
| 13395 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 13396 | << R.getLookupName(); |
| 13397 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 13398 | return ExprError(); |
| 13399 | |
| 13400 | case OR_Ambiguous: |
| 13401 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 13402 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 13403 | return ExprError(); |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13404 | } |
| 13405 | |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 13406 | FunctionDecl *FD = Best->Function; |
| Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 13407 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 13408 | nullptr, HadMultipleCandidates, |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 13409 | SuffixInfo.getLoc(), |
| 13410 | SuffixInfo.getInfo()); |
| 13411 | if (Fn.isInvalid()) |
| 13412 | return true; |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13413 | |
| 13414 | // Check the argument types. This should almost always be a no-op, except |
| 13415 | // that array-to-pointer decay is applied to string literals. |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13416 | Expr *ConvArgs[2]; |
| Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 13417 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13418 | ExprResult InputInit = PerformCopyInitialization( |
| 13419 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 13420 | SourceLocation(), Args[ArgIdx]); |
| 13421 | if (InputInit.isInvalid()) |
| 13422 | return true; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13423 | ConvArgs[ArgIdx] = InputInit.get(); |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13424 | } |
| 13425 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 13426 | QualType ResultTy = FD->getReturnType(); |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13427 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 13428 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 13429 | |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13430 | UserDefinedLiteral *UDL = |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 13431 | new (Context) UserDefinedLiteral(Context, Fn.get(), |
| Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 13432 | llvm::makeArrayRef(ConvArgs, Args.size()), |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13433 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 13434 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 13435 | if (CheckCallReturnType(FD->getReturnType(), UDSuffixLoc, UDL, FD)) |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13436 | return ExprError(); |
| 13437 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13438 | if (CheckFunctionCall(FD, UDL, nullptr)) |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13439 | return ExprError(); |
| 13440 | |
| 13441 | return MaybeBindToTemporary(UDL); |
| 13442 | } |
| 13443 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13444 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 13445 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 13446 | /// will be invoked. Otherwise, the function will be found via argument |
| 13447 | /// dependent lookup. |
| 13448 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 13449 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 13450 | /// is returned. |
| 13451 | Sema::ForRangeStatus |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 13452 | Sema::BuildForRangeBeginEndCall(SourceLocation Loc, |
| 13453 | SourceLocation RangeLoc, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13454 | const DeclarationNameInfo &NameInfo, |
| 13455 | LookupResult &MemberLookup, |
| 13456 | OverloadCandidateSet *CandidateSet, |
| 13457 | Expr *Range, ExprResult *CallExpr) { |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 13458 | Scope *S = nullptr; |
| 13459 | |
| Richard Smith | 67ef14f | 2017-09-26 18:37:55 +0000 | [diff] [blame] | 13460 | CandidateSet->clear(OverloadCandidateSet::CSK_Normal); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13461 | if (!MemberLookup.empty()) { |
| 13462 | ExprResult MemberRef = |
| 13463 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 13464 | /*IsPtr=*/false, CXXScopeSpec(), |
| 13465 | /*TemplateKWLoc=*/SourceLocation(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13466 | /*FirstQualifierInScope=*/nullptr, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13467 | MemberLookup, |
| Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 13468 | /*TemplateArgs=*/nullptr, S); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13469 | if (MemberRef.isInvalid()) { |
| 13470 | *CallExpr = ExprError(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13471 | return FRS_DiagnosticIssued; |
| 13472 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13473 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, nullptr); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13474 | if (CallExpr->isInvalid()) { |
| 13475 | *CallExpr = ExprError(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13476 | return FRS_DiagnosticIssued; |
| 13477 | } |
| 13478 | } else { |
| 13479 | UnresolvedSet<0> FoundNames; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13480 | UnresolvedLookupExpr *Fn = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13481 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/nullptr, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13482 | NestedNameSpecifierLoc(), NameInfo, |
| 13483 | /*NeedsADL=*/true, /*Overloaded=*/false, |
| Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 13484 | FoundNames.begin(), FoundNames.end()); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13485 | |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 13486 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13487 | CandidateSet, CallExpr); |
| 13488 | if (CandidateSet->empty() || CandidateSetError) { |
| 13489 | *CallExpr = ExprError(); |
| 13490 | return FRS_NoViableFunction; |
| 13491 | } |
| 13492 | OverloadCandidateSet::iterator Best; |
| 13493 | OverloadingResult OverloadResult = |
| 13494 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 13495 | |
| 13496 | if (OverloadResult == OR_No_Viable_Function) { |
| 13497 | *CallExpr = ExprError(); |
| 13498 | return FRS_NoViableFunction; |
| 13499 | } |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 13500 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13501 | Loc, nullptr, CandidateSet, &Best, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13502 | OverloadResult, |
| 13503 | /*AllowTypoCorrection=*/false); |
| 13504 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 13505 | *CallExpr = ExprError(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 13506 | return FRS_DiagnosticIssued; |
| 13507 | } |
| 13508 | } |
| 13509 | return FRS_Success; |
| 13510 | } |
| 13511 | |
| 13512 | |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 13513 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 13514 | /// a C++ overloaded function (possibly with some parentheses and |
| 13515 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 13516 | /// to the function declaration Fn, so patch up the expression E to |
| Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 13517 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 13518 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 13519 | FunctionDecl *Fn) { |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 13520 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 13521 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 13522 | Found, Fn); |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13523 | if (SubExpr == PE->getSubExpr()) |
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 13524 | return PE; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13525 | |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13526 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13527 | } |
| 13528 | |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13529 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 13530 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 13531 | Found, Fn); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13532 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13533 | SubExpr->getType()) && |
| Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 13534 | "Implicit cast type cannot be determined from overload"); |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 13535 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13536 | if (SubExpr == ICE->getSubExpr()) |
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 13537 | return ICE; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13538 | |
| 13539 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 13540 | ICE->getCastKind(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13541 | SubExpr, nullptr, |
| John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 13542 | ICE->getValueKind()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13543 | } |
| 13544 | |
| Aaron Ballman | ff7bd8b | 2016-09-02 13:45:40 +0000 | [diff] [blame] | 13545 | if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) { |
| 13546 | if (!GSE->isResultDependent()) { |
| 13547 | Expr *SubExpr = |
| 13548 | FixOverloadedFunctionReference(GSE->getResultExpr(), Found, Fn); |
| 13549 | if (SubExpr == GSE->getResultExpr()) |
| 13550 | return GSE; |
| 13551 | |
| 13552 | // Replace the resulting type information before rebuilding the generic |
| 13553 | // selection expression. |
| Aaron Ballman | 8b871d9 | 2016-09-02 18:31:31 +0000 | [diff] [blame] | 13554 | ArrayRef<Expr *> A = GSE->getAssocExprs(); |
| 13555 | SmallVector<Expr *, 4> AssocExprs(A.begin(), A.end()); |
| Aaron Ballman | ff7bd8b | 2016-09-02 13:45:40 +0000 | [diff] [blame] | 13556 | unsigned ResultIdx = GSE->getResultIndex(); |
| 13557 | AssocExprs[ResultIdx] = SubExpr; |
| 13558 | |
| 13559 | return new (Context) GenericSelectionExpr( |
| 13560 | Context, GSE->getGenericLoc(), GSE->getControllingExpr(), |
| 13561 | GSE->getAssocTypeSourceInfos(), AssocExprs, GSE->getDefaultLoc(), |
| 13562 | GSE->getRParenLoc(), GSE->containsUnexpandedParameterPack(), |
| 13563 | ResultIdx); |
| 13564 | } |
| 13565 | // Rather than fall through to the unreachable, return the original generic |
| 13566 | // selection expression. |
| 13567 | return GSE; |
| 13568 | } |
| 13569 | |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13570 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 13571 | assert(UnOp->getOpcode() == UO_AddrOf && |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 13572 | "Can only take the address of an overloaded function"); |
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 13573 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 13574 | if (Method->isStatic()) { |
| 13575 | // Do nothing: static member functions aren't any different |
| 13576 | // from non-member functions. |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13577 | } else { |
| Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 13578 | // Fix the subexpression, which really has to be an |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 13579 | // UnresolvedLookupExpr holding an overloaded member function |
| 13580 | // or template. |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 13581 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 13582 | Found, Fn); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13583 | if (SubExpr == UnOp->getSubExpr()) |
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 13584 | return UnOp; |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13585 | |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13586 | assert(isa<DeclRefExpr>(SubExpr) |
| 13587 | && "fixed to something other than a decl ref"); |
| 13588 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 13589 | && "fixed to a member ref with no nested name qualifier"); |
| 13590 | |
| 13591 | // We have taken the address of a pointer to member |
| 13592 | // function. Perform the computation here so that we get the |
| 13593 | // appropriate pointer to member type. |
| 13594 | QualType ClassType |
| 13595 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 13596 | QualType MemPtrType |
| 13597 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| David Majnemer | 02d57cc | 2016-06-30 03:02:03 +0000 | [diff] [blame] | 13598 | // Under the MS ABI, lock down the inheritance model now. |
| 13599 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 13600 | (void)isCompleteType(UnOp->getOperatorLoc(), MemPtrType); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13601 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 13602 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 13603 | VK_RValue, OK_Ordinary, |
| Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 13604 | UnOp->getOperatorLoc(), false); |
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 13605 | } |
| 13606 | } |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 13607 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 13608 | Found, Fn); |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13609 | if (SubExpr == UnOp->getSubExpr()) |
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 13610 | return UnOp; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13611 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 13612 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13613 | Context.getPointerType(SubExpr->getType()), |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 13614 | VK_RValue, OK_Ordinary, |
| Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 13615 | UnOp->getOperatorLoc(), false); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13616 | } |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13617 | |
| Richard Smith | 84a0b6d | 2016-10-18 23:39:12 +0000 | [diff] [blame] | 13618 | // C++ [except.spec]p17: |
| 13619 | // An exception-specification is considered to be needed when: |
| 13620 | // - in an expression the function is the unique lookup result or the |
| 13621 | // selected member of a set of overloaded functions |
| 13622 | if (auto *FPT = Fn->getType()->getAs<FunctionProtoType>()) |
| 13623 | ResolveExceptionSpec(E->getExprLoc(), FPT); |
| 13624 | |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13625 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13626 | // FIXME: avoid copy. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13627 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 13628 | if (ULE->hasExplicitTemplateArgs()) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13629 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 13630 | TemplateArgs = &TemplateArgsBuffer; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 13631 | } |
| 13632 | |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13633 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 13634 | ULE->getQualifierLoc(), |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 13635 | ULE->getTemplateKeywordLoc(), |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13636 | Fn, |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 13637 | /*enclosing*/ false, // FIXME? |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13638 | ULE->getNameLoc(), |
| 13639 | Fn->getType(), |
| 13640 | VK_LValue, |
| 13641 | Found.getDecl(), |
| 13642 | TemplateArgs); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 13643 | MarkDeclRefReferenced(DRE); |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13644 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 13645 | return DRE; |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 13646 | } |
| 13647 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 13648 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 13649 | // FIXME: avoid copy. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 13650 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13651 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 13652 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 13653 | TemplateArgs = &TemplateArgsBuffer; |
| 13654 | } |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 13655 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13656 | Expr *Base; |
| 13657 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 13658 | // If we're filling in a static method where we used to have an |
| 13659 | // implicit member access, rewrite to a simple decl ref. |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13660 | if (MemExpr->isImplicitAccess()) { |
| 13661 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13662 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 13663 | MemExpr->getQualifierLoc(), |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 13664 | MemExpr->getTemplateKeywordLoc(), |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13665 | Fn, |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 13666 | /*enclosing*/ false, |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13667 | MemExpr->getMemberLoc(), |
| 13668 | Fn->getType(), |
| 13669 | VK_LValue, |
| 13670 | Found.getDecl(), |
| 13671 | TemplateArgs); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 13672 | MarkDeclRefReferenced(DRE); |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 13673 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 13674 | return DRE; |
| Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 13675 | } else { |
| 13676 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 13677 | if (MemExpr->getQualifier()) |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 13678 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
| Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 13679 | CheckCXXThisCapture(Loc); |
| Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 13680 | Base = new (Context) CXXThisExpr(Loc, |
| 13681 | MemExpr->getBaseType(), |
| 13682 | /*isImplicit=*/true); |
| 13683 | } |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13684 | } else |
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 13685 | Base = MemExpr->getBase(); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 13686 | |
| John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 13687 | ExprValueKind valueKind; |
| 13688 | QualType type; |
| 13689 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 13690 | valueKind = VK_LValue; |
| 13691 | type = Fn->getType(); |
| 13692 | } else { |
| 13693 | valueKind = VK_RValue; |
| Yunzhong Gao | eba323a | 2015-05-01 02:04:32 +0000 | [diff] [blame] | 13694 | type = Context.BoundMemberTy; |
| 13695 | } |
| 13696 | |
| 13697 | MemberExpr *ME = MemberExpr::Create( |
| 13698 | Context, Base, MemExpr->isArrow(), MemExpr->getOperatorLoc(), |
| 13699 | MemExpr->getQualifierLoc(), MemExpr->getTemplateKeywordLoc(), Fn, Found, |
| 13700 | MemExpr->getMemberNameInfo(), TemplateArgs, type, valueKind, |
| 13701 | OK_Ordinary); |
| 13702 | ME->setHadMultipleCandidates(true); |
| 13703 | MarkMemberReferenced(ME); |
| 13704 | return ME; |
| Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 13705 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13706 | |
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 13707 | llvm_unreachable("Invalid reference to overloaded function"); |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 13708 | } |
| 13709 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 13710 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 13711 | DeclAccessPair Found, |
| 13712 | FunctionDecl *Fn) { |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 13713 | return FixOverloadedFunctionReference(E.get(), Found, Fn); |
| Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 13714 | } |