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" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
| 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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | |
| 37 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 39 | |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 40 | /// A convenience routine for creating a decayed reference to a function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 41 | static ExprResult |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 42 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| 43 | bool HadMultipleCandidates, |
Douglas Gregor | e9d6293 | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 44 | SourceLocation Loc = SourceLocation(), |
| 45 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 46 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 47 | return ExprError(); |
| 48 | // If FoundDecl is different from Fn (such as if one is a template |
| 49 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 50 | // called on both. |
| 51 | // FIXME: This would be more comprehensively addressed by modifying |
| 52 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 53 | // being used. |
| 54 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 55 | return ExprError(); |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 56 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 57 | VK_LValue, Loc, LocInfo); |
| 58 | if (HadMultipleCandidates) |
| 59 | DRE->setHadMultipleCandidates(true); |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 60 | |
| 61 | S.MarkDeclRefReferenced(DRE); |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 62 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 63 | ExprResult E = S.Owned(DRE); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 64 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 65 | if (E.isInvalid()) |
| 66 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 67 | return E; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 70 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 71 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 72 | StandardConversionSequence &SCS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 73 | bool CStyle, |
| 74 | bool AllowObjCWritebackConversion); |
Sam Panzer | 04390a6 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 75 | |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 76 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 77 | QualType &ToType, |
| 78 | bool InOverloadResolution, |
| 79 | StandardConversionSequence &SCS, |
| 80 | bool CStyle); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 81 | static OverloadingResult |
| 82 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 83 | UserDefinedConversionSequence& User, |
| 84 | OverloadCandidateSet& Conversions, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 85 | bool AllowExplicit, |
| 86 | bool AllowObjCConversionOnExplicit); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | static ImplicitConversionSequence::CompareKind |
| 90 | CompareStandardConversionSequences(Sema &S, |
| 91 | const StandardConversionSequence& SCS1, |
| 92 | const StandardConversionSequence& SCS2); |
| 93 | |
| 94 | static ImplicitConversionSequence::CompareKind |
| 95 | CompareQualificationConversions(Sema &S, |
| 96 | const StandardConversionSequence& SCS1, |
| 97 | const StandardConversionSequence& SCS2); |
| 98 | |
| 99 | static ImplicitConversionSequence::CompareKind |
| 100 | CompareDerivedToBaseConversions(Sema &S, |
| 101 | const StandardConversionSequence& SCS1, |
| 102 | const StandardConversionSequence& SCS2); |
| 103 | |
| 104 | |
| 105 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | /// GetConversionCategory - Retrieve the implicit conversion |
| 107 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 109 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 110 | static const ImplicitConversionCategory |
| 111 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 112 | ICC_Identity, |
| 113 | ICC_Lvalue_Transformation, |
| 114 | ICC_Lvalue_Transformation, |
| 115 | ICC_Lvalue_Transformation, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 116 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 117 | ICC_Qualification_Adjustment, |
| 118 | ICC_Promotion, |
| 119 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 120 | ICC_Promotion, |
| 121 | ICC_Conversion, |
| 122 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 123 | ICC_Conversion, |
| 124 | ICC_Conversion, |
| 125 | ICC_Conversion, |
| 126 | ICC_Conversion, |
| 127 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 128 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 129 | ICC_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 130 | ICC_Conversion, |
| 131 | ICC_Conversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 132 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 133 | ICC_Conversion |
| 134 | }; |
| 135 | return Category[(int)Kind]; |
| 136 | } |
| 137 | |
| 138 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 139 | /// corresponding to the given implicit conversion kind. |
| 140 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 141 | static const ImplicitConversionRank |
| 142 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 143 | ICR_Exact_Match, |
| 144 | ICR_Exact_Match, |
| 145 | ICR_Exact_Match, |
| 146 | ICR_Exact_Match, |
| 147 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 148 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 149 | ICR_Promotion, |
| 150 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 151 | ICR_Promotion, |
| 152 | ICR_Conversion, |
| 153 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 154 | ICR_Conversion, |
| 155 | ICR_Conversion, |
| 156 | ICR_Conversion, |
| 157 | ICR_Conversion, |
| 158 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 159 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 160 | ICR_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 161 | ICR_Conversion, |
| 162 | ICR_Conversion, |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 163 | ICR_Complex_Real_Conversion, |
| 164 | ICR_Conversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 165 | ICR_Conversion, |
| 166 | ICR_Writeback_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 167 | }; |
| 168 | return Rank[(int)Kind]; |
| 169 | } |
| 170 | |
| 171 | /// GetImplicitConversionName - Return the name of this kind of |
| 172 | /// implicit conversion. |
| 173 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 174 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 175 | "No conversion", |
| 176 | "Lvalue-to-rvalue", |
| 177 | "Array-to-pointer", |
| 178 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 179 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 180 | "Qualification", |
| 181 | "Integral promotion", |
| 182 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 183 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 184 | "Integral conversion", |
| 185 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 186 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 187 | "Floating-integral conversion", |
| 188 | "Pointer conversion", |
| 189 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 190 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 191 | "Compatible-types conversion", |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 192 | "Derived-to-base conversion", |
| 193 | "Vector conversion", |
| 194 | "Vector splat", |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 195 | "Complex-real conversion", |
| 196 | "Block Pointer conversion", |
| 197 | "Transparent Union Conversion" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | "Writeback conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 199 | }; |
| 200 | return Name[Kind]; |
| 201 | } |
| 202 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 203 | /// StandardConversionSequence - Set the standard conversion |
| 204 | /// sequence to the identity conversion. |
| 205 | void StandardConversionSequence::setAsIdentityConversion() { |
| 206 | First = ICK_Identity; |
| 207 | Second = ICK_Identity; |
| 208 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 209 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 210 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 211 | ReferenceBinding = false; |
| 212 | DirectBinding = false; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 213 | IsLvalueReference = true; |
| 214 | BindsToFunctionLvalue = false; |
| 215 | BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 216 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 217 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 218 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 222 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 223 | /// implicit conversions. |
| 224 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 225 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 226 | if (GetConversionRank(First) > Rank) |
| 227 | Rank = GetConversionRank(First); |
| 228 | if (GetConversionRank(Second) > Rank) |
| 229 | Rank = GetConversionRank(Second); |
| 230 | if (GetConversionRank(Third) > Rank) |
| 231 | Rank = GetConversionRank(Third); |
| 232 | return Rank; |
| 233 | } |
| 234 | |
| 235 | /// isPointerConversionToBool - Determines whether this conversion is |
| 236 | /// 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] | 237 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 238 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 240 | // Note that FromType has not necessarily been transformed by the |
| 241 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 242 | // check for their presence as well as checking whether FromType is |
| 243 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 244 | if (getToType(1)->isBooleanType() && |
John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 245 | (getFromType()->isPointerType() || |
| 246 | getFromType()->isObjCObjectPointerType() || |
| 247 | getFromType()->isBlockPointerType() || |
Anders Carlsson | 7da7cc5 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 248 | getFromType()->isNullPtrType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 249 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 250 | return true; |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 255 | /// isPointerConversionToVoidPointer - Determines whether this |
| 256 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 257 | /// used as part of the ranking of standard conversion sequences (C++ |
| 258 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 260 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 262 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 263 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 264 | |
| 265 | // Note that FromType has not necessarily been transformed by the |
| 266 | // array-to-pointer implicit conversion, so check for its presence |
| 267 | // and redo the conversion to get a pointer. |
| 268 | if (First == ICK_Array_To_Pointer) |
| 269 | FromType = Context.getArrayDecayedType(FromType); |
| 270 | |
Douglas Gregor | 5d3d3fa | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 271 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 272 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 273 | return ToPtrType->getPointeeType()->isVoidType(); |
| 274 | |
| 275 | return false; |
| 276 | } |
| 277 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 278 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 279 | /// or after one in an implicit conversion. |
| 280 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 281 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 282 | switch (ICE->getCastKind()) { |
| 283 | case CK_NoOp: |
| 284 | case CK_IntegralCast: |
| 285 | case CK_IntegralToBoolean: |
| 286 | case CK_IntegralToFloating: |
| 287 | case CK_FloatingToIntegral: |
| 288 | case CK_FloatingToBoolean: |
| 289 | case CK_FloatingCast: |
| 290 | Converted = ICE->getSubExpr(); |
| 291 | continue; |
| 292 | |
| 293 | default: |
| 294 | return Converted; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return Converted; |
| 299 | } |
| 300 | |
| 301 | /// Check if this standard conversion sequence represents a narrowing |
| 302 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 303 | /// |
| 304 | /// \param Ctx The AST context. |
| 305 | /// \param Converted The result of applying this standard conversion sequence. |
| 306 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 307 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 308 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 309 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 310 | NarrowingKind |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 311 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 312 | const Expr *Converted, |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 313 | APValue &ConstantValue, |
| 314 | QualType &ConstantType) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 315 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 316 | |
| 317 | // C++11 [dcl.init.list]p7: |
| 318 | // A narrowing conversion is an implicit conversion ... |
| 319 | QualType FromType = getToType(0); |
| 320 | QualType ToType = getToType(1); |
| 321 | switch (Second) { |
| 322 | // -- from a floating-point type to an integer type, or |
| 323 | // |
| 324 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 325 | // type, except where the source is a constant expression and the actual |
| 326 | // value after conversion will fit into the target type and will produce |
| 327 | // the original value when converted back to the original type, or |
| 328 | case ICK_Floating_Integral: |
| 329 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 330 | return NK_Type_Narrowing; |
| 331 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 332 | llvm::APSInt IntConstantValue; |
| 333 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 334 | if (Initializer && |
| 335 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 336 | // Convert the integer to the floating type. |
| 337 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 338 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 339 | llvm::APFloat::rmNearestTiesToEven); |
| 340 | // And back. |
| 341 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 342 | bool ignored; |
| 343 | Result.convertToInteger(ConvertedValue, |
| 344 | llvm::APFloat::rmTowardZero, &ignored); |
| 345 | // If the resulting value is different, this was a narrowing conversion. |
| 346 | if (IntConstantValue != ConvertedValue) { |
| 347 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 348 | ConstantType = Initializer->getType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 349 | return NK_Constant_Narrowing; |
| 350 | } |
| 351 | } else { |
| 352 | // Variables are always narrowings. |
| 353 | return NK_Variable_Narrowing; |
| 354 | } |
| 355 | } |
| 356 | return NK_Not_Narrowing; |
| 357 | |
| 358 | // -- from long double to double or float, or from double to float, except |
| 359 | // where the source is a constant expression and the actual value after |
| 360 | // conversion is within the range of values that can be represented (even |
| 361 | // if it cannot be represented exactly), or |
| 362 | case ICK_Floating_Conversion: |
| 363 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 364 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 365 | // FromType is larger than ToType. |
| 366 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 367 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 368 | // Constant! |
| 369 | assert(ConstantValue.isFloat()); |
| 370 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 371 | // Convert the source value into the target type. |
| 372 | bool ignored; |
| 373 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 374 | Ctx.getFloatTypeSemantics(ToType), |
| 375 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 376 | // If there was no overflow, the source value is within the range of |
| 377 | // values that can be represented. |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 378 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 379 | ConstantType = Initializer->getType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 380 | return NK_Constant_Narrowing; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 381 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 382 | } else { |
| 383 | return NK_Variable_Narrowing; |
| 384 | } |
| 385 | } |
| 386 | return NK_Not_Narrowing; |
| 387 | |
| 388 | // -- from an integer type or unscoped enumeration type to an integer type |
| 389 | // that cannot represent all the values of the original type, except where |
| 390 | // the source is a constant expression and the actual value after |
| 391 | // conversion will fit into the target type and will produce the original |
| 392 | // value when converted back to the original type. |
| 393 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 394 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 395 | // Boolean conversions can be from pointers and pointers to members |
| 396 | // [conv.bool], and those aren't considered narrowing conversions. |
| 397 | return NK_Not_Narrowing; |
| 398 | } // Otherwise, fall through to the integral case. |
| 399 | case ICK_Integral_Conversion: { |
| 400 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 401 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 402 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 403 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 404 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 405 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 406 | |
| 407 | if (FromWidth > ToWidth || |
Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 408 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 409 | (FromSigned && !ToSigned)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 410 | // Not all values of FromType can be represented in ToType. |
| 411 | llvm::APSInt InitializerValue; |
| 412 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 413 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 414 | // Such conversions on variables are always narrowing. |
| 415 | return NK_Variable_Narrowing; |
Richard Smith | 72cd8ea | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 416 | } |
| 417 | bool Narrowing = false; |
| 418 | if (FromWidth < ToWidth) { |
Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 419 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 420 | // narrowing. |
| 421 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 72cd8ea | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 422 | Narrowing = true; |
Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 423 | } else { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 424 | // Add a bit to the InitializerValue so we don't have to worry about |
| 425 | // signed vs. unsigned comparisons. |
| 426 | InitializerValue = InitializerValue.extend( |
| 427 | InitializerValue.getBitWidth() + 1); |
| 428 | // Convert the initializer to and from the target width and signed-ness. |
| 429 | llvm::APSInt ConvertedValue = InitializerValue; |
| 430 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 431 | ConvertedValue.setIsSigned(ToSigned); |
| 432 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 433 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 434 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 72cd8ea | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 435 | if (ConvertedValue != InitializerValue) |
| 436 | Narrowing = true; |
| 437 | } |
| 438 | if (Narrowing) { |
| 439 | ConstantType = Initializer->getType(); |
| 440 | ConstantValue = APValue(InitializerValue); |
| 441 | return NK_Constant_Narrowing; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | return NK_Not_Narrowing; |
| 445 | } |
| 446 | |
| 447 | default: |
| 448 | // Other kinds of conversions are not narrowings. |
| 449 | return NK_Not_Narrowing; |
| 450 | } |
| 451 | } |
| 452 | |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 453 | /// dump - Print this standard conversion sequence to standard |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 454 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 455 | void StandardConversionSequence::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 456 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | bool PrintedSomething = false; |
| 458 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 459 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 460 | PrintedSomething = true; |
| 461 | } |
| 462 | |
| 463 | if (Second != ICK_Identity) { |
| 464 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 465 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 466 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 467 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 468 | |
| 469 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 470 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 471 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 472 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 473 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 474 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 475 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 476 | PrintedSomething = true; |
| 477 | } |
| 478 | |
| 479 | if (Third != ICK_Identity) { |
| 480 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 481 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 483 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 484 | PrintedSomething = true; |
| 485 | } |
| 486 | |
| 487 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 488 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 492 | /// dump - Print this user-defined conversion sequence to standard |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 493 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 494 | void UserDefinedConversionSequence::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 495 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 496 | if (Before.First || Before.Second || Before.Third) { |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 497 | Before.dump(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 498 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 499 | } |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 500 | if (ConversionFunction) |
| 501 | OS << '\'' << *ConversionFunction << '\''; |
| 502 | else |
| 503 | OS << "aggregate initialization"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 505 | OS << " -> "; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 506 | After.dump(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 510 | /// dump - Print this implicit conversion sequence to standard |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 511 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 512 | void ImplicitConversionSequence::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 513 | raw_ostream &OS = llvm::errs(); |
Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 514 | if (isStdInitializerListElement()) |
| 515 | OS << "Worst std::initializer_list element conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | switch (ConversionKind) { |
| 517 | case StandardConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 518 | OS << "Standard conversion: "; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 519 | Standard.dump(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 520 | break; |
| 521 | case UserDefinedConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 522 | OS << "User-defined conversion: "; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 523 | UserDefined.dump(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | break; |
| 525 | case EllipsisConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 526 | OS << "Ellipsis conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 528 | case AmbiguousConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 529 | OS << "Ambiguous conversion"; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 530 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 531 | case BadConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 532 | OS << "Bad conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 536 | OS << "\n"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 537 | } |
| 538 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 539 | void AmbiguousConversionSequence::construct() { |
| 540 | new (&conversions()) ConversionSet(); |
| 541 | } |
| 542 | |
| 543 | void AmbiguousConversionSequence::destruct() { |
| 544 | conversions().~ConversionSet(); |
| 545 | } |
| 546 | |
| 547 | void |
| 548 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 549 | FromTypePtr = O.FromTypePtr; |
| 550 | ToTypePtr = O.ToTypePtr; |
| 551 | new (&conversions()) ConversionSet(O.conversions()); |
| 552 | } |
| 553 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 554 | namespace { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 555 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 556 | // template argument information. |
| 557 | struct DFIArguments { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 558 | TemplateArgument FirstArg; |
| 559 | TemplateArgument SecondArg; |
| 560 | }; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 561 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 562 | // template parameter and template argument information. |
| 563 | struct DFIParamWithArguments : DFIArguments { |
| 564 | TemplateParameter Param; |
| 565 | }; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 566 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 568 | /// \brief Convert from Sema's representation of template deduction information |
| 569 | /// to the form used in overload-candidate information. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 570 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 571 | Sema::TemplateDeductionResult TDK, |
| 572 | TemplateDeductionInfo &Info) { |
| 573 | DeductionFailureInfo Result; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 574 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 575 | Result.HasDiagnostic = false; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 576 | Result.Data = 0; |
| 577 | switch (TDK) { |
| 578 | case Sema::TDK_Success: |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 579 | case Sema::TDK_Invalid: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 580 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 581 | case Sema::TDK_TooManyArguments: |
| 582 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 583 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 585 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 586 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 587 | Result.Data = Info.Param.getOpaqueValue(); |
| 588 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 589 | |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 590 | case Sema::TDK_NonDeducedMismatch: { |
| 591 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 592 | DFIArguments *Saved = new (Context) DFIArguments; |
| 593 | Saved->FirstArg = Info.FirstArg; |
| 594 | Saved->SecondArg = Info.SecondArg; |
| 595 | Result.Data = Saved; |
| 596 | break; |
| 597 | } |
| 598 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 599 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 600 | case Sema::TDK_Underqualified: { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 601 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 602 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 603 | Saved->Param = Info.Param; |
| 604 | Saved->FirstArg = Info.FirstArg; |
| 605 | Saved->SecondArg = Info.SecondArg; |
| 606 | Result.Data = Saved; |
| 607 | break; |
| 608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 610 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 611 | Result.Data = Info.take(); |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 612 | if (Info.hasSFINAEDiagnostic()) { |
| 613 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 614 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 615 | Info.takeSFINAEDiagnostic(*Diag); |
| 616 | Result.HasDiagnostic = true; |
| 617 | } |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 618 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 620 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 621 | Result.Data = Info.Expression; |
| 622 | break; |
| 623 | |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 624 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 625 | break; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 626 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 628 | return Result; |
| 629 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 630 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 631 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 632 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 633 | case Sema::TDK_Success: |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 634 | case Sema::TDK_Invalid: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 635 | case Sema::TDK_InstantiationDepth: |
| 636 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 637 | case Sema::TDK_TooManyArguments: |
| 638 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 639 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 640 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 641 | break; |
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_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 644 | case Sema::TDK_Underqualified: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 645 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 646 | // FIXME: Destroy the data? |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 647 | Data = 0; |
| 648 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 649 | |
| 650 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 651 | // FIXME: Destroy the template argument list? |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 652 | Data = 0; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 653 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 654 | Diag->~PartialDiagnosticAt(); |
| 655 | HasDiagnostic = false; |
| 656 | } |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 657 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 659 | // Unhandled |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 660 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 664 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 665 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 666 | if (HasDiagnostic) |
| 667 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 668 | return 0; |
| 669 | } |
| 670 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 671 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 672 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 673 | case Sema::TDK_Success: |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 674 | case Sema::TDK_Invalid: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 675 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 676 | case Sema::TDK_TooManyArguments: |
| 677 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 678 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 679 | case Sema::TDK_NonDeducedMismatch: |
| 680 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 681 | return TemplateParameter(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 683 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 684 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 685 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 686 | |
| 687 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 688 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 689 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 691 | // Unhandled |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 692 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 696 | return TemplateParameter(); |
| 697 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 698 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 699 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 700 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 701 | case Sema::TDK_Success: |
| 702 | case Sema::TDK_Invalid: |
| 703 | case Sema::TDK_InstantiationDepth: |
| 704 | case Sema::TDK_TooManyArguments: |
| 705 | case Sema::TDK_TooFewArguments: |
| 706 | case Sema::TDK_Incomplete: |
| 707 | case Sema::TDK_InvalidExplicitArguments: |
| 708 | case Sema::TDK_Inconsistent: |
| 709 | case Sema::TDK_Underqualified: |
| 710 | case Sema::TDK_NonDeducedMismatch: |
| 711 | case Sema::TDK_FailedOverloadResolution: |
| 712 | return 0; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 713 | |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 714 | case Sema::TDK_SubstitutionFailure: |
| 715 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 717 | // Unhandled |
| 718 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 719 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 725 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 726 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 727 | case Sema::TDK_Success: |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 728 | case Sema::TDK_Invalid: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 729 | case Sema::TDK_InstantiationDepth: |
| 730 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 731 | case Sema::TDK_TooManyArguments: |
| 732 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 733 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 734 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 735 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 736 | return 0; |
| 737 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 738 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 739 | case Sema::TDK_Underqualified: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 740 | case Sema::TDK_NonDeducedMismatch: |
| 741 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 743 | // Unhandled |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 744 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 748 | return 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 750 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 751 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 752 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 753 | case Sema::TDK_Success: |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 754 | case Sema::TDK_Invalid: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 755 | case Sema::TDK_InstantiationDepth: |
| 756 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 757 | case Sema::TDK_TooManyArguments: |
| 758 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 759 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 760 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 761 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 762 | return 0; |
| 763 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 764 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 765 | case Sema::TDK_Underqualified: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 766 | case Sema::TDK_NonDeducedMismatch: |
| 767 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 768 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 769 | // Unhandled |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 770 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 777 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 778 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 779 | Sema::TDK_FailedOverloadResolution) |
| 780 | return static_cast<Expr*>(Data); |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
Benjamin Kramer | 97e5949 | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 785 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | 0bf93aa | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 786 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 02b0843 | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 787 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 788 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | 0bf93aa | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 789 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 790 | i->DeductionFailure.Destroy(); |
| 791 | } |
Benjamin Kramer | 97e5949 | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | void OverloadCandidateSet::clear() { |
| 795 | destroyCandidates(); |
Benjamin Kramer | 0b9c509 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 796 | NumInlineSequences = 0; |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 797 | Candidates.clear(); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 798 | Functions.clear(); |
| 799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 801 | namespace { |
| 802 | class UnbridgedCastsSet { |
| 803 | struct Entry { |
| 804 | Expr **Addr; |
| 805 | Expr *Saved; |
| 806 | }; |
| 807 | SmallVector<Entry, 2> Entries; |
| 808 | |
| 809 | public: |
| 810 | void save(Sema &S, Expr *&E) { |
| 811 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 812 | Entry entry = { &E, E }; |
| 813 | Entries.push_back(entry); |
| 814 | E = S.stripARCUnbridgedCast(E); |
| 815 | } |
| 816 | |
| 817 | void restore() { |
| 818 | for (SmallVectorImpl<Entry>::iterator |
| 819 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 820 | *i->Addr = i->Saved; |
| 821 | } |
| 822 | }; |
| 823 | } |
| 824 | |
| 825 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 826 | /// preprocessing on the given expression. |
| 827 | /// |
| 828 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 829 | /// without this, they will be immediately diagnosed as errors |
| 830 | /// |
| 831 | /// Return true on unrecoverable error. |
| 832 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 833 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 834 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 835 | // We can't handle overloaded expressions here because overload |
| 836 | // resolution might reasonably tweak them. |
| 837 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 838 | |
| 839 | // If the context potentially accepts unbridged ARC casts, strip |
| 840 | // the unbridged cast and add it to the collection for later restoration. |
| 841 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 842 | unbridgedCasts) { |
| 843 | unbridgedCasts->save(S, E); |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | // Go ahead and check everything else. |
| 848 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 849 | if (result.isInvalid()) |
| 850 | return true; |
| 851 | |
| 852 | E = result.take(); |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | // Nothing to do. |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 861 | /// placeholders. |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 862 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 863 | MultiExprArg Args, |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 864 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 865 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 866 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 867 | return true; |
| 868 | |
| 869 | return false; |
| 870 | } |
| 871 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 872 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 873 | // overload of the declarations in Old. This routine returns false if |
| 874 | // New and Old cannot be overloaded, e.g., if New has the same |
| 875 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 876 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 877 | // it does return false, MatchedDecl will point to the decl that New |
| 878 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 879 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 880 | // |
| 881 | // Example: Given the following input: |
| 882 | // |
| 883 | // void f(int, float); // #1 |
| 884 | // void f(int, int); // #2 |
| 885 | // int f(int, int); // #3 |
| 886 | // |
| 887 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 889 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 890 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 891 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 892 | // (since they have different signatures), so this routine returns |
| 893 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 894 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 895 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 896 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 897 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 898 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 899 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 900 | // point to the FunctionDecl for #2. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 901 | // |
| 902 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 903 | // into a class by a using declaration. The rules for whether to hide |
| 904 | // shadow declarations ignore some properties which otherwise figure |
| 905 | // into a function template's signature. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 906 | Sema::OverloadKind |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 907 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 908 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 909 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 910 | I != E; ++I) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 911 | NamedDecl *OldD = *I; |
| 912 | |
| 913 | bool OldIsUsingDecl = false; |
| 914 | if (isa<UsingShadowDecl>(OldD)) { |
| 915 | OldIsUsingDecl = true; |
| 916 | |
| 917 | // We can always introduce two using declarations into the same |
| 918 | // context, even if they have identical signatures. |
| 919 | if (NewIsUsingDecl) continue; |
| 920 | |
| 921 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 922 | } |
| 923 | |
| 924 | // If either declaration was introduced by a using declaration, |
| 925 | // we'll need to use slightly different rules for matching. |
| 926 | // Essentially, these rules are the normal rules, except that |
| 927 | // function templates hide function templates with different |
| 928 | // return types or template parameter lists. |
| 929 | bool UseMemberUsingDeclRules = |
John McCall | c70fca6 | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 930 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 931 | !New->getFriendObjectKind(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 932 | |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 933 | if (FunctionDecl *OldF = OldD->getAsFunction()) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 934 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 935 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 936 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 937 | continue; |
| 938 | } |
| 939 | |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 940 | if (!isa<FunctionTemplateDecl>(OldD) && |
| 941 | !shouldLinkPossiblyHiddenDecl(*I, New)) |
Rafael Espindola | 5bddd6a | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 942 | continue; |
| 943 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 944 | Match = *I; |
| 945 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 946 | } |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 947 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 948 | // We can overload with these, which can show up when doing |
| 949 | // redeclaration checks for UsingDecls. |
| 950 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 951 | } else if (isa<TagDecl>(OldD)) { |
| 952 | // We can always overload with tags by hiding them. |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 953 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 954 | // Optimistically assume that an unresolved using decl will |
| 955 | // overload; if it doesn't, we'll have to diagnose during |
| 956 | // template instantiation. |
| 957 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 958 | // (C++ 13p1): |
| 959 | // Only function declarations can be overloaded; object and type |
| 960 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 961 | Match = *I; |
| 962 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 963 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 964 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 965 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 966 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 969 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 970 | bool UseUsingDeclRules) { |
| 971 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 972 | if (New->isMain()) |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 973 | return false; |
Rafael Espindola | 7cf35ef | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 974 | |
David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 975 | // MSVCRT user defined entry points cannot be overloaded. |
| 976 | if (New->isMSVCRTEntryPoint()) |
| 977 | return false; |
| 978 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 979 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 980 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 981 | |
| 982 | // C++ [temp.fct]p2: |
| 983 | // A function template can be overloaded with other function templates |
| 984 | // and with normal (non-template) functions. |
| 985 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 986 | return true; |
| 987 | |
| 988 | // Is the function New an overload of the function Old? |
Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 989 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 990 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 991 | |
| 992 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 993 | // determine whether they are overloads. If we find any mismatch |
| 994 | // in the signature, they are overloads. |
| 995 | |
| 996 | // If either of these functions is a K&R-style function (no |
| 997 | // prototype), then we consider them to have matching signatures. |
| 998 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 999 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1000 | return false; |
| 1001 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1002 | const FunctionProtoType *OldType = cast<FunctionProtoType>(OldQType); |
| 1003 | const FunctionProtoType *NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1004 | |
| 1005 | // The signature of a function includes the types of its |
| 1006 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1007 | // of the ellipsis; see C++ DR 357). |
| 1008 | if (OldQType != NewQType && |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1009 | (OldType->getNumParams() != NewType->getNumParams() || |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1010 | OldType->isVariadic() != NewType->isVariadic() || |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1011 | !FunctionParamTypesAreEqual(OldType, NewType))) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1012 | return true; |
| 1013 | |
| 1014 | // C++ [temp.over.link]p4: |
| 1015 | // The signature of a function template consists of its function |
| 1016 | // signature, its return type and its template parameter list. The names |
| 1017 | // of the template parameters are significant only for establishing the |
| 1018 | // relationship between the template parameters and the rest of the |
| 1019 | // signature. |
| 1020 | // |
| 1021 | // We check the return type and template parameter lists for function |
| 1022 | // templates first; the remaining checks follow. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1023 | // |
| 1024 | // However, we don't consider either of these when deciding whether |
| 1025 | // a member introduced by a shadow declaration is hidden. |
| 1026 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1027 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1028 | OldTemplate->getTemplateParameters(), |
| 1029 | false, TPL_TemplateMatch) || |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1030 | OldType->getReturnType() != NewType->getReturnType())) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1031 | return true; |
| 1032 | |
| 1033 | // If the function is a class member, its signature includes the |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1034 | // 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] | 1035 | // |
| 1036 | // As part of this, also check whether one of the member functions |
| 1037 | // is static, in which case they are not overloads (C++ |
| 1038 | // 13.1p2). While not part of the definition of the signature, |
| 1039 | // this check is important to determine whether these functions |
| 1040 | // can be overloaded. |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1041 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1042 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1043 | if (OldMethod && NewMethod && |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1044 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1045 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1046 | if (!UseUsingDeclRules && |
| 1047 | (OldMethod->getRefQualifier() == RQ_None || |
| 1048 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1049 | // C++0x [over.load]p2: |
| 1050 | // - Member function declarations with the same name and the same |
| 1051 | // parameter-type-list as well as member function template |
| 1052 | // declarations with the same name, the same parameter-type-list, and |
| 1053 | // the same template parameter lists cannot be overloaded if any of |
| 1054 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1055 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1056 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1057 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1058 | } |
| 1059 | return true; |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1060 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1061 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1062 | // We may not have applied the implicit const for a constexpr member |
| 1063 | // function yet (because we haven't yet resolved whether this is a static |
| 1064 | // or non-static member function). Add it now, on the assumption that this |
| 1065 | // is a redeclaration of OldMethod. |
David Majnemer | 42350df | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1066 | unsigned OldQuals = OldMethod->getTypeQualifiers(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1067 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | ac974a3 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1068 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | e83b1d3 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1069 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1070 | NewQuals |= Qualifiers::Const; |
David Majnemer | 42350df | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1071 | |
| 1072 | // We do not allow overloading based off of '__restrict'. |
| 1073 | OldQuals &= ~Qualifiers::Restrict; |
| 1074 | NewQuals &= ~Qualifiers::Restrict; |
| 1075 | if (OldQuals != NewQuals) |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1076 | return true; |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1077 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1079 | // enable_if attributes are an order-sensitive part of the signature. |
| 1080 | for (specific_attr_iterator<EnableIfAttr> |
| 1081 | NewI = New->specific_attr_begin<EnableIfAttr>(), |
| 1082 | NewE = New->specific_attr_end<EnableIfAttr>(), |
| 1083 | OldI = Old->specific_attr_begin<EnableIfAttr>(), |
| 1084 | OldE = Old->specific_attr_end<EnableIfAttr>(); |
| 1085 | NewI != NewE || OldI != OldE; ++NewI, ++OldI) { |
| 1086 | if (NewI == NewE || OldI == OldE) |
| 1087 | return true; |
| 1088 | llvm::FoldingSetNodeID NewID, OldID; |
| 1089 | NewI->getCond()->Profile(NewID, Context, true); |
| 1090 | OldI->getCond()->Profile(OldID, Context, true); |
Nick Lewycky | d950ae7 | 2014-01-21 01:30:30 +0000 | [diff] [blame] | 1091 | if (NewID != OldID) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
| 1094 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1095 | // The signatures match; this is not an overload. |
| 1096 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1099 | /// \brief Checks availability of the function depending on the current |
| 1100 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1101 | /// |
| 1102 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1103 | /// an available function, false otherwise. |
| 1104 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1105 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1106 | } |
| 1107 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1108 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1109 | /// |
| 1110 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1111 | /// is not an option. See TryImplicitConversion for more information. |
| 1112 | static ImplicitConversionSequence |
| 1113 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1114 | bool SuppressUserConversions, |
| 1115 | bool AllowExplicit, |
| 1116 | bool InOverloadResolution, |
| 1117 | bool CStyle, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1118 | bool AllowObjCWritebackConversion, |
| 1119 | bool AllowObjCConversionOnExplicit) { |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1120 | ImplicitConversionSequence ICS; |
| 1121 | |
| 1122 | if (SuppressUserConversions) { |
| 1123 | // We're not in the case above, so there is no conversion that |
| 1124 | // we can perform. |
| 1125 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1126 | return ICS; |
| 1127 | } |
| 1128 | |
| 1129 | // Attempt user-defined conversion. |
| 1130 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1131 | OverloadingResult UserDefResult |
| 1132 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1133 | AllowExplicit, AllowObjCConversionOnExplicit); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (UserDefResult == OR_Success) { |
| 1136 | ICS.setUserDefined(); |
Ismail Pazarbasi | df1a280 | 2014-01-24 13:16:17 +0000 | [diff] [blame] | 1137 | ICS.UserDefined.Before.setAsIdentityConversion(); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1138 | // C++ [over.ics.user]p4: |
| 1139 | // A conversion of an expression of class type to the same class |
| 1140 | // type is given Exact Match rank, and a conversion of an |
| 1141 | // expression of class type to a base class of that type is |
| 1142 | // given Conversion rank, in spite of the fact that a copy |
| 1143 | // constructor (i.e., a user-defined conversion function) is |
| 1144 | // called for those cases. |
| 1145 | if (CXXConstructorDecl *Constructor |
| 1146 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1147 | QualType FromCanon |
| 1148 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1149 | QualType ToCanon |
| 1150 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1151 | if (Constructor->isCopyConstructor() && |
| 1152 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1153 | // Turn this into a "standard" conversion sequence, so that it |
| 1154 | // gets ranked with standard conversion sequences. |
| 1155 | ICS.setStandard(); |
| 1156 | ICS.Standard.setAsIdentityConversion(); |
| 1157 | ICS.Standard.setFromType(From->getType()); |
| 1158 | ICS.Standard.setAllToTypes(ToType); |
| 1159 | ICS.Standard.CopyConstructor = Constructor; |
| 1160 | if (ToCanon != FromCanon) |
| 1161 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | // C++ [over.best.ics]p4: |
| 1166 | // However, when considering the argument of a user-defined |
| 1167 | // conversion function that is a candidate by 13.3.1.3 when |
| 1168 | // invoked for the copying of the temporary in the second step |
| 1169 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1170 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1171 | // ellipsis conversion sequences are allowed. |
| 1172 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1173 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1174 | } |
| 1175 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1176 | ICS.setAmbiguous(); |
| 1177 | ICS.Ambiguous.setFromType(From->getType()); |
| 1178 | ICS.Ambiguous.setToType(ToType); |
| 1179 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1180 | Cand != Conversions.end(); ++Cand) |
| 1181 | if (Cand->Viable) |
| 1182 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1183 | } else { |
| 1184 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1185 | } |
| 1186 | |
| 1187 | return ICS; |
| 1188 | } |
| 1189 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1190 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1191 | /// from the given expression (Expr) to the given type (ToType). This |
| 1192 | /// function returns an implicit conversion sequence that can be used |
| 1193 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1194 | /// |
| 1195 | /// void f(float f); |
| 1196 | /// void g(int i) { f(i); } |
| 1197 | /// |
| 1198 | /// this routine would produce an implicit conversion sequence to |
| 1199 | /// describe the initialization of f from i, which will be a standard |
| 1200 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1201 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1202 | // |
| 1203 | /// Note that this routine only determines how the conversion can be |
| 1204 | /// performed; it does not actually perform the conversion. As such, |
| 1205 | /// it will not produce any diagnostics if no conversion is available, |
| 1206 | /// but will instead return an implicit conversion sequence of kind |
| 1207 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1208 | /// |
| 1209 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1210 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1211 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1212 | /// permitted. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1213 | /// |
| 1214 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1215 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1216 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1217 | static ImplicitConversionSequence |
| 1218 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1219 | bool SuppressUserConversions, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1220 | bool AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1221 | bool InOverloadResolution, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1222 | bool CStyle, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1223 | bool AllowObjCWritebackConversion, |
| 1224 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1225 | ImplicitConversionSequence ICS; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1226 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1227 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1228 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1229 | return ICS; |
| 1230 | } |
| 1231 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1232 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1233 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1234 | return ICS; |
| 1235 | } |
| 1236 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1237 | // C++ [over.ics.user]p4: |
| 1238 | // A conversion of an expression of class type to the same class |
| 1239 | // type is given Exact Match rank, and a conversion of an |
| 1240 | // expression of class type to a base class of that type is |
| 1241 | // given Conversion rank, in spite of the fact that a copy/move |
| 1242 | // constructor (i.e., a user-defined conversion function) is |
| 1243 | // called for those cases. |
| 1244 | QualType FromType = From->getType(); |
| 1245 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1246 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1247 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1248 | ICS.setStandard(); |
| 1249 | ICS.Standard.setAsIdentityConversion(); |
| 1250 | ICS.Standard.setFromType(FromType); |
| 1251 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1253 | // We don't actually check at this point whether there is a valid |
| 1254 | // copy/move constructor, since overloading just assumes that it |
| 1255 | // exists. When we actually perform initialization, we'll find the |
| 1256 | // appropriate constructor to copy the returned object, if needed. |
| 1257 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1258 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1259 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1260 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1261 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1262 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1263 | return ICS; |
| 1264 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1265 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1266 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1267 | AllowExplicit, InOverloadResolution, CStyle, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1268 | AllowObjCWritebackConversion, |
| 1269 | AllowObjCConversionOnExplicit); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1272 | ImplicitConversionSequence |
| 1273 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1274 | bool SuppressUserConversions, |
| 1275 | bool AllowExplicit, |
| 1276 | bool InOverloadResolution, |
| 1277 | bool CStyle, |
| 1278 | bool AllowObjCWritebackConversion) { |
| 1279 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1280 | SuppressUserConversions, AllowExplicit, |
| 1281 | InOverloadResolution, CStyle, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1282 | AllowObjCWritebackConversion, |
| 1283 | /*AllowObjCConversionOnExplicit=*/false); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1286 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1287 | /// expression From to the type ToType. Returns the |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1288 | /// converted expression. Flavor is the kind of conversion we're |
| 1289 | /// performing, used in the error message. If @p AllowExplicit, |
| 1290 | /// explicit user-defined conversions are permitted. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1291 | ExprResult |
| 1292 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1293 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1294 | ImplicitConversionSequence ICS; |
Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1295 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1298 | ExprResult |
| 1299 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1300 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1301 | ImplicitConversionSequence& ICS) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1302 | if (checkPlaceholderForOverload(*this, From)) |
| 1303 | return ExprError(); |
| 1304 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1305 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1306 | bool AllowObjCWritebackConversion |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1307 | = getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1308 | (Action == AA_Passing || Action == AA_Sending); |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 1309 | if (getLangOpts().ObjC1) |
| 1310 | CheckObjCBridgeRelatedConversions(From->getLocStart(), |
| 1311 | ToType, From->getType(), From); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1312 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1313 | /*SuppressUserConversions=*/false, |
| 1314 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1315 | /*InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1316 | /*CStyle=*/false, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1317 | AllowObjCWritebackConversion, |
| 1318 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1319 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1320 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1321 | |
| 1322 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1323 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1324 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1325 | QualType &ResultTy) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1326 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1327 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1328 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1329 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1330 | // where F adds one of the following at most once: |
| 1331 | // - a pointer |
| 1332 | // - a member pointer |
| 1333 | // - a block pointer |
| 1334 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1335 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1336 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1337 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1338 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1339 | if (TyClass == Type::Pointer) { |
| 1340 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1341 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1342 | } else if (TyClass == Type::BlockPointer) { |
| 1343 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1344 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1345 | } else if (TyClass == Type::MemberPointer) { |
| 1346 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1347 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1348 | } else { |
| 1349 | return false; |
| 1350 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1351 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1352 | TyClass = CanTo->getTypeClass(); |
| 1353 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1354 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1355 | return false; |
| 1356 | } |
| 1357 | |
| 1358 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1359 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1360 | if (!EInfo.getNoReturn()) return false; |
| 1361 | |
| 1362 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1363 | assert(QualType(FromFn, 0).isCanonical()); |
| 1364 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1365 | |
| 1366 | ResultTy = ToType; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1367 | return true; |
| 1368 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1369 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1370 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1371 | /// vector conversion. |
| 1372 | /// |
| 1373 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1374 | /// conversion. |
John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1375 | static bool IsVectorConversion(Sema &S, QualType FromType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1376 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1377 | // We need at least one of these types to be a vector type to have a vector |
| 1378 | // conversion. |
| 1379 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1380 | return false; |
| 1381 | |
| 1382 | // Identical types require no conversions. |
John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1383 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1384 | return false; |
| 1385 | |
| 1386 | // There are no conversions between extended vector types, only identity. |
| 1387 | if (ToType->isExtVectorType()) { |
| 1388 | // There are no conversions between extended vector types other than the |
| 1389 | // identity conversion. |
| 1390 | if (FromType->isExtVectorType()) |
| 1391 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1393 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1394 | if (FromType->isArithmeticType()) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1395 | ICK = ICK_Vector_Splat; |
| 1396 | return true; |
| 1397 | } |
| 1398 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1399 | |
| 1400 | // We can perform the conversion between vector types in the following cases: |
| 1401 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1402 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1403 | // same size |
| 1404 | if (ToType->isVectorType() && FromType->isVectorType()) { |
John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1405 | if (S.Context.areCompatibleVectorTypes(FromType, ToType) || |
| 1406 | S.isLaxVectorConversion(FromType, ToType)) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1407 | ICK = ICK_Vector_Conversion; |
| 1408 | return true; |
| 1409 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1415 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1416 | bool InOverloadResolution, |
| 1417 | StandardConversionSequence &SCS, |
| 1418 | bool CStyle); |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1420 | /// IsStandardConversion - Determines whether there is a standard |
| 1421 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1422 | /// expression From to the type ToType. Standard conversion sequences |
| 1423 | /// only consider non-class types; for conversions that involve class |
| 1424 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1425 | /// contain the standard conversion sequence required to perform this |
| 1426 | /// conversion and this routine will return true. Otherwise, this |
| 1427 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1428 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1429 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1430 | StandardConversionSequence &SCS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1431 | bool CStyle, |
| 1432 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1433 | QualType FromType = From->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1435 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1436 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1437 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1438 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1439 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1440 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1441 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1443 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1444 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1445 | return false; |
| 1446 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1450 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1451 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1452 | // (C++ 4p1). |
| 1453 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1454 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1455 | DeclAccessPair AccessPair; |
| 1456 | if (FunctionDecl *Fn |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1457 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1458 | AccessPair)) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1459 | // We were able to resolve the address of the overloaded function, |
| 1460 | // so we can convert to the type of that function. |
| 1461 | FromType = Fn->getType(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1462 | |
| 1463 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1464 | // if the type matches (identity) or we are converting to bool |
| 1465 | if (!S.Context.hasSameUnqualifiedType( |
| 1466 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1467 | QualType resultTy; |
| 1468 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1469 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1470 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1471 | // otherwise, only a boolean conversion is standard |
| 1472 | if (!ToType->isBooleanType()) |
| 1473 | return false; |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1474 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1475 | |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1476 | // Check if the "from" expression is taking the address of an overloaded |
| 1477 | // function and recompute the FromType accordingly. Take advantage of the |
| 1478 | // fact that non-static member functions *must* have such an address-of |
| 1479 | // expression. |
| 1480 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1481 | if (Method && !Method->isStatic()) { |
| 1482 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1483 | "Non-unary operator on non-static member address"); |
| 1484 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1485 | == UO_AddrOf && |
| 1486 | "Non-address-of operator on non-static member address"); |
| 1487 | const Type *ClassType |
| 1488 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1489 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | 7750f76 | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1490 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1491 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1492 | UO_AddrOf && |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1493 | "Non-address-of operator for overloaded function expression"); |
| 1494 | FromType = S.Context.getPointerType(FromType); |
| 1495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1497 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1498 | assert(S.Context.hasSameType( |
| 1499 | FromType, |
| 1500 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1501 | } else { |
| 1502 | return false; |
| 1503 | } |
Anders Carlsson | ba37e1e | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1504 | } |
John McCall | 154a2fd | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1505 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1506 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1507 | // be converted to a prvalue. |
| 1508 | bool argIsLValue = From->isGLValue(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1509 | if (argIsLValue && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1510 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1511 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1512 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1513 | |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1514 | // C11 6.3.2.1p2: |
| 1515 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1516 | // of the type of the lvalue ... |
| 1517 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1518 | FromType = Atomic->getValueType(); |
| 1519 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1520 | // If T is a non-class type, the type of the rvalue is the |
| 1521 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1522 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1523 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1524 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1525 | } else if (FromType->isArrayType()) { |
| 1526 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1527 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1528 | |
| 1529 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1530 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1531 | // T" (C++ 4.2p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1532 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1533 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1534 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 1535 | // This conversion is deprecated in C++03 (D.4) |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1536 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1537 | |
| 1538 | // For the purpose of ranking in overload resolution |
| 1539 | // (13.3.3.1.1), this conversion is considered an |
| 1540 | // array-to-pointer conversion followed by a qualification |
| 1541 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1542 | SCS.Second = ICK_Identity; |
| 1543 | SCS.Third = ICK_Qualification; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1544 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1545 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1546 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1547 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1548 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1549 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1550 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1551 | |
| 1552 | // An lvalue of function type T can be converted to an rvalue of |
| 1553 | // type "pointer to T." The result is a pointer to the |
| 1554 | // function. (C++ 4.3p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1555 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1556 | } else { |
| 1557 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1558 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1559 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1560 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1561 | |
| 1562 | // The second conversion can be an integral promotion, floating |
| 1563 | // point promotion, integral conversion, floating point conversion, |
| 1564 | // floating-integral conversion, pointer conversion, |
| 1565 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1566 | // For overloading in C, this can also be a "compatible-type" |
| 1567 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1568 | bool IncompatibleObjC = false; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1569 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1570 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1571 | // The unqualified versions of the types are the same: there's no |
| 1572 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1573 | SCS.Second = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1574 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1576 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1577 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1578 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1579 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1580 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1581 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1582 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1583 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1584 | SCS.Second = ICK_Complex_Promotion; |
| 1585 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1586 | } else if (ToType->isBooleanType() && |
| 1587 | (FromType->isArithmeticType() || |
| 1588 | FromType->isAnyPointerType() || |
| 1589 | FromType->isBlockPointerType() || |
| 1590 | FromType->isMemberPointerType() || |
| 1591 | FromType->isNullPtrType())) { |
| 1592 | // Boolean conversions (C++ 4.12). |
| 1593 | SCS.Second = ICK_Boolean_Conversion; |
| 1594 | FromType = S.Context.BoolTy; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1595 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1596 | ToType->isIntegralType(S.Context)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1597 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1598 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1599 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | b8a9824 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1600 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1601 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1602 | SCS.Second = ICK_Complex_Conversion; |
| 1603 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1604 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1605 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1606 | // Complex-real conversions (C99 6.3.1.7) |
| 1607 | SCS.Second = ICK_Complex_Real; |
| 1608 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1609 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1610 | // Floating point conversions (C++ 4.8). |
| 1611 | SCS.Second = ICK_Floating_Conversion; |
| 1612 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1613 | } else if ((FromType->isRealFloatingType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1614 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1615 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1616 | ToType->isRealFloatingType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1617 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1618 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1619 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1620 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1621 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1622 | } else if (AllowObjCWritebackConversion && |
| 1623 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1624 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1625 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1626 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1627 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1628 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1629 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1630 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1631 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1632 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1633 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1634 | SCS.Second = ICK_Pointer_Member; |
John McCall | 9b595db | 2014-02-04 23:58:19 +0000 | [diff] [blame] | 1635 | } else if (IsVectorConversion(S, FromType, ToType, SecondICK)) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1636 | SCS.Second = SecondICK; |
| 1637 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1638 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1639 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1640 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1641 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1642 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1643 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1644 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1645 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1646 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1647 | InOverloadResolution, |
| 1648 | SCS, CStyle)) { |
| 1649 | SCS.Second = ICK_TransparentUnionConversion; |
| 1650 | FromType = ToType; |
Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1651 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1652 | CStyle)) { |
| 1653 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1654 | // appropriately. |
| 1655 | return true; |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1656 | } else if (ToType->isEventT() && |
| 1657 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1658 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1659 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1660 | FromType = ToType; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1661 | } else { |
| 1662 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1663 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1664 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1665 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1666 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1667 | QualType CanonFrom; |
| 1668 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1669 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1670 | bool ObjCLifetimeConversion; |
| 1671 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1672 | ObjCLifetimeConversion)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1673 | SCS.Third = ICK_Qualification; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1674 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1675 | FromType = ToType; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1676 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1677 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1678 | } else { |
| 1679 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1680 | SCS.Third = ICK_Identity; |
| 1681 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1683 | // [...] Any difference in top-level cv-qualification is |
| 1684 | // subsumed by the initialization itself and does not constitute |
| 1685 | // a conversion. [...] |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1686 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1687 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1688 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1689 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 7d36c01 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1690 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1691 | FromType = ToType; |
| 1692 | CanonFrom = CanonTo; |
| 1693 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1694 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1695 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1696 | |
| 1697 | // If we have not converted the argument type to the parameter type, |
| 1698 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1699 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1700 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1701 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1702 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1703 | } |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1704 | |
| 1705 | static bool |
| 1706 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1707 | QualType &ToType, |
| 1708 | bool InOverloadResolution, |
| 1709 | StandardConversionSequence &SCS, |
| 1710 | bool CStyle) { |
| 1711 | |
| 1712 | const RecordType *UT = ToType->getAsUnionType(); |
| 1713 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1714 | return false; |
| 1715 | // The field to initialize within the transparent union. |
| 1716 | RecordDecl *UD = UT->getDecl(); |
| 1717 | // It's compatible if the expression matches any of the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1718 | for (const auto *it : UD->fields()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1719 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1720 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1721 | ToType = it->getType(); |
| 1722 | return true; |
| 1723 | } |
| 1724 | } |
| 1725 | return false; |
| 1726 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1727 | |
| 1728 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1729 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1730 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1731 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1733 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1734 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1735 | if (!To) { |
| 1736 | return false; |
| 1737 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1738 | |
| 1739 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1740 | // unsigned short int can be converted to an rvalue of type int if |
| 1741 | // int can represent all the values of the source type; otherwise, |
| 1742 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1743 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1744 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1745 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1746 | if (// We can promote any signed, promotable integer type to an int |
| 1747 | (FromType->isSignedIntegerType() || |
| 1748 | // We can promote any unsigned integer type whose size is |
| 1749 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1751 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1752 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1755 | return To->getKind() == BuiltinType::UInt; |
| 1756 | } |
| 1757 | |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1758 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1759 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1760 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1761 | // following types that can represent all the values of the enumeration |
| 1762 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1763 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1764 | // 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] | 1765 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1766 | // 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] | 1767 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1768 | // 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] | 1769 | // there are two such extended types, the signed one is chosen. |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1770 | // C++11 [conv.prom]p4: |
| 1771 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1772 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1773 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1774 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1775 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1776 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1777 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1778 | // provided for a scoped enumeration. |
| 1779 | if (FromEnumType->getDecl()->isScoped()) |
| 1780 | return false; |
| 1781 | |
Richard Smith | b9c5a60 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1782 | // We can perform an integral promotion to the underlying type of the enum, |
| 1783 | // even if that's not the promoted type. |
| 1784 | if (FromEnumType->getDecl()->isFixed()) { |
| 1785 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1786 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1787 | IsIntegralPromotion(From, Underlying, ToType); |
| 1788 | } |
| 1789 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1790 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1791 | if (ToType->isIntegerType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1792 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1793 | return Context.hasSameUnqualifiedType(ToType, |
| 1794 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1795 | } |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1797 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1798 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1799 | // to an rvalue a prvalue of the first of the following types that can |
| 1800 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1801 | // 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] | 1802 | // 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] | 1803 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1804 | // 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] | 1805 | // type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1806 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1807 | ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1808 | // Determine whether the type we're converting from is signed or |
| 1809 | // unsigned. |
David Majnemer | fa01a58 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1810 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1811 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1812 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1813 | // The types we'll try to promote to, in the appropriate |
| 1814 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | QualType PromoteTypes[6] = { |
| 1816 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1817 | Context.LongTy, Context.UnsignedLongTy , |
| 1818 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1819 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1820 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1821 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1822 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1824 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1825 | // We found the type that we can promote to. If this is the |
| 1826 | // type we wanted, we have a promotion. Otherwise, no |
| 1827 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1828 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1834 | // rvalue of type int if int can represent all the values of the |
| 1835 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1836 | // unsigned int can represent all the values of the bit-field. If |
| 1837 | // the bit-field is larger yet, no integral promotion applies to |
| 1838 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1839 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1840 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1841 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1842 | using llvm::APSInt; |
| 1843 | if (From) |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1844 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1845 | APSInt BitWidth; |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1846 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1847 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1848 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1849 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1851 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1852 | if (BitWidth < ToSize || |
| 1853 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1854 | return To->getKind() == BuiltinType::Int; |
| 1855 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1857 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1858 | // that fits into an unsigned int? |
| 1859 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1860 | return To->getKind() == BuiltinType::UInt; |
| 1861 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1863 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1864 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1865 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1867 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1868 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1869 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1870 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1871 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1872 | |
| 1873 | return false; |
| 1874 | } |
| 1875 | |
| 1876 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1877 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1878 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1880 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1881 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1882 | /// An rvalue of type float can be converted to an rvalue of type |
| 1883 | /// double. (C++ 4.6p1). |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1884 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1885 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1886 | return true; |
| 1887 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1888 | // C99 6.3.1.5p1: |
| 1889 | // When a float is promoted to double or long double, or a |
| 1890 | // double is promoted to long double [...]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1891 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1892 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1893 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1894 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1895 | return true; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1896 | |
| 1897 | // Half can be promoted to float. |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1898 | if (!getLangOpts().NativeHalfType && |
| 1899 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1900 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1901 | return true; |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1904 | return false; |
| 1905 | } |
| 1906 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1907 | /// \brief Determine if a conversion is a complex promotion. |
| 1908 | /// |
| 1909 | /// A complex promotion is defined as a complex -> complex conversion |
| 1910 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1911 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1912 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1913 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1914 | if (!FromComplex) |
| 1915 | return false; |
| 1916 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1917 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1918 | if (!ToComplex) |
| 1919 | return false; |
| 1920 | |
| 1921 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1922 | ToComplex->getElementType()) || |
| 1923 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1924 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1927 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1928 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1929 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1930 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1931 | /// the right set of qualifiers on its pointee. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1932 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | static QualType |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1934 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1935 | QualType ToPointee, QualType ToType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1936 | ASTContext &Context, |
| 1937 | bool StripObjCLifetime = false) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1938 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1939 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1940 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1941 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1942 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1943 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | c6bd1d3 | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1944 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1945 | |
| 1946 | QualType CanonFromPointee |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1947 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1948 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1949 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1951 | if (StripObjCLifetime) |
| 1952 | Quals.removeObjCLifetime(); |
| 1953 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1955 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1956 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1957 | if (!ToType.isNull()) |
Douglas Gregor | b9f907b | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1958 | return ToType.getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1961 | // already. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1962 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1963 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1964 | return Context.getPointerType(ToPointee); |
| 1965 | } |
| 1966 | |
| 1967 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1968 | QualType QualifiedCanonToPointee |
| 1969 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1970 | |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1971 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1972 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1973 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1974 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1975 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1977 | bool InOverloadResolution, |
| 1978 | ASTContext &Context) { |
| 1979 | // Handle value-dependent integral null pointer constants correctly. |
| 1980 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1981 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1982 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1983 | return !InOverloadResolution; |
| 1984 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1985 | return Expr->isNullPointerConstant(Context, |
| 1986 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1987 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1988 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1990 | /// IsPointerConversion - Determines whether the conversion of the |
| 1991 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1992 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1993 | /// 4.10). If so, returns true and places the converted type (that |
| 1994 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1995 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1996 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1997 | /// This routine also supports conversions to and from block pointers |
| 1998 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1999 | /// pointers to interfaces. FIXME: Once we've determined the |
| 2000 | /// appropriate overloading rules for Objective-C, we may want to |
| 2001 | /// split the Objective-C checks into a different routine; however, |
| 2002 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2003 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 2004 | /// set if the conversion is an allowed Objective-C conversion that |
| 2005 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2006 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2007 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2008 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2010 | IncompatibleObjC = false; |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2011 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 2012 | IncompatibleObjC)) |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2013 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2014 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 2016 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2017 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 2018 | ConvertedType = ToType; |
| 2019 | return true; |
| 2020 | } |
| 2021 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2022 | // Blocks: Block pointers can be converted to void*. |
| 2023 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2024 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2025 | ConvertedType = ToType; |
| 2026 | return true; |
| 2027 | } |
| 2028 | // Blocks: A null pointer constant can be converted to a block |
| 2029 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2031 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2032 | ConvertedType = ToType; |
| 2033 | return true; |
| 2034 | } |
| 2035 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2036 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2037 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2039 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2040 | ConvertedType = ToType; |
| 2041 | return true; |
| 2042 | } |
| 2043 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2044 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2045 | if (!ToTypePtr) |
| 2046 | return false; |
| 2047 | |
| 2048 | // 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] | 2049 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2050 | ConvertedType = ToType; |
| 2051 | return true; |
| 2052 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2053 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2054 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2055 | // , including objective-c pointers. |
| 2056 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2057 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2058 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2059 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2060 | FromType->getAs<ObjCObjectPointerType>(), |
| 2061 | ToPointeeType, |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2062 | ToType, Context); |
| 2063 | return true; |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2064 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2065 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2066 | if (!FromTypePtr) |
| 2067 | return false; |
| 2068 | |
| 2069 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2070 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | // 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] | 2072 | // pointer conversion, so don't do all of the work below. |
| 2073 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2074 | return false; |
| 2075 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2076 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2077 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2078 | // 4.10p2). |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2079 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2080 | ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2082 | ToPointeeType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2083 | ToType, Context, |
| 2084 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2085 | return true; |
| 2086 | } |
| 2087 | |
Francois Pichet | bc6ebb5 | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2088 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2089 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | bc6ebb5 | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2090 | ToPointeeType->isVoidType()) { |
| 2091 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2092 | ToPointeeType, |
| 2093 | ToType, Context); |
| 2094 | return true; |
| 2095 | } |
| 2096 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2097 | // When we're overloading in C, we allow a special kind of pointer |
| 2098 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2099 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2100 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2102 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2104 | return true; |
| 2105 | } |
| 2106 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2107 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2109 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2110 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2111 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2112 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2113 | // necessitates this conversion is ill-formed. The result of the |
| 2114 | // conversion is a pointer to the base class sub-object of the |
| 2115 | // derived class object. The null pointer value is converted to |
| 2116 | // the null pointer value of the destination type. |
| 2117 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2118 | // Note that we do not check for ambiguity or inaccessibility |
| 2119 | // here. That is handled by CheckPointerConversion. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2120 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2121 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2122 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2123 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2124 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2126 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2127 | ToType, Context); |
| 2128 | return true; |
| 2129 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2130 | |
Fariborz Jahanian | bc2ee93 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2131 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2132 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2133 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2134 | ToPointeeType, |
| 2135 | ToType, Context); |
| 2136 | return true; |
| 2137 | } |
| 2138 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2139 | return false; |
| 2140 | } |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2141 | |
| 2142 | /// \brief Adopt the given qualifiers for the given type. |
| 2143 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2144 | Qualifiers TQs = T.getQualifiers(); |
| 2145 | |
| 2146 | // Check whether qualifiers already match. |
| 2147 | if (TQs == Qs) |
| 2148 | return T; |
| 2149 | |
| 2150 | if (Qs.compatiblyIncludes(TQs)) |
| 2151 | return Context.getQualifiedType(T, Qs); |
| 2152 | |
| 2153 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2154 | } |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2155 | |
| 2156 | /// isObjCPointerConversion - Determines whether this is an |
| 2157 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2158 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2160 | QualType& ConvertedType, |
| 2161 | bool &IncompatibleObjC) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2162 | if (!getLangOpts().ObjC1) |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2163 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2165 | // The set of qualifiers on the type we're converting from. |
| 2166 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2167 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2168 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2169 | const ObjCObjectPointerType* ToObjCPtr = |
| 2170 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2172 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2173 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2174 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2175 | // If the pointee types are the same (ignoring qualifications), |
| 2176 | // then this is not a pointer conversion. |
| 2177 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2178 | FromObjCPtr->getPointeeType())) |
| 2179 | return false; |
| 2180 | |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2181 | // Check for compatible |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2182 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2183 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2184 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2185 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2186 | return true; |
| 2187 | } |
| 2188 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2190 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2192 | /*compare=*/false)) { |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2193 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2194 | return true; |
| 2195 | } |
| 2196 | // Objective C++: We're able to convert from a pointer to an |
| 2197 | // interface to a pointer to a different interface. |
| 2198 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2199 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2200 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2201 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2202 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2203 | FromObjCPtr->getPointeeType())) |
| 2204 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2205 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2206 | ToObjCPtr->getPointeeType(), |
| 2207 | ToType, Context); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2208 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2209 | return true; |
| 2210 | } |
| 2211 | |
| 2212 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2213 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2214 | // interfaces, which is permitted. However, we're going to |
| 2215 | // complain about it. |
| 2216 | IncompatibleObjC = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2217 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2218 | ToObjCPtr->getPointeeType(), |
| 2219 | ToType, Context); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2220 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2221 | return true; |
| 2222 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2224 | // 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] | 2225 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2226 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2227 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2228 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2229 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2230 | // 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] | 2231 | // to a block pointer type. |
| 2232 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2233 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2234 | return true; |
| 2235 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2236 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2237 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2238 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2239 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2240 | // 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] | 2241 | // pointer to any object. |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2242 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2243 | return true; |
| 2244 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2245 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2246 | return false; |
| 2247 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2248 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2249 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2250 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2251 | else if (const BlockPointerType *FromBlockPtr = |
| 2252 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2253 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2254 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2255 | return false; |
| 2256 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2257 | // If we have pointers to pointers, recursively check whether this |
| 2258 | // is an Objective-C conversion. |
| 2259 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2260 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2261 | IncompatibleObjC)) { |
| 2262 | // We always complain about this conversion. |
| 2263 | IncompatibleObjC = true; |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2264 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2265 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2266 | return true; |
| 2267 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2268 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2269 | // as in I* to id. |
| 2270 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2271 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2272 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2273 | IncompatibleObjC)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2275 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2276 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2277 | return true; |
| 2278 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2279 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2280 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2281 | // differences in the argument and result types are in Objective-C |
| 2282 | // pointer conversions. If so, we permit the conversion (but |
| 2283 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2285 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2286 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2287 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2288 | if (FromFunctionType && ToFunctionType) { |
| 2289 | // If the function types are exactly the same, this isn't an |
| 2290 | // Objective-C pointer conversion. |
| 2291 | if (Context.getCanonicalType(FromPointeeType) |
| 2292 | == Context.getCanonicalType(ToPointeeType)) |
| 2293 | return false; |
| 2294 | |
| 2295 | // Perform the quick checks that will tell us whether these |
| 2296 | // function types are obviously different. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2297 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2298 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2299 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2300 | return false; |
| 2301 | |
| 2302 | bool HasObjCConversion = false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2303 | if (Context.getCanonicalType(FromFunctionType->getReturnType()) == |
| 2304 | Context.getCanonicalType(ToFunctionType->getReturnType())) { |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2305 | // Okay, the types match exactly. Nothing to do. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2306 | } else if (isObjCPointerConversion(FromFunctionType->getReturnType(), |
| 2307 | ToFunctionType->getReturnType(), |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2308 | ConvertedType, IncompatibleObjC)) { |
| 2309 | // Okay, we have an Objective-C pointer conversion. |
| 2310 | HasObjCConversion = true; |
| 2311 | } else { |
| 2312 | // Function types are too different. Abort. |
| 2313 | return false; |
| 2314 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2316 | // Check argument types. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2317 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2318 | ArgIdx != NumArgs; ++ArgIdx) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2319 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
| 2320 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2321 | if (Context.getCanonicalType(FromArgType) |
| 2322 | == Context.getCanonicalType(ToArgType)) { |
| 2323 | // Okay, the types match exactly. Nothing to do. |
| 2324 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2325 | ConvertedType, IncompatibleObjC)) { |
| 2326 | // Okay, we have an Objective-C pointer conversion. |
| 2327 | HasObjCConversion = true; |
| 2328 | } else { |
| 2329 | // Argument types are too different. Abort. |
| 2330 | return false; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | if (HasObjCConversion) { |
| 2335 | // We had an Objective-C conversion. Allow this pointer |
| 2336 | // conversion, but complain about it. |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2337 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2338 | IncompatibleObjC = true; |
| 2339 | return true; |
| 2340 | } |
| 2341 | } |
| 2342 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2343 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2344 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2345 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2346 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2347 | /// used for parameter passing when performing automatic reference counting. |
| 2348 | /// |
| 2349 | /// \param FromType The type we're converting form. |
| 2350 | /// |
| 2351 | /// \param ToType The type we're converting to. |
| 2352 | /// |
| 2353 | /// \param ConvertedType The type that will be produced after applying |
| 2354 | /// this conversion. |
| 2355 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2356 | QualType &ConvertedType) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2357 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2358 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2359 | return false; |
| 2360 | |
| 2361 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2362 | QualType ToPointee; |
| 2363 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2364 | ToPointee = ToPointer->getPointeeType(); |
| 2365 | else |
| 2366 | return false; |
| 2367 | |
| 2368 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2369 | if (!ToPointee->isObjCLifetimeType() || |
| 2370 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2371 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2372 | return false; |
| 2373 | |
| 2374 | // Argument must be a pointer to __strong to __weak. |
| 2375 | QualType FromPointee; |
| 2376 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2377 | FromPointee = FromPointer->getPointeeType(); |
| 2378 | else |
| 2379 | return false; |
| 2380 | |
| 2381 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2382 | if (!FromPointee->isObjCLifetimeType() || |
| 2383 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2384 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2385 | return false; |
| 2386 | |
| 2387 | // Make sure that we have compatible qualifiers. |
| 2388 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2389 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2390 | return false; |
| 2391 | |
| 2392 | // Remove qualifiers from the pointee type we're converting from; they |
| 2393 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2394 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2395 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2396 | |
| 2397 | // The unqualified form of the pointee types must be compatible. |
| 2398 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2399 | bool IncompatibleObjC; |
| 2400 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2401 | FromPointee = ToPointee; |
| 2402 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2403 | IncompatibleObjC)) |
| 2404 | return false; |
| 2405 | |
| 2406 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2407 | /// __autoreleasing pointee. |
| 2408 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2409 | ConvertedType = Context.getPointerType(FromPointee); |
| 2410 | return true; |
| 2411 | } |
| 2412 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2413 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2414 | QualType& ConvertedType) { |
| 2415 | QualType ToPointeeType; |
| 2416 | if (const BlockPointerType *ToBlockPtr = |
| 2417 | ToType->getAs<BlockPointerType>()) |
| 2418 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2419 | else |
| 2420 | return false; |
| 2421 | |
| 2422 | QualType FromPointeeType; |
| 2423 | if (const BlockPointerType *FromBlockPtr = |
| 2424 | FromType->getAs<BlockPointerType>()) |
| 2425 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2426 | else |
| 2427 | return false; |
| 2428 | // We have pointer to blocks, check whether the only |
| 2429 | // differences in the argument and result types are in Objective-C |
| 2430 | // pointer conversions. If so, we permit the conversion. |
| 2431 | |
| 2432 | const FunctionProtoType *FromFunctionType |
| 2433 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2434 | const FunctionProtoType *ToFunctionType |
| 2435 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2436 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2437 | if (!FromFunctionType || !ToFunctionType) |
| 2438 | return false; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2439 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2440 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2441 | return true; |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2442 | |
| 2443 | // Perform the quick checks that will tell us whether these |
| 2444 | // function types are obviously different. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2445 | if (FromFunctionType->getNumParams() != ToFunctionType->getNumParams() || |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2446 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2447 | return false; |
| 2448 | |
| 2449 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2450 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2451 | if (FromEInfo != ToEInfo) |
| 2452 | return false; |
| 2453 | |
| 2454 | bool IncompatibleObjC = false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2455 | if (Context.hasSameType(FromFunctionType->getReturnType(), |
| 2456 | ToFunctionType->getReturnType())) { |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2457 | // Okay, the types match exactly. Nothing to do. |
| 2458 | } else { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2459 | QualType RHS = FromFunctionType->getReturnType(); |
| 2460 | QualType LHS = ToFunctionType->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2461 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2462 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2463 | LHS = LHS.getUnqualifiedType(); |
| 2464 | |
| 2465 | if (Context.hasSameType(RHS,LHS)) { |
| 2466 | // OK exact match. |
| 2467 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2468 | ConvertedType, IncompatibleObjC)) { |
| 2469 | if (IncompatibleObjC) |
| 2470 | return false; |
| 2471 | // Okay, we have an Objective-C pointer conversion. |
| 2472 | } |
| 2473 | else |
| 2474 | return false; |
| 2475 | } |
| 2476 | |
| 2477 | // Check argument types. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2478 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumParams(); |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2479 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2480 | IncompatibleObjC = false; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2481 | QualType FromArgType = FromFunctionType->getParamType(ArgIdx); |
| 2482 | QualType ToArgType = ToFunctionType->getParamType(ArgIdx); |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2483 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2484 | // Okay, the types match exactly. Nothing to do. |
| 2485 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2486 | ConvertedType, IncompatibleObjC)) { |
| 2487 | if (IncompatibleObjC) |
| 2488 | return false; |
| 2489 | // Okay, we have an Objective-C pointer conversion. |
| 2490 | } else |
| 2491 | // Argument types are too different. Abort. |
| 2492 | return false; |
| 2493 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2494 | if (LangOpts.ObjCAutoRefCount && |
| 2495 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2496 | ToFunctionType)) |
| 2497 | return false; |
Fariborz Jahanian | 600ba20 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2498 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2499 | ConvertedType = ToType; |
| 2500 | return true; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2503 | enum { |
| 2504 | ft_default, |
| 2505 | ft_different_class, |
| 2506 | ft_parameter_arity, |
| 2507 | ft_parameter_mismatch, |
| 2508 | ft_return_type, |
| 2509 | ft_qualifer_mismatch |
| 2510 | }; |
| 2511 | |
| 2512 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2513 | /// function types. Catches different number of parameter, mismatch in |
| 2514 | /// parameter types, and different return types. |
| 2515 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2516 | QualType FromType, QualType ToType) { |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2517 | // If either type is not valid, include no extra info. |
| 2518 | if (FromType.isNull() || ToType.isNull()) { |
| 2519 | PDiag << ft_default; |
| 2520 | return; |
| 2521 | } |
| 2522 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2523 | // Get the function type from the pointers. |
| 2524 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2525 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2526 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2527 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2528 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2529 | << QualType(FromMember->getClass(), 0); |
| 2530 | return; |
| 2531 | } |
| 2532 | FromType = FromMember->getPointeeType(); |
| 2533 | ToType = ToMember->getPointeeType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2536 | if (FromType->isPointerType()) |
| 2537 | FromType = FromType->getPointeeType(); |
| 2538 | if (ToType->isPointerType()) |
| 2539 | ToType = ToType->getPointeeType(); |
| 2540 | |
| 2541 | // Remove references. |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2542 | FromType = FromType.getNonReferenceType(); |
| 2543 | ToType = ToType.getNonReferenceType(); |
| 2544 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2545 | // Don't print extra info for non-specialized template functions. |
| 2546 | if (FromType->isInstantiationDependentType() && |
| 2547 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2548 | PDiag << ft_default; |
| 2549 | return; |
| 2550 | } |
| 2551 | |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2552 | // No extra info for same types. |
| 2553 | if (Context.hasSameType(FromType, ToType)) { |
| 2554 | PDiag << ft_default; |
| 2555 | return; |
| 2556 | } |
| 2557 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2558 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2559 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2560 | |
| 2561 | // Both types need to be function types. |
| 2562 | if (!FromFunction || !ToFunction) { |
| 2563 | PDiag << ft_default; |
| 2564 | return; |
| 2565 | } |
| 2566 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2567 | if (FromFunction->getNumParams() != ToFunction->getNumParams()) { |
| 2568 | PDiag << ft_parameter_arity << ToFunction->getNumParams() |
| 2569 | << FromFunction->getNumParams(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2570 | return; |
| 2571 | } |
| 2572 | |
| 2573 | // Handle different parameter types. |
| 2574 | unsigned ArgPos; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2575 | if (!FunctionParamTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2576 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2577 | << ToFunction->getParamType(ArgPos) |
| 2578 | << FromFunction->getParamType(ArgPos); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | // Handle different return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2583 | if (!Context.hasSameType(FromFunction->getReturnType(), |
| 2584 | ToFunction->getReturnType())) { |
| 2585 | PDiag << ft_return_type << ToFunction->getReturnType() |
| 2586 | << FromFunction->getReturnType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2587 | return; |
| 2588 | } |
| 2589 | |
| 2590 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2591 | ToQuals = ToFunction->getTypeQuals(); |
| 2592 | if (FromQuals != ToQuals) { |
| 2593 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2594 | return; |
| 2595 | } |
| 2596 | |
| 2597 | // Unable to find a difference, so add no extra info. |
| 2598 | PDiag << ft_default; |
| 2599 | } |
| 2600 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2601 | /// FunctionParamTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | 2039ca0 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2602 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 5f50895 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2603 | /// they have same number of arguments. If the parameters are different, |
| 2604 | /// ArgPos will have the parameter index of the first different parameter. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2605 | bool Sema::FunctionParamTypesAreEqual(const FunctionProtoType *OldType, |
| 2606 | const FunctionProtoType *NewType, |
| 2607 | unsigned *ArgPos) { |
| 2608 | for (FunctionProtoType::param_type_iterator O = OldType->param_type_begin(), |
| 2609 | N = NewType->param_type_begin(), |
| 2610 | E = OldType->param_type_end(); |
| 2611 | O && (O != E); ++O, ++N) { |
Richard Trieu | 4b03d98 | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2612 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2613 | N->getUnqualifiedType())) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2614 | if (ArgPos) |
| 2615 | *ArgPos = O - OldType->param_type_begin(); |
Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2616 | return false; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2617 | } |
| 2618 | } |
| 2619 | return true; |
| 2620 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2621 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2622 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2623 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2624 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2625 | /// conversions for which IsPointerConversion has already returned |
| 2626 | /// true. It returns true and produces a diagnostic if there was an |
| 2627 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2628 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2629 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2630 | CXXCastPath& BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2631 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2632 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | d6ea6bd | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2633 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2634 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2635 | Kind = CK_BitCast; |
| 2636 | |
David Blaikie | 1c7c8f7 | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2637 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
Argyrios Kyrtzidis | 3e3305d | 2014-02-02 05:26:43 +0000 | [diff] [blame] | 2638 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
David Blaikie | 1c7c8f7 | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2639 | Expr::NPCK_ZeroExpression) { |
| 2640 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2641 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2642 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2643 | << ToType << From->getSourceRange()); |
| 2644 | else if (!isUnevaluatedContext()) |
| 2645 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2646 | << ToType << From->getSourceRange(); |
| 2647 | } |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2648 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2649 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2650 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2651 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2652 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2653 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2654 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2655 | // We must have a derived-to-base conversion. Check an |
| 2656 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2657 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2658 | From->getExprLoc(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2659 | From->getSourceRange(), &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2660 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2661 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2662 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2663 | // The conversion was successful. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2664 | Kind = CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2667 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2668 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2669 | if (const ObjCObjectPointerType *FromPtrType = |
| 2670 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2671 | // Objective-C++ conversions are always okay. |
| 2672 | // FIXME: We should have a different class of conversions for the |
| 2673 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2674 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2675 | return false; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2676 | } else if (FromType->isBlockPointerType()) { |
| 2677 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2678 | } else { |
| 2679 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2680 | } |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2681 | } else if (ToType->isBlockPointerType()) { |
| 2682 | if (!FromType->isBlockPointerType()) |
| 2683 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2684 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2685 | |
| 2686 | // We shouldn't fall into this case unless it's valid for other |
| 2687 | // reasons. |
| 2688 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2689 | Kind = CK_NullToPointer; |
| 2690 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2691 | return false; |
| 2692 | } |
| 2693 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2694 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2695 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2696 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2697 | /// If so, returns true and places the converted type (that might differ from |
| 2698 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2699 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2700 | QualType ToType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2701 | bool InOverloadResolution, |
| 2702 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2703 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2704 | if (!ToTypePtr) |
| 2705 | return false; |
| 2706 | |
| 2707 | // 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] | 2708 | if (From->isNullPointerConstant(Context, |
| 2709 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2710 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2711 | ConvertedType = ToType; |
| 2712 | return true; |
| 2713 | } |
| 2714 | |
| 2715 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2716 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2717 | if (!FromTypePtr) |
| 2718 | return false; |
| 2719 | |
| 2720 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2721 | // where D is derived from B (C++ 4.11p2). |
| 2722 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2723 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2725 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2726 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2727 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2728 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2729 | ToClass.getTypePtr()); |
| 2730 | return true; |
| 2731 | } |
| 2732 | |
| 2733 | return false; |
| 2734 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2736 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2737 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2738 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2739 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2740 | /// true and produces a diagnostic if there was an error, or returns false |
| 2741 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2742 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2743 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2744 | CXXCastPath &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2745 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2746 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2747 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2748 | if (!FromPtrType) { |
| 2749 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2750 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2751 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2752 | "Expr must be null pointer constant!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2753 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2754 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2755 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2756 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2757 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2758 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2759 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2760 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2761 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2762 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2763 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2764 | // FIXME: What about dependent types? |
| 2765 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2766 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2767 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2768 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2769 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2770 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2771 | assert(DerivationOkay && |
| 2772 | "Should not have been called if derivation isn't OK."); |
| 2773 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2774 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2775 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2776 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2777 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2778 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2779 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2780 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2781 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2782 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2783 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2784 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2785 | << FromClass << ToClass << QualType(VBase, 0) |
| 2786 | << From->getSourceRange(); |
| 2787 | return true; |
| 2788 | } |
| 2789 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2790 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2791 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2792 | Paths.front(), |
| 2793 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2794 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2795 | // Must be a base to derived member conversion. |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2796 | BuildBasePathArray(Paths, BasePath); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2797 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2798 | return false; |
| 2799 | } |
| 2800 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2801 | /// Determine whether the lifetime conversion between the two given |
| 2802 | /// qualifiers sets is nontrivial. |
| 2803 | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
| 2804 | Qualifiers ToQuals) { |
| 2805 | // Converting anything to const __unsafe_unretained is trivial. |
| 2806 | if (ToQuals.hasConst() && |
| 2807 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 2808 | return false; |
| 2809 | |
| 2810 | return true; |
| 2811 | } |
| 2812 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2813 | /// IsQualificationConversion - Determines whether the conversion from |
| 2814 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2815 | /// (C++ 4.4). |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2816 | /// |
| 2817 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2818 | /// when the qualification conversion involves a change in the Objective-C |
| 2819 | /// object lifetime. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | bool |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2821 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2822 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2823 | FromType = Context.getCanonicalType(FromType); |
| 2824 | ToType = Context.getCanonicalType(ToType); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2825 | ObjCLifetimeConversion = false; |
| 2826 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2827 | // If FromType and ToType are the same type, this is not a |
| 2828 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2829 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2830 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2832 | // (C++ 4.4p4): |
| 2833 | // A conversion can add cv-qualifiers at levels other than the first |
| 2834 | // in multi-level pointers, subject to the following rules: [...] |
| 2835 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2836 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2837 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2838 | // Within each iteration of the loop, we check the qualifiers to |
| 2839 | // determine if this still looks like a qualification |
| 2840 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2841 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2842 | // until there are no more pointers or pointers-to-members left to |
| 2843 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2844 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2846 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2847 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2848 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2849 | // Objective-C ARC: |
| 2850 | // Check Objective-C lifetime conversions. |
| 2851 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2852 | UnwrappedAnyPointer) { |
| 2853 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2854 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
| 2855 | ObjCLifetimeConversion = true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2856 | FromQuals.removeObjCLifetime(); |
| 2857 | ToQuals.removeObjCLifetime(); |
| 2858 | } else { |
| 2859 | // Qualification conversions cannot cast between different |
| 2860 | // Objective-C lifetime qualifiers. |
| 2861 | return false; |
| 2862 | } |
| 2863 | } |
| 2864 | |
Douglas Gregor | f30053d | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2865 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2866 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2867 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2868 | FromQuals.removeObjCGCAttr(); |
| 2869 | ToQuals.removeObjCGCAttr(); |
| 2870 | } |
| 2871 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2872 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2873 | // 2,j, and similarly for volatile. |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2874 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2875 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2877 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2878 | // every cv for 0 < k < j. |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2879 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2880 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2881 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2883 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2884 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | PreviousToQualsIncludeConst |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2886 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2887 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2888 | |
| 2889 | // We are left with FromType and ToType being the pointee types |
| 2890 | // after unwrapping the original FromType and ToType the same number |
| 2891 | // of types. If we unwrapped any pointers, and if FromType and |
| 2892 | // ToType have the same unqualified type (since we checked |
| 2893 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2894 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2897 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2898 | /// atomic type. |
| 2899 | /// |
| 2900 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2901 | /// sequence to finish the conversion. |
Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2902 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2903 | bool InOverloadResolution, |
| 2904 | StandardConversionSequence &SCS, |
| 2905 | bool CStyle) { |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2906 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2907 | if (!ToAtomic) |
| 2908 | return false; |
| 2909 | |
| 2910 | StandardConversionSequence InnerSCS; |
| 2911 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2912 | InOverloadResolution, InnerSCS, |
| 2913 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2914 | return false; |
| 2915 | |
| 2916 | SCS.Second = InnerSCS.Second; |
| 2917 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2918 | SCS.Third = InnerSCS.Third; |
| 2919 | SCS.QualificationIncludesObjCLifetime |
| 2920 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2921 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2922 | return true; |
| 2923 | } |
| 2924 | |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2925 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2926 | CXXConstructorDecl *Constructor, |
| 2927 | QualType Type) { |
| 2928 | const FunctionProtoType *CtorType = |
| 2929 | Constructor->getType()->getAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2930 | if (CtorType->getNumParams() > 0) { |
| 2931 | QualType FirstArg = CtorType->getParamType(0); |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2932 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2933 | return true; |
| 2934 | } |
| 2935 | return false; |
| 2936 | } |
| 2937 | |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2938 | static OverloadingResult |
| 2939 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2940 | CXXRecordDecl *To, |
| 2941 | UserDefinedConversionSequence &User, |
| 2942 | OverloadCandidateSet &CandidateSet, |
| 2943 | bool AllowExplicit) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2944 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2945 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2946 | Con != ConEnd; ++Con) { |
| 2947 | NamedDecl *D = *Con; |
| 2948 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2949 | |
| 2950 | // Find the constructor (which may be a template). |
| 2951 | CXXConstructorDecl *Constructor = 0; |
| 2952 | FunctionTemplateDecl *ConstructorTmpl |
| 2953 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2954 | if (ConstructorTmpl) |
| 2955 | Constructor |
| 2956 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2957 | else |
| 2958 | Constructor = cast<CXXConstructorDecl>(D); |
| 2959 | |
| 2960 | bool Usable = !Constructor->isInvalidDecl() && |
| 2961 | S.isInitListConstructor(Constructor) && |
| 2962 | (AllowExplicit || !Constructor->isExplicit()); |
| 2963 | if (Usable) { |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2964 | // If the first argument is (a reference to) the target type, |
| 2965 | // suppress conversions. |
| 2966 | bool SuppressUserConversions = |
| 2967 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2968 | if (ConstructorTmpl) |
| 2969 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2970 | /*ExplicitArgs*/ 0, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2971 | From, CandidateSet, |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2972 | SuppressUserConversions); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2973 | else |
| 2974 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2975 | From, CandidateSet, |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2976 | SuppressUserConversions); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2981 | |
| 2982 | OverloadCandidateSet::iterator Best; |
| 2983 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2984 | case OR_Success: { |
| 2985 | // Record the standard conversion we used and the conversion function. |
| 2986 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2987 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2988 | // Initializer lists don't have conversions as such. |
| 2989 | User.Before.setAsIdentityConversion(); |
| 2990 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2991 | User.ConversionFunction = Constructor; |
| 2992 | User.FoundConversionFunction = Best->FoundDecl; |
| 2993 | User.After.setAsIdentityConversion(); |
| 2994 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2995 | User.After.setAllToTypes(ToType); |
| 2996 | return OR_Success; |
| 2997 | } |
| 2998 | |
| 2999 | case OR_No_Viable_Function: |
| 3000 | return OR_No_Viable_Function; |
| 3001 | case OR_Deleted: |
| 3002 | return OR_Deleted; |
| 3003 | case OR_Ambiguous: |
| 3004 | return OR_Ambiguous; |
| 3005 | } |
| 3006 | |
| 3007 | llvm_unreachable("Invalid OverloadResult!"); |
| 3008 | } |
| 3009 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3010 | /// Determines whether there is a user-defined conversion sequence |
| 3011 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 3012 | /// ToType. If such a conversion exists, User will contain the |
| 3013 | /// user-defined conversion sequence that performs such a conversion |
| 3014 | /// and this routine will return true. Otherwise, this routine returns |
| 3015 | /// false and User is unspecified. |
| 3016 | /// |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3017 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 3018 | /// "explicit" conversion functions as well as non-explicit conversion |
| 3019 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3020 | /// |
| 3021 | /// \param AllowObjCConversionOnExplicit true if the conversion should |
| 3022 | /// allow an extra Objective-C pointer conversion on uses of explicit |
| 3023 | /// constructors. Requires \c AllowExplicit to also be set. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3024 | static OverloadingResult |
| 3025 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3026 | UserDefinedConversionSequence &User, |
| 3027 | OverloadCandidateSet &CandidateSet, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3028 | bool AllowExplicit, |
| 3029 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 2ee1d99 | 2013-11-08 01:20:25 +0000 | [diff] [blame] | 3030 | assert(AllowExplicit || !AllowObjCConversionOnExplicit); |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3031 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3032 | // Whether we will only visit constructors. |
| 3033 | bool ConstructorsOnly = false; |
| 3034 | |
| 3035 | // If the type we are conversion to is a class type, enumerate its |
| 3036 | // constructors. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3037 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3038 | // C++ [over.match.ctor]p1: |
| 3039 | // When objects of class type are direct-initialized (8.5), or |
| 3040 | // copy-initialized from an expression of the same or a |
| 3041 | // derived class type (8.5), overload resolution selects the |
| 3042 | // constructor. [...] For copy-initialization, the candidate |
| 3043 | // functions are all the converting constructors (12.3.1) of |
| 3044 | // that class. The argument list is the expression-list within |
| 3045 | // the parentheses of the initializer. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3046 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3047 | (From->getType()->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3048 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3049 | ConstructorsOnly = true; |
| 3050 | |
Benjamin Kramer | 90633e3 | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3051 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | 7a6f2a3 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3052 | // RequireCompleteType may have returned true due to some invalid decl |
| 3053 | // during template instantiation, but ToType may be complete enough now |
| 3054 | // to try to recover. |
| 3055 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3056 | // We're not going to find any constructors. |
| 3057 | } else if (CXXRecordDecl *ToRecordDecl |
| 3058 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3059 | |
| 3060 | Expr **Args = &From; |
| 3061 | unsigned NumArgs = 1; |
| 3062 | bool ListInitializing = false; |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3063 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3064 | // 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] | 3065 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3066 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3067 | if (Result != OR_No_Viable_Function) |
| 3068 | return Result; |
| 3069 | // Never mind. |
| 3070 | CandidateSet.clear(); |
| 3071 | |
| 3072 | // If we're list-initializing, we pass the individual elements as |
| 3073 | // arguments, not the entire list. |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3074 | Args = InitList->getInits(); |
| 3075 | NumArgs = InitList->getNumInits(); |
| 3076 | ListInitializing = true; |
| 3077 | } |
| 3078 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3079 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3080 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3081 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3082 | NamedDecl *D = *Con; |
| 3083 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3084 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3085 | // Find the constructor (which may be a template). |
| 3086 | CXXConstructorDecl *Constructor = 0; |
| 3087 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3088 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3089 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3091 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3092 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3093 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3094 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3095 | bool Usable = !Constructor->isInvalidDecl(); |
| 3096 | if (ListInitializing) |
| 3097 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3098 | else |
| 3099 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3100 | if (Usable) { |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3101 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3102 | if (SuppressUserConversions && ListInitializing) { |
| 3103 | SuppressUserConversions = false; |
| 3104 | if (NumArgs == 1) { |
| 3105 | // If the first argument is (a reference to) the target type, |
| 3106 | // suppress conversions. |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3107 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3108 | S.Context, Constructor, ToType); |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3109 | } |
| 3110 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3111 | if (ConstructorTmpl) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3112 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3113 | /*ExplicitArgs*/ 0, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3114 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3115 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3116 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3117 | // Allow one user-defined conversion when user specifies a |
| 3118 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3119 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3120 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3121 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3122 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3123 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3124 | } |
| 3125 | } |
| 3126 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3127 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3128 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3129 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3130 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3132 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3134 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3135 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3136 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3137 | CXXRecordDecl::conversion_iterator> |
| 3138 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3139 | for (CXXRecordDecl::conversion_iterator |
| 3140 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3141 | DeclAccessPair FoundDecl = I.getPair(); |
| 3142 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3143 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3144 | if (isa<UsingShadowDecl>(D)) |
| 3145 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3146 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3147 | CXXConversionDecl *Conv; |
| 3148 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3149 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3150 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3151 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3152 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3153 | |
| 3154 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3155 | if (ConvTemplate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3156 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3157 | ActingContext, From, ToType, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3158 | CandidateSet, |
| 3159 | AllowObjCConversionOnExplicit); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3160 | else |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3161 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3162 | From, ToType, CandidateSet, |
| 3163 | AllowObjCConversionOnExplicit); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3164 | } |
| 3165 | } |
| 3166 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3167 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3168 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3169 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3170 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3171 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3172 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3173 | case OR_Success: |
| 3174 | // Record the standard conversion we used and the conversion function. |
| 3175 | if (CXXConstructorDecl *Constructor |
| 3176 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3177 | // C++ [over.ics.user]p1: |
| 3178 | // If the user-defined conversion is specified by a |
| 3179 | // constructor (12.3.1), the initial standard conversion |
| 3180 | // sequence converts the source type to the type required by |
| 3181 | // the argument of the constructor. |
| 3182 | // |
| 3183 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3184 | if (isa<InitListExpr>(From)) { |
| 3185 | // Initializer lists don't have conversions as such. |
| 3186 | User.Before.setAsIdentityConversion(); |
| 3187 | } else { |
| 3188 | if (Best->Conversions[0].isEllipsis()) |
| 3189 | User.EllipsisConversion = true; |
| 3190 | else { |
| 3191 | User.Before = Best->Conversions[0].Standard; |
| 3192 | User.EllipsisConversion = false; |
| 3193 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3194 | } |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3195 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3196 | User.ConversionFunction = Constructor; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3197 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3198 | User.After.setAsIdentityConversion(); |
| 3199 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3200 | User.After.setAllToTypes(ToType); |
| 3201 | return OR_Success; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3202 | } |
| 3203 | if (CXXConversionDecl *Conversion |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3204 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3205 | // C++ [over.ics.user]p1: |
| 3206 | // |
| 3207 | // [...] If the user-defined conversion is specified by a |
| 3208 | // conversion function (12.3.2), the initial standard |
| 3209 | // conversion sequence converts the source type to the |
| 3210 | // implicit object parameter of the conversion function. |
| 3211 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3212 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3213 | User.ConversionFunction = Conversion; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3214 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3215 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3217 | // C++ [over.ics.user]p2: |
| 3218 | // The second standard conversion sequence converts the |
| 3219 | // result of the user-defined conversion to the target type |
| 3220 | // for the sequence. Since an implicit conversion sequence |
| 3221 | // is an initialization, the special rules for |
| 3222 | // initialization by user-defined conversion apply when |
| 3223 | // selecting the best user-defined conversion for a |
| 3224 | // user-defined conversion sequence (see 13.3.3 and |
| 3225 | // 13.3.3.1). |
| 3226 | User.After = Best->FinalConversion; |
| 3227 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3228 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3229 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3230 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3231 | case OR_No_Viable_Function: |
| 3232 | return OR_No_Viable_Function; |
| 3233 | case OR_Deleted: |
| 3234 | // No conversion here! We're done. |
| 3235 | return OR_Deleted; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3236 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3237 | case OR_Ambiguous: |
| 3238 | return OR_Ambiguous; |
| 3239 | } |
| 3240 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3241 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3242 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3243 | |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3244 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3245 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3246 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3247 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3248 | OverloadingResult OvResult = |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3249 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3250 | CandidateSet, false, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3251 | if (OvResult == OR_Ambiguous) |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3252 | Diag(From->getLocStart(), diag::err_typecheck_ambiguous_condition) |
| 3253 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3254 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3255 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3256 | diag::err_typecheck_nonviable_condition_incomplete, |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3257 | From->getType(), From->getSourceRange())) |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3258 | Diag(From->getLocStart(), diag::err_typecheck_nonviable_condition) |
| 3259 | << From->getType() << From->getSourceRange() << ToType; |
| 3260 | } else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3261 | return false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3262 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3263 | return true; |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3264 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3265 | |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3266 | /// \brief Compare the user-defined conversion functions or constructors |
| 3267 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3268 | /// is possible. |
| 3269 | static ImplicitConversionSequence::CompareKind |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3270 | compareConversionFunctions(Sema &S, FunctionDecl *Function1, |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3271 | FunctionDecl *Function2) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3272 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3273 | return ImplicitConversionSequence::Indistinguishable; |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3275 | // Objective-C++: |
| 3276 | // If both conversion functions are implicitly-declared conversions from |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3277 | // a lambda closure type to a function pointer and a block pointer, |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3278 | // respectively, always prefer the conversion to a function pointer, |
| 3279 | // because the function pointer is more lightweight and is more likely |
| 3280 | // to keep code working. |
| 3281 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3282 | if (!Conv1) |
| 3283 | return ImplicitConversionSequence::Indistinguishable; |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3285 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3286 | if (!Conv2) |
| 3287 | return ImplicitConversionSequence::Indistinguishable; |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3289 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3290 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3291 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3292 | if (Block1 != Block2) |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3293 | return Block1 ? ImplicitConversionSequence::Worse |
| 3294 | : ImplicitConversionSequence::Better; |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | return ImplicitConversionSequence::Indistinguishable; |
| 3298 | } |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3299 | |
| 3300 | static bool hasDeprecatedStringLiteralToCharPtrConversion( |
| 3301 | const ImplicitConversionSequence &ICS) { |
| 3302 | return (ICS.isStandard() && ICS.Standard.DeprecatedStringLiteralToCharPtr) || |
| 3303 | (ICS.isUserDefined() && |
| 3304 | ICS.UserDefined.Before.DeprecatedStringLiteralToCharPtr); |
| 3305 | } |
| 3306 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3307 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3308 | /// conversion sequences to determine whether one is better than the |
| 3309 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3310 | static ImplicitConversionSequence::CompareKind |
| 3311 | CompareImplicitConversionSequences(Sema &S, |
| 3312 | const ImplicitConversionSequence& ICS1, |
| 3313 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3314 | { |
| 3315 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3316 | // conversion sequences (as defined in 13.3.3.1) |
| 3317 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3318 | // conversion sequence than a user-defined conversion sequence or |
| 3319 | // an ellipsis conversion sequence, and |
| 3320 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3321 | // conversion sequence than an ellipsis conversion sequence |
| 3322 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3324 | // C++0x [over.best.ics]p10: |
| 3325 | // For the purpose of ranking implicit conversion sequences as |
| 3326 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3327 | // treated as a user-defined sequence that is indistinguishable |
| 3328 | // from any other user-defined conversion sequence. |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 3329 | |
| 3330 | // String literal to 'char *' conversion has been deprecated in C++03. It has |
| 3331 | // been removed from C++11. We still accept this conversion, if it happens at |
| 3332 | // the best viable function. Otherwise, this conversion is considered worse |
| 3333 | // than ellipsis conversion. Consider this as an extension; this is not in the |
| 3334 | // standard. For example: |
| 3335 | // |
| 3336 | // int &f(...); // #1 |
| 3337 | // void f(char*); // #2 |
| 3338 | // void g() { int &r = f("foo"); } |
| 3339 | // |
| 3340 | // In C++03, we pick #2 as the best viable function. |
| 3341 | // In C++11, we pick #1 as the best viable function, because ellipsis |
| 3342 | // conversion is better than string-literal to char* conversion (since there |
| 3343 | // is no such conversion in C++11). If there was no #1 at all or #1 couldn't |
| 3344 | // convert arguments, #2 would be the best viable function in C++11. |
| 3345 | // If the best viable function has this conversion, a warning will be issued |
| 3346 | // in C++03, or an ExtWarn (+SFINAE failure) will be issued in C++11. |
| 3347 | |
| 3348 | if (S.getLangOpts().CPlusPlus11 && !S.getLangOpts().WritableStrings && |
| 3349 | hasDeprecatedStringLiteralToCharPtrConversion(ICS1) != |
| 3350 | hasDeprecatedStringLiteralToCharPtrConversion(ICS2)) |
| 3351 | return hasDeprecatedStringLiteralToCharPtrConversion(ICS1) |
| 3352 | ? ImplicitConversionSequence::Worse |
| 3353 | : ImplicitConversionSequence::Better; |
| 3354 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3355 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3356 | return ImplicitConversionSequence::Better; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3357 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3358 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3359 | |
Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3360 | // The following checks require both conversion sequences to be of |
| 3361 | // the same kind. |
| 3362 | if (ICS1.getKind() != ICS2.getKind()) |
| 3363 | return ImplicitConversionSequence::Indistinguishable; |
| 3364 | |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3365 | ImplicitConversionSequence::CompareKind Result = |
| 3366 | ImplicitConversionSequence::Indistinguishable; |
| 3367 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3368 | // Two implicit conversion sequences of the same form are |
| 3369 | // indistinguishable conversion sequences unless one of the |
| 3370 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3371 | if (ICS1.isStandard()) |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3372 | Result = CompareStandardConversionSequences(S, |
| 3373 | ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3374 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3375 | // User-defined conversion sequence U1 is a better conversion |
| 3376 | // sequence than another user-defined conversion sequence U2 if |
| 3377 | // they contain the same user-defined conversion function or |
| 3378 | // constructor and if the second standard conversion sequence of |
| 3379 | // U1 is better than the second standard conversion sequence of |
| 3380 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3382 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3383 | Result = CompareStandardConversionSequences(S, |
| 3384 | ICS1.UserDefined.After, |
| 3385 | ICS2.UserDefined.After); |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3386 | else |
| 3387 | Result = compareConversionFunctions(S, |
| 3388 | ICS1.UserDefined.ConversionFunction, |
| 3389 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3392 | // List-initialization sequence L1 is a better conversion sequence than |
| 3393 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3394 | // for some X and L2 does not. |
| 3395 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 3396 | !ICS1.isBad()) { |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3397 | if (ICS1.isStdInitializerListElement() && |
| 3398 | !ICS2.isStdInitializerListElement()) |
| 3399 | return ImplicitConversionSequence::Better; |
| 3400 | if (!ICS1.isStdInitializerListElement() && |
| 3401 | ICS2.isStdInitializerListElement()) |
| 3402 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3408 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3409 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3410 | Qualifiers Quals; |
| 3411 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3412 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3413 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3414 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3415 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3416 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3418 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3419 | // determine if one is a proper subset of the other. |
| 3420 | static ImplicitConversionSequence::CompareKind |
| 3421 | compareStandardConversionSubsets(ASTContext &Context, |
| 3422 | const StandardConversionSequence& SCS1, |
| 3423 | const StandardConversionSequence& SCS2) { |
| 3424 | ImplicitConversionSequence::CompareKind Result |
| 3425 | = ImplicitConversionSequence::Indistinguishable; |
| 3426 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | e87561a | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3428 | // any non-identity conversion sequence |
Douglas Gregor | 377c109 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3429 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3430 | return ImplicitConversionSequence::Better; |
| 3431 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3432 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3434 | if (SCS1.Second != SCS2.Second) { |
| 3435 | if (SCS1.Second == ICK_Identity) |
| 3436 | Result = ImplicitConversionSequence::Better; |
| 3437 | else if (SCS2.Second == ICK_Identity) |
| 3438 | Result = ImplicitConversionSequence::Worse; |
| 3439 | else |
| 3440 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3441 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3442 | return ImplicitConversionSequence::Indistinguishable; |
| 3443 | |
| 3444 | if (SCS1.Third == SCS2.Third) { |
| 3445 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3446 | : ImplicitConversionSequence::Indistinguishable; |
| 3447 | } |
| 3448 | |
| 3449 | if (SCS1.Third == ICK_Identity) |
| 3450 | return Result == ImplicitConversionSequence::Worse |
| 3451 | ? ImplicitConversionSequence::Indistinguishable |
| 3452 | : ImplicitConversionSequence::Better; |
| 3453 | |
| 3454 | if (SCS2.Third == ICK_Identity) |
| 3455 | return Result == ImplicitConversionSequence::Better |
| 3456 | ? ImplicitConversionSequence::Indistinguishable |
| 3457 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3459 | return ImplicitConversionSequence::Indistinguishable; |
| 3460 | } |
| 3461 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3462 | /// \brief Determine whether one of the given reference bindings is better |
| 3463 | /// than the other based on what kind of bindings they are. |
| 3464 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3465 | const StandardConversionSequence &SCS2) { |
| 3466 | // C++0x [over.ics.rank]p3b4: |
| 3467 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3468 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3469 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3470 | // 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] | 3471 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3472 | // reference*. |
| 3473 | // |
| 3474 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3475 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3476 | // time of this writing) break the standard definition of std::forward |
| 3477 | // and std::reference_wrapper when dealing with references to functions. |
| 3478 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3479 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3480 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3481 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3482 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3483 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3484 | SCS2.IsLvalueReference) || |
| 3485 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3486 | !SCS2.IsLvalueReference); |
| 3487 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3489 | /// CompareStandardConversionSequences - Compare two standard |
| 3490 | /// conversion sequences to determine whether one is better than the |
| 3491 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3492 | static ImplicitConversionSequence::CompareKind |
| 3493 | CompareStandardConversionSequences(Sema &S, |
| 3494 | const StandardConversionSequence& SCS1, |
| 3495 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3496 | { |
| 3497 | // Standard conversion sequence S1 is a better conversion sequence |
| 3498 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3499 | |
| 3500 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3501 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3502 | // excluding any Lvalue Transformation; the identity conversion |
| 3503 | // sequence is considered to be a subsequence of any |
| 3504 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3505 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3506 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3507 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3508 | |
| 3509 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3510 | // defined below), or, if not that, |
| 3511 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3512 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3513 | if (Rank1 < Rank2) |
| 3514 | return ImplicitConversionSequence::Better; |
| 3515 | else if (Rank2 < Rank1) |
| 3516 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3517 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3518 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3519 | // are indistinguishable unless one of the following rules |
| 3520 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3522 | // A conversion that is not a conversion of a pointer, or |
| 3523 | // pointer to member, to bool is better than another conversion |
| 3524 | // that is such a conversion. |
| 3525 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3526 | return SCS2.isPointerConversionToBool() |
| 3527 | ? ImplicitConversionSequence::Better |
| 3528 | : ImplicitConversionSequence::Worse; |
| 3529 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3530 | // C++ [over.ics.rank]p4b2: |
| 3531 | // |
| 3532 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3533 | // conversion of B* to A* is better than conversion of B* to |
| 3534 | // void*, and conversion of A* to void* is better than conversion |
| 3535 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | bool SCS1ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3537 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3538 | bool SCS2ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3539 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3540 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3541 | // Exactly one of the conversion sequences is a conversion to |
| 3542 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3543 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3544 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3545 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3546 | // Neither conversion sequence converts to a void pointer; compare |
| 3547 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3548 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3549 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3550 | return DerivedCK; |
Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3551 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3552 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3553 | // Both conversion sequences are conversions to void |
| 3554 | // pointers. Compare the source types to determine if there's an |
| 3555 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3556 | QualType FromType1 = SCS1.getFromType(); |
| 3557 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3558 | |
| 3559 | // Adjust the types we're converting from via the array-to-pointer |
| 3560 | // conversion, if we need to. |
| 3561 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3562 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3563 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3564 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3565 | |
Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3566 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3567 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3568 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3569 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3570 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3571 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3572 | return ImplicitConversionSequence::Worse; |
| 3573 | |
| 3574 | // Objective-C++: If one interface is more specific than the |
| 3575 | // other, it is the better one. |
Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3576 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3577 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3578 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3579 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3580 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3581 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3582 | FromObjCPtr2); |
| 3583 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3584 | FromObjCPtr1); |
| 3585 | if (AssignLeft != AssignRight) { |
| 3586 | return AssignLeft? ImplicitConversionSequence::Better |
| 3587 | : ImplicitConversionSequence::Worse; |
| 3588 | } |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3589 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3590 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3591 | |
| 3592 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3593 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3595 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3596 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3597 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3598 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3599 | // Check for a better reference binding based on the kind of bindings. |
| 3600 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3601 | return ImplicitConversionSequence::Better; |
| 3602 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3603 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3604 | |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3605 | // C++ [over.ics.rank]p3b4: |
| 3606 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3607 | // which the references refer are the same type except for |
| 3608 | // top-level cv-qualifiers, and the type to which the reference |
| 3609 | // initialized by S2 refers is more cv-qualified than the type |
| 3610 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3611 | QualType T1 = SCS1.getToType(2); |
| 3612 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3613 | T1 = S.Context.getCanonicalType(T1); |
| 3614 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3615 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3616 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3617 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3618 | if (UnqualT1 == UnqualT2) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3619 | // Objective-C++ ARC: If the references refer to objects with different |
| 3620 | // lifetimes, prefer bindings that don't change lifetime. |
| 3621 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3622 | SCS2.ObjCLifetimeConversionBinding) { |
| 3623 | return SCS1.ObjCLifetimeConversionBinding |
| 3624 | ? ImplicitConversionSequence::Worse |
| 3625 | : ImplicitConversionSequence::Better; |
| 3626 | } |
| 3627 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3628 | // If the type is an array type, promote the element qualifiers to the |
| 3629 | // type for comparison. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3630 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3631 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3632 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3633 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3634 | if (T2.isMoreQualifiedThan(T1)) |
| 3635 | return ImplicitConversionSequence::Better; |
| 3636 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3637 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3638 | } |
| 3639 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3640 | |
Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3641 | // In Microsoft mode, prefer an integral conversion to a |
| 3642 | // floating-to-integral conversion if the integral conversion |
| 3643 | // is between types of the same size. |
| 3644 | // For example: |
| 3645 | // void f(float); |
| 3646 | // void f(int); |
| 3647 | // int main { |
| 3648 | // long a; |
| 3649 | // f(a); |
| 3650 | // } |
| 3651 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3652 | // as clang will do in standard mode. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 3653 | if (S.getLangOpts().MSVCCompat && SCS1.Second == ICK_Integral_Conversion && |
| 3654 | SCS2.Second == ICK_Floating_Integral && |
Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3655 | S.Context.getTypeSize(SCS1.getFromType()) == |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 3656 | S.Context.getTypeSize(SCS1.getToType(2))) |
Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3657 | return ImplicitConversionSequence::Better; |
| 3658 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3659 | return ImplicitConversionSequence::Indistinguishable; |
| 3660 | } |
| 3661 | |
| 3662 | /// CompareQualificationConversions - Compares two standard conversion |
| 3663 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3665 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3666 | CompareQualificationConversions(Sema &S, |
| 3667 | const StandardConversionSequence& SCS1, |
| 3668 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3669 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3670 | // -- S1 and S2 differ only in their qualification conversion and |
| 3671 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3672 | // cv-qualification signature of type T1 is a proper subset of |
| 3673 | // the cv-qualification signature of type T2, and S1 is not the |
| 3674 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3675 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3676 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3677 | return ImplicitConversionSequence::Indistinguishable; |
| 3678 | |
| 3679 | // FIXME: the example in the standard doesn't use a qualification |
| 3680 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3681 | QualType T1 = SCS1.getToType(2); |
| 3682 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3683 | T1 = S.Context.getCanonicalType(T1); |
| 3684 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3685 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3686 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3687 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3688 | |
| 3689 | // If the types are the same, we won't learn anything by unwrapped |
| 3690 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3691 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3692 | return ImplicitConversionSequence::Indistinguishable; |
| 3693 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3694 | // If the type is an array type, promote the element qualifiers to the type |
| 3695 | // for comparison. |
| 3696 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3697 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3698 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3699 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3700 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3702 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3703 | |
| 3704 | // Objective-C++ ARC: |
| 3705 | // Prefer qualification conversions not involving a change in lifetime |
| 3706 | // to qualification conversions that do not change lifetime. |
| 3707 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3708 | SCS2.QualificationIncludesObjCLifetime) { |
| 3709 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3710 | ? ImplicitConversionSequence::Worse |
| 3711 | : ImplicitConversionSequence::Better; |
| 3712 | } |
| 3713 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3714 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3715 | // Within each iteration of the loop, we check the qualifiers to |
| 3716 | // determine if this still looks like a qualification |
| 3717 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3718 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3719 | // until there are no more pointers or pointers-to-members left |
| 3720 | // to unwrap. This essentially mimics what |
| 3721 | // IsQualificationConversion does, but here we're checking for a |
| 3722 | // strict subset of qualifiers. |
| 3723 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3724 | // The qualifiers are the same, so this doesn't tell us anything |
| 3725 | // about how the sequences rank. |
| 3726 | ; |
| 3727 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3728 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3729 | if (Result == ImplicitConversionSequence::Worse) |
| 3730 | // Neither has qualifiers that are a subset of the other's |
| 3731 | // qualifiers. |
| 3732 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3734 | Result = ImplicitConversionSequence::Better; |
| 3735 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3736 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3737 | if (Result == ImplicitConversionSequence::Better) |
| 3738 | // Neither has qualifiers that are a subset of the other's |
| 3739 | // qualifiers. |
| 3740 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3742 | Result = ImplicitConversionSequence::Worse; |
| 3743 | } else { |
| 3744 | // Qualifiers are disjoint. |
| 3745 | return ImplicitConversionSequence::Indistinguishable; |
| 3746 | } |
| 3747 | |
| 3748 | // If the types after this point are equivalent, we're done. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3749 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3750 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3753 | // Check that the winning standard conversion sequence isn't using |
| 3754 | // the deprecated string literal array to pointer conversion. |
| 3755 | switch (Result) { |
| 3756 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3757 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3758 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3759 | break; |
| 3760 | |
| 3761 | case ImplicitConversionSequence::Indistinguishable: |
| 3762 | break; |
| 3763 | |
| 3764 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3765 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3766 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3767 | break; |
| 3768 | } |
| 3769 | |
| 3770 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3773 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3774 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3775 | /// various kinds of derived-to-base conversions (C++ |
| 3776 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3777 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3778 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3779 | CompareDerivedToBaseConversions(Sema &S, |
| 3780 | const StandardConversionSequence& SCS1, |
| 3781 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3782 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3783 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3784 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3785 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3786 | |
| 3787 | // Adjust the types we're converting from via the array-to-pointer |
| 3788 | // conversion, if we need to. |
| 3789 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3790 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3791 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3792 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Canonicalize all of the types. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3795 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3796 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3797 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3798 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3799 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3800 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3801 | // |
| 3802 | // If class B is derived directly or indirectly from class A and |
| 3803 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3804 | // |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3805 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3807 | SCS2.Second == ICK_Pointer_Conversion && |
| 3808 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3809 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3810 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3812 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3814 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3815 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3816 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3817 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3818 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3819 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3820 | // -- 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] | 3821 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3822 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3823 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3824 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3825 | return ImplicitConversionSequence::Worse; |
| 3826 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3827 | |
| 3828 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3829 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3830 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3831 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3832 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3833 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3834 | } |
| 3835 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3836 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3837 | const ObjCObjectPointerType *FromPtr1 |
| 3838 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3839 | const ObjCObjectPointerType *FromPtr2 |
| 3840 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3841 | const ObjCObjectPointerType *ToPtr1 |
| 3842 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3843 | const ObjCObjectPointerType *ToPtr2 |
| 3844 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3845 | |
| 3846 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3847 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3848 | // that we do for C++ pointers to class types. However, we employ the |
| 3849 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3850 | // Objective-C pointer types. |
| 3851 | bool FromAssignLeft |
| 3852 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3853 | bool FromAssignRight |
| 3854 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3855 | bool ToAssignLeft |
| 3856 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3857 | bool ToAssignRight |
| 3858 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3859 | |
| 3860 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3861 | // type is better than a conversion to 'id'. |
| 3862 | if (ToPtr1->isObjCIdType() && |
| 3863 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3864 | return ImplicitConversionSequence::Worse; |
| 3865 | if (ToPtr2->isObjCIdType() && |
| 3866 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3867 | return ImplicitConversionSequence::Better; |
| 3868 | |
| 3869 | // A conversion to a non-id object pointer type is better than a |
| 3870 | // conversion to a qualified 'id' type |
| 3871 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3872 | return ImplicitConversionSequence::Worse; |
| 3873 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3874 | return ImplicitConversionSequence::Better; |
| 3875 | |
| 3876 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3877 | // type is better than a conversion to 'Class'. |
| 3878 | if (ToPtr1->isObjCClassType() && |
| 3879 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3880 | return ImplicitConversionSequence::Worse; |
| 3881 | if (ToPtr2->isObjCClassType() && |
| 3882 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3883 | return ImplicitConversionSequence::Better; |
| 3884 | |
| 3885 | // A conversion to a non-Class object pointer type is better than a |
| 3886 | // conversion to a qualified 'Class' type. |
| 3887 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3888 | return ImplicitConversionSequence::Worse; |
| 3889 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3890 | return ImplicitConversionSequence::Better; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3892 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3893 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3894 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3895 | (ToAssignLeft != ToAssignRight)) |
| 3896 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3897 | : ImplicitConversionSequence::Better; |
| 3898 | |
| 3899 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3900 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3901 | (FromAssignLeft != FromAssignRight)) |
| 3902 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3903 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3904 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3905 | } |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3906 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3907 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3908 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3909 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3910 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3912 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3914 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3916 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3918 | ToType2->getAs<MemberPointerType>(); |
| 3919 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3920 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3921 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3922 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3923 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3924 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3925 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3926 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3927 | // 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] | 3928 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3929 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3930 | return ImplicitConversionSequence::Worse; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3931 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3932 | return ImplicitConversionSequence::Better; |
| 3933 | } |
| 3934 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3935 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3936 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3937 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3938 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3939 | return ImplicitConversionSequence::Worse; |
| 3940 | } |
| 3941 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3943 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3944 | // -- 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] | 3945 | // -- binding of an expression of type C to a reference of type |
| 3946 | // B& is better than binding an expression of type C to a |
| 3947 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3948 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3949 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3950 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3951 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3952 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3953 | return ImplicitConversionSequence::Worse; |
| 3954 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3955 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3956 | // -- 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] | 3957 | // -- binding of an expression of type B to a reference of type |
| 3958 | // A& is better than binding an expression of type C to a |
| 3959 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3960 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3961 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3962 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3963 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3964 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3965 | return ImplicitConversionSequence::Worse; |
| 3966 | } |
| 3967 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3968 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3969 | return ImplicitConversionSequence::Indistinguishable; |
| 3970 | } |
| 3971 | |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3972 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3973 | /// C++ class. |
| 3974 | static bool isTypeValid(QualType T) { |
| 3975 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3976 | return !Record->isInvalidDecl(); |
| 3977 | |
| 3978 | return true; |
| 3979 | } |
| 3980 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3981 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3982 | /// determine whether they are reference-related, |
| 3983 | /// reference-compatible, reference-compatible with added |
| 3984 | /// qualification, or incompatible, for use in C++ initialization by |
| 3985 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3986 | /// type, and the first type (T1) is the pointee type of the reference |
| 3987 | /// type being initialized. |
| 3988 | Sema::ReferenceCompareResult |
| 3989 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3990 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3991 | bool &DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3992 | bool &ObjCConversion, |
| 3993 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3994 | assert(!OrigT1->isReferenceType() && |
| 3995 | "T1 must be the pointee type of the reference type"); |
| 3996 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3997 | |
| 3998 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3999 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 4000 | Qualifiers T1Quals, T2Quals; |
| 4001 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 4002 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 4003 | |
| 4004 | // C++ [dcl.init.ref]p4: |
| 4005 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 4006 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 4007 | // T1 is a base class of T2. |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4008 | DerivedToBase = false; |
| 4009 | ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4010 | ObjCLifetimeConversion = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4011 | if (UnqualT1 == UnqualT2) { |
| 4012 | // Nothing to do. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4013 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 4014 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 4015 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4016 | DerivedToBase = true; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4017 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 4018 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 4019 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 4020 | ObjCConversion = true; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4021 | else |
| 4022 | return Ref_Incompatible; |
| 4023 | |
| 4024 | // At this point, we know that T1 and T2 are reference-related (at |
| 4025 | // least). |
| 4026 | |
| 4027 | // If the type is an array type, promote the element qualifiers to the type |
| 4028 | // for comparison. |
| 4029 | if (isa<ArrayType>(T1) && T1Quals) |
| 4030 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 4031 | if (isa<ArrayType>(T2) && T2Quals) |
| 4032 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 4033 | |
| 4034 | // C++ [dcl.init.ref]p4: |
| 4035 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 4036 | // reference-related to T2 and cv1 is the same cv-qualification |
| 4037 | // as, or greater cv-qualification than, cv2. For purposes of |
| 4038 | // overload resolution, cases for which cv1 is greater |
| 4039 | // cv-qualification than cv2 are identified as |
| 4040 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | d517d55 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4041 | // |
| 4042 | // Note that we also require equivalence of Objective-C GC and address-space |
| 4043 | // qualifiers when performing these computations, so that e.g., an int in |
| 4044 | // address space 1 is not reference-compatible with an int in address |
| 4045 | // space 2. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4046 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 4047 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 4048 | if (isNonTrivialObjCLifetimeConversion(T2Quals, T1Quals)) |
| 4049 | ObjCLifetimeConversion = true; |
| 4050 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4051 | T1Quals.removeObjCLifetime(); |
| 4052 | T2Quals.removeObjCLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
Douglas Gregor | d517d55 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4055 | if (T1Quals == T2Quals) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4056 | return Ref_Compatible; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4057 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4058 | return Ref_Compatible_With_Added_Qualification; |
| 4059 | else |
| 4060 | return Ref_Related; |
| 4061 | } |
| 4062 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4063 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4064 | /// with DeclType. Return true if something definite is found. |
| 4065 | static bool |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4066 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 4067 | QualType DeclType, SourceLocation DeclLoc, |
| 4068 | Expr *Init, QualType T2, bool AllowRvalues, |
| 4069 | bool AllowExplicit) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4070 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4071 | CXXRecordDecl *T2RecordDecl |
| 4072 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4073 | |
| 4074 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4075 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4076 | CXXRecordDecl::conversion_iterator> |
| 4077 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4078 | for (CXXRecordDecl::conversion_iterator |
| 4079 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4080 | NamedDecl *D = *I; |
| 4081 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4082 | if (isa<UsingShadowDecl>(D)) |
| 4083 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4084 | |
| 4085 | FunctionTemplateDecl *ConvTemplate |
| 4086 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4087 | CXXConversionDecl *Conv; |
| 4088 | if (ConvTemplate) |
| 4089 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4090 | else |
| 4091 | Conv = cast<CXXConversionDecl>(D); |
| 4092 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4093 | // 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] | 4094 | // explicit conversions, skip it. |
| 4095 | if (!AllowExplicit && Conv->isExplicit()) |
| 4096 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4098 | if (AllowRvalues) { |
| 4099 | bool DerivedToBase = false; |
| 4100 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4101 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4102 | |
| 4103 | // If we are initializing an rvalue reference, don't permit conversion |
| 4104 | // functions that return lvalues. |
| 4105 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4106 | const ReferenceType *RefType |
| 4107 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4108 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4109 | continue; |
| 4110 | } |
| 4111 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4112 | if (!ConvTemplate && |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4113 | S.CompareReferenceRelationship( |
| 4114 | DeclLoc, |
| 4115 | Conv->getConversionType().getNonReferenceType() |
| 4116 | .getUnqualifiedType(), |
| 4117 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4118 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4119 | Sema::Ref_Incompatible) |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4120 | continue; |
| 4121 | } else { |
| 4122 | // If the conversion function doesn't return a reference type, |
| 4123 | // it can't be considered for this conversion. An rvalue reference |
| 4124 | // is only acceptable if its referencee is a function type. |
| 4125 | |
| 4126 | const ReferenceType *RefType = |
| 4127 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4128 | if (!RefType || |
| 4129 | (!RefType->isLValueReferenceType() && |
| 4130 | !RefType->getPointeeType()->isFunctionType())) |
| 4131 | continue; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4132 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4134 | if (ConvTemplate) |
| 4135 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4136 | Init, DeclType, CandidateSet, |
| 4137 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4138 | else |
| 4139 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4140 | DeclType, CandidateSet, |
| 4141 | /*AllowObjCConversionOnExplicit=*/false); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4144 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4145 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4146 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4147 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4148 | case OR_Success: |
| 4149 | // C++ [over.ics.ref]p1: |
| 4150 | // |
| 4151 | // [...] If the parameter binds directly to the result of |
| 4152 | // applying a conversion function to the argument |
| 4153 | // expression, the implicit conversion sequence is a |
| 4154 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4155 | // second standard conversion sequence either an identity |
| 4156 | // conversion or, if the conversion function returns an |
| 4157 | // entity of a type that is a derived class of the parameter |
| 4158 | // type, a derived-to-base Conversion. |
| 4159 | if (!Best->FinalConversion.DirectBinding) |
| 4160 | return false; |
| 4161 | |
| 4162 | ICS.setUserDefined(); |
| 4163 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4164 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4165 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4166 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4167 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4168 | ICS.UserDefined.EllipsisConversion = false; |
| 4169 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4170 | ICS.UserDefined.After.DirectBinding && |
| 4171 | "Expected a direct reference binding!"); |
| 4172 | return true; |
| 4173 | |
| 4174 | case OR_Ambiguous: |
| 4175 | ICS.setAmbiguous(); |
| 4176 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4177 | Cand != CandidateSet.end(); ++Cand) |
| 4178 | if (Cand->Viable) |
| 4179 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4180 | return true; |
| 4181 | |
| 4182 | case OR_No_Viable_Function: |
| 4183 | case OR_Deleted: |
| 4184 | // There was no suitable conversion, or we found a deleted |
| 4185 | // conversion; continue with other checks. |
| 4186 | return false; |
| 4187 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4188 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4189 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4192 | /// \brief Compute an implicit conversion sequence for reference |
| 4193 | /// initialization. |
| 4194 | static ImplicitConversionSequence |
Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4195 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4196 | SourceLocation DeclLoc, |
| 4197 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4198 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4199 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4200 | |
| 4201 | // Most paths end in a failed conversion. |
| 4202 | ImplicitConversionSequence ICS; |
| 4203 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4204 | |
| 4205 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4206 | QualType T2 = Init->getType(); |
| 4207 | |
| 4208 | // If the initializer is the address of an overloaded function, try |
| 4209 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4210 | // type of the resulting function. |
| 4211 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4212 | DeclAccessPair Found; |
| 4213 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4214 | false, Found)) |
| 4215 | T2 = Fn->getType(); |
| 4216 | } |
| 4217 | |
| 4218 | // Compute some basic properties of the types and the initializer. |
| 4219 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4220 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4221 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4222 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4223 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4224 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4225 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4226 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4227 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4228 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4229 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4230 | // A reference to type "cv1 T1" is initialized by an expression |
| 4231 | // of type "cv2 T2" as follows: |
| 4232 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4233 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4234 | if (!isRValRef) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4235 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4236 | // reference-compatible with "cv2 T2," or |
| 4237 | // |
| 4238 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4239 | if (InitCategory.isLValue() && |
| 4240 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4241 | // C++ [over.ics.ref]p1: |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4242 | // When a parameter of reference type binds directly (8.5.3) |
| 4243 | // to an argument expression, the implicit conversion sequence |
| 4244 | // is the identity conversion, unless the argument expression |
| 4245 | // has a type that is a derived class of the parameter type, |
| 4246 | // in which case the implicit conversion sequence is a |
| 4247 | // derived-to-base Conversion (13.3.3.1). |
| 4248 | ICS.setStandard(); |
| 4249 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4250 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4251 | : ObjCConversion? ICK_Compatible_Conversion |
| 4252 | : ICK_Identity; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4253 | ICS.Standard.Third = ICK_Identity; |
| 4254 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4255 | ICS.Standard.setToType(0, T2); |
| 4256 | ICS.Standard.setToType(1, T1); |
| 4257 | ICS.Standard.setToType(2, T1); |
| 4258 | ICS.Standard.ReferenceBinding = true; |
| 4259 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4260 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4261 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4262 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4263 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4264 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4265 | ICS.Standard.CopyConstructor = 0; |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4266 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4267 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4268 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4269 | // derived-to-base conversions is suppressed when we're |
| 4270 | // computing the implicit conversion sequence (C++ |
| 4271 | // [over.best.ics]p2). |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4272 | return ICS; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4273 | } |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4274 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4275 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4276 | // not reference-related to T2, and can be implicitly |
| 4277 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4278 | // is reference-compatible with "cv3 T3" 92) (this |
| 4279 | // conversion is selected by enumerating the applicable |
| 4280 | // conversion functions (13.3.1.6) and choosing the best |
| 4281 | // one through overload resolution (13.3)), |
| 4282 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4284 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4285 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4286 | Init, T2, /*AllowRvalues=*/false, |
| 4287 | AllowExplicit)) |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4288 | return ICS; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4292 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4293 | // 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] | 4294 | // shall be an rvalue reference. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | // |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4296 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4297 | // point, which is that, due to p2 (which short-circuits reference |
| 4298 | // binding by only attempting a simple conversion for non-direct |
| 4299 | // bindings) and p3's strange wording, we allow a const volatile |
| 4300 | // reference to bind to an rvalue. Hence the check for the presence |
| 4301 | // of "const" rather than checking for "const" being the only |
| 4302 | // qualifier. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4303 | // This is also the point where rvalue references and lvalue inits no longer |
| 4304 | // go together. |
Richard Smith | ce4f608 | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4305 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4306 | return ICS; |
| 4307 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4308 | // -- If the initializer expression |
| 4309 | // |
| 4310 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4311 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4312 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4313 | (InitCategory.isXValue() || |
| 4314 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4315 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4316 | ICS.setStandard(); |
| 4317 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4319 | : ObjCConversion? ICK_Compatible_Conversion |
| 4320 | : ICK_Identity; |
| 4321 | ICS.Standard.Third = ICK_Identity; |
| 4322 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4323 | ICS.Standard.setToType(0, T2); |
| 4324 | ICS.Standard.setToType(1, T1); |
| 4325 | ICS.Standard.setToType(2, T1); |
| 4326 | ICS.Standard.ReferenceBinding = true; |
| 4327 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4328 | // binding unless we're binding to a class prvalue. |
| 4329 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4330 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4331 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4332 | ICS.Standard.DirectBinding = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4333 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4334 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4335 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4336 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4338 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4339 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4340 | ICS.Standard.CopyConstructor = 0; |
Ismail Pazarbasi | 1121de3 | 2014-01-17 21:08:52 +0000 | [diff] [blame] | 4341 | ICS.Standard.DeprecatedStringLiteralToCharPtr = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | return ICS; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4343 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4345 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4346 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4347 | // an xvalue, class prvalue, or function lvalue of type |
| 4348 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4349 | // "cv3 T3", |
| 4350 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4351 | // then the reference is bound to the value of the initializer |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4352 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4354 | // class subobject). |
| 4355 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4356 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4357 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4358 | Init, T2, /*AllowRvalues=*/true, |
| 4359 | AllowExplicit)) { |
| 4360 | // In the second case, if the reference is an rvalue reference |
| 4361 | // and the second standard conversion sequence of the |
| 4362 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4363 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4364 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4365 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4366 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4367 | |
Douglas Gregor | 95273c3 | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4368 | return ICS; |
Rafael Espindola | be468d9 | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4369 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4371 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4372 | // initialized from the initializer expression using the |
| 4373 | // rules for a non-reference copy initialization (8.5). The |
| 4374 | // reference is then bound to the temporary. If T1 is |
| 4375 | // reference-related to T2, cv1 must be the same |
| 4376 | // cv-qualification as, or greater cv-qualification than, |
| 4377 | // cv2; otherwise, the program is ill-formed. |
| 4378 | if (RefRelationship == Sema::Ref_Related) { |
| 4379 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4380 | // we would be reference-compatible or reference-compatible with |
| 4381 | // added qualification. But that wasn't the case, so the reference |
| 4382 | // initialization fails. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4383 | // |
| 4384 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4385 | // ObjC GC and lifetime qualifiers aren't important. |
| 4386 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4387 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4388 | T1Quals.removeObjCGCAttr(); |
| 4389 | T1Quals.removeObjCLifetime(); |
| 4390 | T2Quals.removeObjCGCAttr(); |
| 4391 | T2Quals.removeObjCLifetime(); |
| 4392 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4393 | return ICS; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | // If at least one of the types is a class type, the types are not |
| 4397 | // related, and we aren't allowed any user conversions, the |
| 4398 | // reference binding fails. This case is important for breaking |
| 4399 | // recursion, since TryImplicitConversion below will attempt to |
| 4400 | // create a temporary through the use of a copy constructor. |
| 4401 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4402 | (T1->isRecordType() || T2->isRecordType())) |
| 4403 | return ICS; |
| 4404 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4405 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4406 | // reference, the initializer expression shall not be an lvalue. |
| 4407 | if (RefRelationship >= Sema::Ref_Related && |
| 4408 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4409 | return ICS; |
| 4410 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4411 | // C++ [over.ics.ref]p2: |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4412 | // When a parameter of reference type is not bound directly to |
| 4413 | // an argument expression, the conversion sequence is the one |
| 4414 | // required to convert the argument expression to the |
| 4415 | // underlying type of the reference according to |
| 4416 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4417 | // to copy-initializing a temporary of the underlying type with |
| 4418 | // the argument expression. Any difference in top-level |
| 4419 | // cv-qualification is subsumed by the initialization itself |
| 4420 | // and does not constitute a conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4421 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4422 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4423 | /*InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4424 | /*CStyle=*/false, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4425 | /*AllowObjCWritebackConversion=*/false, |
| 4426 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4427 | |
| 4428 | // Of course, that's still a reference binding. |
| 4429 | if (ICS.isStandard()) { |
| 4430 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4431 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4432 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4433 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4434 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4435 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4436 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4437 | // Don't allow rvalue references to bind to lvalues. |
| 4438 | if (DeclType->isRValueReferenceType()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4439 | if (const ReferenceType *RefType = |
| 4440 | ICS.UserDefined.ConversionFunction->getReturnType() |
| 4441 | ->getAs<LValueReferenceType>()) { |
Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4442 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4443 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4444 | DeclType); |
| 4445 | return ICS; |
| 4446 | } |
| 4447 | } |
| 4448 | } |
Ismail Pazarbasi | 99afd96 | 2014-01-24 10:54:12 +0000 | [diff] [blame] | 4449 | ICS.UserDefined.Before.setAsIdentityConversion(); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4450 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | 3ec7910 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4451 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4452 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4453 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4454 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4455 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4456 | } |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4457 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4458 | return ICS; |
| 4459 | } |
| 4460 | |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4461 | static ImplicitConversionSequence |
| 4462 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4463 | bool SuppressUserConversions, |
| 4464 | bool InOverloadResolution, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4465 | bool AllowObjCWritebackConversion, |
| 4466 | bool AllowExplicit = false); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4467 | |
| 4468 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4469 | /// initializer list From. |
| 4470 | static ImplicitConversionSequence |
| 4471 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4472 | bool SuppressUserConversions, |
| 4473 | bool InOverloadResolution, |
| 4474 | bool AllowObjCWritebackConversion) { |
| 4475 | // C++11 [over.ics.list]p1: |
| 4476 | // When an argument is an initializer list, it is not an expression and |
| 4477 | // special rules apply for converting it to a parameter type. |
| 4478 | |
| 4479 | ImplicitConversionSequence Result; |
| 4480 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4481 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4482 | // 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] | 4483 | // initialized from init lists. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4484 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4485 | return Result; |
| 4486 | |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4487 | // C++11 [over.ics.list]p2: |
| 4488 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4489 | // all the elements can be implicitly converted to X, the implicit |
| 4490 | // conversion sequence is the worst conversion necessary to convert an |
| 4491 | // element of the list to X. |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4492 | bool toStdInitializerList = false; |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4493 | QualType X; |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4494 | if (ToType->isArrayType()) |
Richard Smith | 0db1ea5 | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4495 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4496 | else |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4497 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4498 | if (!X.isNull()) { |
| 4499 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4500 | Expr *Init = From->getInit(i); |
| 4501 | ImplicitConversionSequence ICS = |
| 4502 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4503 | InOverloadResolution, |
| 4504 | AllowObjCWritebackConversion); |
| 4505 | // If a single element isn't convertible, fail. |
| 4506 | if (ICS.isBad()) { |
| 4507 | Result = ICS; |
| 4508 | break; |
| 4509 | } |
| 4510 | // Otherwise, look for the worst conversion. |
| 4511 | if (Result.isBad() || |
| 4512 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4513 | ImplicitConversionSequence::Worse) |
| 4514 | Result = ICS; |
| 4515 | } |
Douglas Gregor | 0f5c1c0 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4516 | |
| 4517 | // For an empty list, we won't have computed any conversion sequence. |
| 4518 | // Introduce the identity conversion sequence. |
| 4519 | if (From->getNumInits() == 0) { |
| 4520 | Result.setStandard(); |
| 4521 | Result.Standard.setAsIdentityConversion(); |
| 4522 | Result.Standard.setFromType(ToType); |
| 4523 | Result.Standard.setAllToTypes(ToType); |
| 4524 | } |
| 4525 | |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4526 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4527 | return Result; |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4528 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4529 | |
| 4530 | // C++11 [over.ics.list]p3: |
| 4531 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4532 | // resolution chooses a single best constructor [...] the implicit |
| 4533 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4534 | // constructors are viable but none is better than the others, the |
| 4535 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4536 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4537 | // This function can deal with initializer lists. |
Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4538 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4539 | /*AllowExplicit=*/false, |
| 4540 | InOverloadResolution, /*CStyle=*/false, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4541 | AllowObjCWritebackConversion, |
| 4542 | /*AllowObjCConversionOnExplicit=*/false); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4543 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4544 | |
| 4545 | // C++11 [over.ics.list]p4: |
| 4546 | // Otherwise, if the parameter has an aggregate type which can be |
| 4547 | // initialized from the initializer list [...] the implicit conversion |
| 4548 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4549 | if (ToType->isAggregateType()) { |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4550 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4551 | // down to checking whether the initialization works. |
| 4552 | // FIXME: Find out whether this parameter is consumed or not. |
| 4553 | InitializedEntity Entity = |
| 4554 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4555 | /*Consumed=*/false); |
| 4556 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4557 | Result.setUserDefined(); |
| 4558 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4559 | // Initializer lists don't have a type. |
| 4560 | Result.UserDefined.Before.setFromType(QualType()); |
| 4561 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4562 | |
| 4563 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4564 | Result.UserDefined.After.setFromType(ToType); |
| 4565 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | b6d6508 | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4566 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4567 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4568 | return Result; |
| 4569 | } |
| 4570 | |
| 4571 | // C++11 [over.ics.list]p5: |
| 4572 | // 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] | 4573 | if (ToType->isReferenceType()) { |
| 4574 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4575 | // mention initializer lists in any way. So we go by what list- |
| 4576 | // initialization would do and try to extrapolate from that. |
| 4577 | |
| 4578 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4579 | |
| 4580 | // If the initializer list has a single element that is reference-related |
| 4581 | // to the parameter type, we initialize the reference from that. |
| 4582 | if (From->getNumInits() == 1) { |
| 4583 | Expr *Init = From->getInit(0); |
| 4584 | |
| 4585 | QualType T2 = Init->getType(); |
| 4586 | |
| 4587 | // If the initializer is the address of an overloaded function, try |
| 4588 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4589 | // type of the resulting function. |
| 4590 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4591 | DeclAccessPair Found; |
| 4592 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4593 | Init, ToType, false, Found)) |
| 4594 | T2 = Fn->getType(); |
| 4595 | } |
| 4596 | |
| 4597 | // Compute some basic properties of the types and the initializer. |
| 4598 | bool dummy1 = false; |
| 4599 | bool dummy2 = false; |
| 4600 | bool dummy3 = false; |
| 4601 | Sema::ReferenceCompareResult RefRelationship |
| 4602 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4603 | dummy2, dummy3); |
| 4604 | |
Richard Smith | 4d2bbd7 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4605 | if (RefRelationship >= Sema::Ref_Related) { |
Richard Smith | a93f102 | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4606 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4607 | SuppressUserConversions, |
| 4608 | /*AllowExplicit=*/false); |
Richard Smith | 4d2bbd7 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4609 | } |
Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | // Otherwise, we bind the reference to a temporary created from the |
| 4613 | // initializer list. |
| 4614 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4615 | InOverloadResolution, |
| 4616 | AllowObjCWritebackConversion); |
| 4617 | if (Result.isFailure()) |
| 4618 | return Result; |
| 4619 | assert(!Result.isEllipsis() && |
| 4620 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4621 | |
| 4622 | // Can we even bind to a temporary? |
| 4623 | if (ToType->isRValueReferenceType() || |
| 4624 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4625 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4626 | Result.UserDefined.After; |
| 4627 | SCS.ReferenceBinding = true; |
| 4628 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4629 | SCS.BindsToRvalue = true; |
| 4630 | SCS.BindsToFunctionLvalue = false; |
| 4631 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4632 | SCS.ObjCLifetimeConversionBinding = false; |
| 4633 | } else |
| 4634 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4635 | From, ToType); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4636 | return Result; |
Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4637 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4638 | |
| 4639 | // C++11 [over.ics.list]p6: |
| 4640 | // Otherwise, if the parameter type is not a class: |
| 4641 | if (!ToType->isRecordType()) { |
| 4642 | // - if the initializer list has one element, the implicit conversion |
| 4643 | // sequence is the one required to convert the element to the |
| 4644 | // parameter type. |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4645 | unsigned NumInits = From->getNumInits(); |
| 4646 | if (NumInits == 1) |
| 4647 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4648 | SuppressUserConversions, |
| 4649 | InOverloadResolution, |
| 4650 | AllowObjCWritebackConversion); |
| 4651 | // - if the initializer list has no elements, the implicit conversion |
| 4652 | // sequence is the identity conversion. |
| 4653 | else if (NumInits == 0) { |
| 4654 | Result.setStandard(); |
| 4655 | Result.Standard.setAsIdentityConversion(); |
John McCall | b73bc9a | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4656 | Result.Standard.setFromType(ToType); |
| 4657 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4658 | } |
| 4659 | return Result; |
| 4660 | } |
| 4661 | |
| 4662 | // C++11 [over.ics.list]p7: |
| 4663 | // In all cases other than those enumerated above, no conversion is possible |
| 4664 | return Result; |
| 4665 | } |
| 4666 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4667 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4668 | /// ToType from the expression From. Return the implicit conversion |
| 4669 | /// sequence required to pass this argument, which may be a bad |
| 4670 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4671 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4672 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4673 | static ImplicitConversionSequence |
| 4674 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | bool SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4676 | bool InOverloadResolution, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4677 | bool AllowObjCWritebackConversion, |
| 4678 | bool AllowExplicit) { |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4679 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4680 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4681 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4682 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4683 | if (ToType->isReferenceType()) |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4684 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4685 | /*FIXME:*/From->getLocStart(), |
| 4686 | SuppressUserConversions, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4687 | AllowExplicit); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4688 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4689 | return TryImplicitConversion(S, From, ToType, |
| 4690 | SuppressUserConversions, |
| 4691 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4692 | InOverloadResolution, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4693 | /*CStyle=*/false, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4694 | AllowObjCWritebackConversion, |
| 4695 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4698 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4699 | const CanQualType ToQTy, |
| 4700 | Sema &S, |
| 4701 | SourceLocation Loc, |
| 4702 | ExprValueKind FromVK) { |
| 4703 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4704 | ImplicitConversionSequence ICS = |
| 4705 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4706 | |
| 4707 | return !ICS.isBad(); |
| 4708 | } |
| 4709 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4710 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4711 | /// parameter of the given member function (@c Method) from the |
| 4712 | /// expression @p From. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4713 | static ImplicitConversionSequence |
Richard Smith | 03c66d3 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4714 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4715 | Expr::Classification FromClassification, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4716 | CXXMethodDecl *Method, |
| 4717 | CXXRecordDecl *ActingContext) { |
| 4718 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4719 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4720 | // const volatile object. |
| 4721 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4722 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4723 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4724 | |
| 4725 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4726 | // to exit early. |
| 4727 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4728 | |
| 4729 | // We need to have an object of class type. |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4730 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4731 | FromType = PT->getPointeeType(); |
| 4732 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4733 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4734 | // better have an lvalue. |
| 4735 | assert(FromClassification.isLValue()); |
| 4736 | } |
| 4737 | |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4738 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4740 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4742 | // parameter is |
| 4743 | // |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4744 | // - "lvalue reference to cv X" for functions declared without a |
| 4745 | // ref-qualifier or with the & ref-qualifier |
| 4746 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4747 | // ref-qualifier |
| 4748 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 | // 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] | 4750 | // cv-qualification on the member function declaration. |
| 4751 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4752 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4753 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4754 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4755 | // reference binding here, that allows class rvalues to bind to |
| 4756 | // non-constant references. |
| 4757 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4758 | // First check the qualifiers. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4759 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4760 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4761 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4762 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4763 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 03c66d3 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4764 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4765 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4766 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4767 | |
| 4768 | // Check that we have either the same type or a derived type. It |
| 4769 | // affects the conversion rank. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4770 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4771 | ImplicitConversionKind SecondKind; |
| 4772 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4773 | SecondKind = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4774 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4775 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4776 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4777 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4778 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4779 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4780 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4782 | // Check the ref-qualifier. |
| 4783 | switch (Method->getRefQualifier()) { |
| 4784 | case RQ_None: |
| 4785 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4786 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4788 | case RQ_LValue: |
| 4789 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4790 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4791 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4792 | ImplicitParamType); |
| 4793 | return ICS; |
| 4794 | } |
| 4795 | break; |
| 4796 | |
| 4797 | case RQ_RValue: |
| 4798 | if (!FromClassification.isRValue()) { |
| 4799 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4800 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4801 | ImplicitParamType); |
| 4802 | return ICS; |
| 4803 | } |
| 4804 | break; |
| 4805 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4807 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4808 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4809 | ICS.Standard.setAsIdentityConversion(); |
| 4810 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4811 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4812 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4813 | ICS.Standard.ReferenceBinding = true; |
| 4814 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4816 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4817 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4818 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4819 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4820 | return ICS; |
| 4821 | } |
| 4822 | |
| 4823 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4824 | /// the implicit object parameter for the given Method with the given |
| 4825 | /// expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4826 | ExprResult |
| 4827 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4828 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4829 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4830 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4831 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4832 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4833 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4835 | Expr::Classification FromClassification; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4836 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4837 | FromRecordType = PT->getPointeeType(); |
| 4838 | DestType = Method->getThisType(Context); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4839 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4840 | } else { |
| 4841 | FromRecordType = From->getType(); |
| 4842 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4843 | FromClassification = From->Classify(Context); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4846 | // Note that we always use the true parent context when performing |
| 4847 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | ImplicitConversionSequence ICS |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4849 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4850 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4851 | if (ICS.isBad()) { |
| 4852 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4853 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4854 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4855 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4856 | if (CVR) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4857 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4858 | diag::err_member_function_call_bad_cvr) |
| 4859 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4860 | << From->getSourceRange(); |
| 4861 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4862 | << Method->getDeclName(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4863 | return ExprError(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4864 | } |
| 4865 | } |
| 4866 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4867 | return Diag(From->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4868 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4869 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4870 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4871 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4872 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4873 | ExprResult FromRes = |
| 4874 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4875 | if (FromRes.isInvalid()) |
| 4876 | return ExprError(); |
| 4877 | From = FromRes.take(); |
| 4878 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4879 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4880 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4881 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | 4a905b6 | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4882 | From->getValueKind()).take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4883 | return Owned(From); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4886 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4887 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4888 | static ImplicitConversionSequence |
| 4889 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4890 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4891 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4892 | /*AllowExplicit=*/true, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4893 | /*InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4894 | /*CStyle=*/false, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4895 | /*AllowObjCWritebackConversion=*/false, |
| 4896 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4900 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4901 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4902 | if (checkPlaceholderForOverload(*this, From)) |
| 4903 | return ExprError(); |
| 4904 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4905 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4906 | if (!ICS.isBad()) |
| 4907 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4908 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4909 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4910 | return Diag(From->getLocStart(), |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4911 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4912 | << From->getType() << From->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4913 | return ExprError(); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4914 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4915 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4916 | /// Check that the specified conversion is permitted in a converted constant |
| 4917 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4918 | /// is acceptable. |
| 4919 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4920 | StandardConversionSequence &SCS) { |
| 4921 | // Since we know that the target type is an integral or unscoped enumeration |
| 4922 | // type, most conversion kinds are impossible. All possible First and Third |
| 4923 | // conversions are fine. |
| 4924 | switch (SCS.Second) { |
| 4925 | case ICK_Identity: |
| 4926 | case ICK_Integral_Promotion: |
| 4927 | case ICK_Integral_Conversion: |
Guy Benyei | 259f9f4 | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4928 | case ICK_Zero_Event_Conversion: |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4929 | return true; |
| 4930 | |
| 4931 | case ICK_Boolean_Conversion: |
Richard Smith | ca24ed4 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4932 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4933 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4934 | // conversion, so it's permitted in a converted constant expression. |
| 4935 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4936 | SCS.getToType(2)->isBooleanType(); |
| 4937 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4938 | case ICK_Floating_Integral: |
| 4939 | case ICK_Complex_Real: |
| 4940 | return false; |
| 4941 | |
| 4942 | case ICK_Lvalue_To_Rvalue: |
| 4943 | case ICK_Array_To_Pointer: |
| 4944 | case ICK_Function_To_Pointer: |
| 4945 | case ICK_NoReturn_Adjustment: |
| 4946 | case ICK_Qualification: |
| 4947 | case ICK_Compatible_Conversion: |
| 4948 | case ICK_Vector_Conversion: |
| 4949 | case ICK_Vector_Splat: |
| 4950 | case ICK_Derived_To_Base: |
| 4951 | case ICK_Pointer_Conversion: |
| 4952 | case ICK_Pointer_Member: |
| 4953 | case ICK_Block_Pointer_Conversion: |
| 4954 | case ICK_Writeback_Conversion: |
| 4955 | case ICK_Floating_Promotion: |
| 4956 | case ICK_Complex_Promotion: |
| 4957 | case ICK_Complex_Conversion: |
| 4958 | case ICK_Floating_Conversion: |
| 4959 | case ICK_TransparentUnionConversion: |
| 4960 | llvm_unreachable("unexpected second conversion kind"); |
| 4961 | |
| 4962 | case ICK_Num_Conversion_Kinds: |
| 4963 | break; |
| 4964 | } |
| 4965 | |
| 4966 | llvm_unreachable("unknown conversion kind"); |
| 4967 | } |
| 4968 | |
| 4969 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4970 | /// converted constant expression of type T, perform the conversion and produce |
| 4971 | /// the converted expression, per C++11 [expr.const]p3. |
| 4972 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4973 | llvm::APSInt &Value, |
| 4974 | CCEKind CCE) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4975 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4976 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4977 | |
| 4978 | if (checkPlaceholderForOverload(*this, From)) |
| 4979 | return ExprError(); |
| 4980 | |
| 4981 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4982 | // A converted constant expression of type T is a core constant expression, |
| 4983 | // implicitly converted to a prvalue of type T, where the converted |
| 4984 | // expression is a literal constant expression and the implicit conversion |
| 4985 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4986 | // conversions, integral promotions, and integral conversions other than |
| 4987 | // narrowing conversions. |
| 4988 | ImplicitConversionSequence ICS = |
| 4989 | TryImplicitConversion(From, T, |
| 4990 | /*SuppressUserConversions=*/false, |
| 4991 | /*AllowExplicit=*/false, |
| 4992 | /*InOverloadResolution=*/false, |
| 4993 | /*CStyle=*/false, |
| 4994 | /*AllowObjcWritebackConversion=*/false); |
| 4995 | StandardConversionSequence *SCS = 0; |
| 4996 | switch (ICS.getKind()) { |
| 4997 | case ImplicitConversionSequence::StandardConversion: |
| 4998 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4999 | return Diag(From->getLocStart(), |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5000 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 5001 | << From->getType() << From->getSourceRange() << T; |
| 5002 | SCS = &ICS.Standard; |
| 5003 | break; |
| 5004 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5005 | // We are converting from class type to an integral or enumeration type, so |
| 5006 | // the Before sequence must be trivial. |
| 5007 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5008 | return Diag(From->getLocStart(), |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5009 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 5010 | << From->getType() << From->getSourceRange() << T; |
| 5011 | SCS = &ICS.UserDefined.After; |
| 5012 | break; |
| 5013 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5014 | case ImplicitConversionSequence::BadConversion: |
| 5015 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5016 | return Diag(From->getLocStart(), |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5017 | diag::err_typecheck_converted_constant_expression) |
| 5018 | << From->getType() << From->getSourceRange() << T; |
| 5019 | return ExprError(); |
| 5020 | |
| 5021 | case ImplicitConversionSequence::EllipsisConversion: |
| 5022 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 5023 | } |
| 5024 | |
| 5025 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 5026 | if (Result.isInvalid()) |
| 5027 | return Result; |
| 5028 | |
| 5029 | // Check for a narrowing implicit conversion. |
| 5030 | APValue PreNarrowingValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5031 | QualType PreNarrowingType; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5032 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 5033 | PreNarrowingType)) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5034 | case NK_Variable_Narrowing: |
| 5035 | // Implicit conversion to a narrower type, and the value is not a constant |
| 5036 | // expression. We'll diagnose this in a moment. |
| 5037 | case NK_Not_Narrowing: |
| 5038 | break; |
| 5039 | |
| 5040 | case NK_Constant_Narrowing: |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 5041 | Diag(From->getLocStart(), diag::ext_cce_narrowing) |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5042 | << CCE << /*Constant*/1 |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5043 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5044 | break; |
| 5045 | |
| 5046 | case NK_Type_Narrowing: |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 5047 | Diag(From->getLocStart(), diag::ext_cce_narrowing) |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5048 | << CCE << /*Constant*/0 << From->getType() << T; |
| 5049 | break; |
| 5050 | } |
| 5051 | |
| 5052 | // Check the expression is a constant expression. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5053 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5054 | Expr::EvalResult Eval; |
| 5055 | Eval.Diag = &Notes; |
| 5056 | |
Douglas Gregor | ebe2db7 | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 5057 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5058 | // The expression can't be folded, so we can't keep it at this position in |
| 5059 | // the AST. |
| 5060 | Result = ExprError(); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5061 | } else { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5062 | Value = Eval.Val.getInt(); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5063 | |
| 5064 | if (Notes.empty()) { |
| 5065 | // It's a constant expression. |
| 5066 | return Result; |
| 5067 | } |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5071 | if (Notes.size() == 1 && |
| 5072 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5073 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5074 | else { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5075 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5076 | << CCE << From->getSourceRange(); |
| 5077 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5078 | Diag(Notes[I].first, Notes[I].second); |
| 5079 | } |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5080 | return Result; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5083 | /// dropPointerConversions - If the given standard conversion sequence |
| 5084 | /// involves any pointer conversions, remove them. This may change |
| 5085 | /// the result type of the conversion sequence. |
| 5086 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5087 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5088 | SCS.Second = ICK_Identity; |
| 5089 | SCS.Third = ICK_Identity; |
| 5090 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5091 | } |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5092 | } |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5093 | |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5094 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5095 | /// convert the expression From to an Objective-C pointer type. |
| 5096 | static ImplicitConversionSequence |
| 5097 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5098 | // Do an implicit conversion to 'id'. |
| 5099 | QualType Ty = S.Context.getObjCIdType(); |
| 5100 | ImplicitConversionSequence ICS |
| 5101 | = TryImplicitConversion(S, From, Ty, |
| 5102 | // FIXME: Are these flags correct? |
| 5103 | /*SuppressUserConversions=*/false, |
| 5104 | /*AllowExplicit=*/true, |
| 5105 | /*InOverloadResolution=*/false, |
| 5106 | /*CStyle=*/false, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5107 | /*AllowObjCWritebackConversion=*/false, |
| 5108 | /*AllowObjCConversionOnExplicit=*/true); |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5109 | |
| 5110 | // Strip off any final conversions to 'id'. |
| 5111 | switch (ICS.getKind()) { |
| 5112 | case ImplicitConversionSequence::BadConversion: |
| 5113 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5114 | case ImplicitConversionSequence::EllipsisConversion: |
| 5115 | break; |
| 5116 | |
| 5117 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5118 | dropPointerConversion(ICS.UserDefined.After); |
| 5119 | break; |
| 5120 | |
| 5121 | case ImplicitConversionSequence::StandardConversion: |
| 5122 | dropPointerConversion(ICS.Standard); |
| 5123 | break; |
| 5124 | } |
| 5125 | |
| 5126 | return ICS; |
| 5127 | } |
| 5128 | |
| 5129 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5130 | /// conversion of the expression From to an Objective-C pointer type. |
| 5131 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5132 | if (checkPlaceholderForOverload(*this, From)) |
| 5133 | return ExprError(); |
| 5134 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5135 | QualType Ty = Context.getObjCIdType(); |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5136 | ImplicitConversionSequence ICS = |
| 5137 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5138 | if (!ICS.isBad()) |
| 5139 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5140 | return ExprError(); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5141 | } |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5142 | |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5143 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5144 | /// type of a permitted flavor. |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5145 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5146 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5147 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5150 | static ExprResult |
| 5151 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5152 | Sema::ContextualImplicitConverter &Converter, |
| 5153 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5154 | |
| 5155 | if (Converter.Suppress) |
| 5156 | return ExprError(); |
| 5157 | |
| 5158 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5159 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5160 | CXXConversionDecl *Conv = |
| 5161 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5162 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5163 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5164 | } |
| 5165 | return SemaRef.Owned(From); |
| 5166 | } |
| 5167 | |
| 5168 | static bool |
| 5169 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5170 | Sema::ContextualImplicitConverter &Converter, |
| 5171 | QualType T, bool HadMultipleCandidates, |
| 5172 | UnresolvedSetImpl &ExplicitConversions) { |
| 5173 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5174 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5175 | CXXConversionDecl *Conversion = |
| 5176 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5177 | |
| 5178 | // The user probably meant to invoke the given explicit |
| 5179 | // conversion; use it. |
| 5180 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5181 | std::string TypeStr; |
| 5182 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5183 | |
| 5184 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5185 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5186 | "static_cast<" + TypeStr + ">(") |
| 5187 | << FixItHint::CreateInsertion( |
| 5188 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5189 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5190 | |
| 5191 | // If we aren't in a SFINAE context, build a call to the |
| 5192 | // explicit conversion function. |
| 5193 | if (SemaRef.isSFINAEContext()) |
| 5194 | return true; |
| 5195 | |
| 5196 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5197 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5198 | HadMultipleCandidates); |
| 5199 | if (Result.isInvalid()) |
| 5200 | return true; |
| 5201 | // Record usage of conversion in an implicit cast. |
| 5202 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5203 | CK_UserDefinedConversion, Result.get(), 0, |
| 5204 | Result.get()->getValueKind()); |
| 5205 | } |
| 5206 | return false; |
| 5207 | } |
| 5208 | |
| 5209 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5210 | Sema::ContextualImplicitConverter &Converter, |
| 5211 | QualType T, bool HadMultipleCandidates, |
| 5212 | DeclAccessPair &Found) { |
| 5213 | CXXConversionDecl *Conversion = |
| 5214 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5215 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5216 | |
| 5217 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5218 | if (!Converter.SuppressConversion) { |
| 5219 | if (SemaRef.isSFINAEContext()) |
| 5220 | return true; |
| 5221 | |
| 5222 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5223 | << From->getSourceRange(); |
| 5224 | } |
| 5225 | |
| 5226 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5227 | HadMultipleCandidates); |
| 5228 | if (Result.isInvalid()) |
| 5229 | return true; |
| 5230 | // Record usage of conversion in an implicit cast. |
| 5231 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5232 | CK_UserDefinedConversion, Result.get(), 0, |
| 5233 | Result.get()->getValueKind()); |
| 5234 | return false; |
| 5235 | } |
| 5236 | |
| 5237 | static ExprResult finishContextualImplicitConversion( |
| 5238 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5239 | Sema::ContextualImplicitConverter &Converter) { |
| 5240 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5241 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5242 | << From->getSourceRange(); |
| 5243 | |
| 5244 | return SemaRef.DefaultLvalueConversion(From); |
| 5245 | } |
| 5246 | |
| 5247 | static void |
| 5248 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5249 | UnresolvedSetImpl &ViableConversions, |
| 5250 | OverloadCandidateSet &CandidateSet) { |
| 5251 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5252 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5253 | NamedDecl *D = FoundDecl.getDecl(); |
| 5254 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5255 | if (isa<UsingShadowDecl>(D)) |
| 5256 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5257 | |
| 5258 | CXXConversionDecl *Conv; |
| 5259 | FunctionTemplateDecl *ConvTemplate; |
| 5260 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5261 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5262 | else |
| 5263 | Conv = cast<CXXConversionDecl>(D); |
| 5264 | |
| 5265 | if (ConvTemplate) |
| 5266 | SemaRef.AddTemplateConversionCandidate( |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5267 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet, |
| 5268 | /*AllowObjCConversionOnExplicit=*/false); |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5269 | else |
| 5270 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5271 | ToType, CandidateSet, |
| 5272 | /*AllowObjCConversionOnExplicit=*/false); |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5273 | } |
| 5274 | } |
| 5275 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5276 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5277 | /// by the given converter. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5278 | /// |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5279 | /// This routine will attempt to convert an expression of class type to a |
| 5280 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5281 | /// must have a single non-explicit conversion function converting to a matching |
| 5282 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5283 | /// one target type. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5284 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5285 | /// \param Loc The source location of the construct that requires the |
| 5286 | /// conversion. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5287 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5288 | /// \param From The expression we're converting from. |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5289 | /// |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5290 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5291 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5292 | /// \returns The expression, converted to an integral or enumeration type if |
| 5293 | /// successful. |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5294 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5295 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5296 | // We can't perform any more checking for type-dependent expressions. |
| 5297 | if (From->isTypeDependent()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5298 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5299 | |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5300 | // Process placeholders immediately. |
| 5301 | if (From->hasPlaceholderType()) { |
| 5302 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5303 | if (result.isInvalid()) |
| 5304 | return result; |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5305 | From = result.take(); |
| 5306 | } |
| 5307 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5308 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5309 | QualType T = From->getType(); |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5310 | if (Converter.match(T)) |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5311 | return DefaultLvalueConversion(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5312 | |
| 5313 | // FIXME: Check for missing '()' if T is a function type? |
| 5314 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5315 | // We can only perform contextual implicit conversions on objects of class |
| 5316 | // type. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5317 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5318 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5319 | if (!Converter.Suppress) |
| 5320 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5321 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5322 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5324 | // We must have a complete class type. |
Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5325 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5326 | ContextualImplicitConverter &Converter; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5327 | Expr *From; |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5328 | |
| 5329 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5330 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5331 | |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5332 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5333 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5334 | } |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5335 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5336 | |
| 5337 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5338 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5339 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5340 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5341 | UnresolvedSet<4> |
| 5342 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5343 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5344 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5345 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5346 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5347 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5348 | bool HadMultipleCandidates = |
| 5349 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5350 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5351 | // To check that there is only one target type, in C++1y: |
| 5352 | QualType ToType; |
| 5353 | bool HasUniqueTargetType = true; |
| 5354 | |
| 5355 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5356 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5357 | E = Conversions.second; |
| 5358 | I != E; ++I) { |
| 5359 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5360 | CXXConversionDecl *Conversion; |
| 5361 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5362 | if (ConvTemplate) { |
| 5363 | if (getLangOpts().CPlusPlus1y) |
| 5364 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5365 | else |
| 5366 | continue; // C++11 does not consider conversion operator templates(?). |
| 5367 | } else |
| 5368 | Conversion = cast<CXXConversionDecl>(D); |
| 5369 | |
| 5370 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5371 | "Conversion operator templates are considered potentially " |
| 5372 | "viable in C++1y"); |
| 5373 | |
| 5374 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5375 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5376 | |
| 5377 | if (Conversion->isExplicit()) { |
| 5378 | // FIXME: For C++1y, do we need this restriction? |
| 5379 | // cf. diagnoseNoViableConversion() |
| 5380 | if (!ConvTemplate) |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5381 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5382 | } else { |
| 5383 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5384 | if (ToType.isNull()) |
| 5385 | ToType = CurToType.getUnqualifiedType(); |
| 5386 | else if (HasUniqueTargetType && |
| 5387 | (CurToType.getUnqualifiedType() != ToType)) |
| 5388 | HasUniqueTargetType = false; |
| 5389 | } |
| 5390 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5391 | } |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5392 | } |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5393 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5394 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5395 | if (getLangOpts().CPlusPlus1y) { |
| 5396 | // C++1y [conv]p6: |
| 5397 | // ... An expression e of class type E appearing in such a context |
| 5398 | // is said to be contextually implicitly converted to a specified |
| 5399 | // type T and is well-formed if and only if e can be implicitly |
| 5400 | // 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] | 5401 | // for conversion functions whose return type is cv T or reference to |
| 5402 | // 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] | 5403 | // exactly one such T. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5405 | // If no unique T is found: |
| 5406 | if (ToType.isNull()) { |
| 5407 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5408 | HadMultipleCandidates, |
| 5409 | ExplicitConversions)) |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5410 | return ExprError(); |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5411 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5412 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5413 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5414 | // If more than one unique Ts are found: |
| 5415 | if (!HasUniqueTargetType) |
| 5416 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5417 | ViableConversions); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5418 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5419 | // If one unique T is found: |
| 5420 | // First, build a candidate set from the previously recorded |
| 5421 | // potentially viable conversions. |
| 5422 | OverloadCandidateSet CandidateSet(Loc); |
| 5423 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5424 | CandidateSet); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5425 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5426 | // Then, perform overload resolution over the candidate set. |
| 5427 | OverloadCandidateSet::iterator Best; |
| 5428 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5429 | case OR_Success: { |
| 5430 | // Apply this conversion. |
| 5431 | DeclAccessPair Found = |
| 5432 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5433 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5434 | HadMultipleCandidates, Found)) |
| 5435 | return ExprError(); |
| 5436 | break; |
| 5437 | } |
| 5438 | case OR_Ambiguous: |
| 5439 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5440 | ViableConversions); |
| 5441 | case OR_No_Viable_Function: |
| 5442 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5443 | HadMultipleCandidates, |
| 5444 | ExplicitConversions)) |
| 5445 | return ExprError(); |
| 5446 | // fall through 'OR_Deleted' case. |
| 5447 | case OR_Deleted: |
| 5448 | // We'll complain below about a non-integral condition type. |
| 5449 | break; |
| 5450 | } |
| 5451 | } else { |
| 5452 | switch (ViableConversions.size()) { |
| 5453 | case 0: { |
| 5454 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5455 | HadMultipleCandidates, |
| 5456 | ExplicitConversions)) |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5457 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5458 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5459 | // We'll complain below about a non-integral condition type. |
| 5460 | break; |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5461 | } |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5462 | case 1: { |
| 5463 | // Apply this conversion. |
| 5464 | DeclAccessPair Found = ViableConversions[0]; |
| 5465 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5466 | HadMultipleCandidates, Found)) |
| 5467 | return ExprError(); |
| 5468 | break; |
| 5469 | } |
| 5470 | default: |
| 5471 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5472 | ViableConversions); |
| 5473 | } |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5474 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | |
Larisse Voufo | 236bec2 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5476 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5479 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5480 | /// candidate functions, using the given function call arguments. If |
| 5481 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5482 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5483 | /// |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5484 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5485 | /// based on an incomplete set of function arguments. This feature is used by |
| 5486 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | void |
| 5488 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5489 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5490 | ArrayRef<Expr *> Args, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5491 | OverloadCandidateSet &CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5492 | bool SuppressUserConversions, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5493 | bool PartialOverloading, |
| 5494 | bool AllowExplicit) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5495 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5496 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5497 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5498 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5499 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5500 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5501 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5502 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5503 | // If we get here, it's because we're calling a member function |
| 5504 | // that is named without a member access expression (e.g., |
| 5505 | // "this->f") that was either written explicitly or created |
| 5506 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5507 | // function, e.g., X::f(). We use an empty type for the implied |
| 5508 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5509 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5510 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5511 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5512 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5513 | return; |
| 5514 | } |
| 5515 | // We treat a constructor like a non-member function, since its object |
| 5516 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5519 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5520 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5521 | |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5522 | // C++11 [class.copy]p11: [DR1402] |
| 5523 | // A defaulted move constructor that is defined as deleted is ignored by |
| 5524 | // overload resolution. |
| 5525 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function); |
| 5526 | if (Constructor && Constructor->isDefaulted() && Constructor->isDeleted() && |
| 5527 | Constructor->isMoveConstructor()) |
| 5528 | return; |
| 5529 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5530 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5531 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5532 | |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5533 | if (Constructor) { |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5534 | // C++ [class.copy]p3: |
| 5535 | // A member function template is never instantiated to perform the copy |
| 5536 | // of a class object to an object of its class type. |
| 5537 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5538 | if (Args.size() == 1 && |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5539 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5540 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5541 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5542 | return; |
| 5543 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5544 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5545 | // Add this candidate |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5546 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5547 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5548 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5549 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5550 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5551 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5552 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5553 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 5554 | unsigned NumParams = Proto->getNumParams(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5555 | |
| 5556 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5557 | // parameters is viable only if it has an ellipsis in its parameter |
| 5558 | // list (8.3.5). |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 5559 | if ((Args.size() + (PartialOverloading && Args.size())) > NumParams && |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5560 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5561 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5562 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5563 | return; |
| 5564 | } |
| 5565 | |
| 5566 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5567 | // is viable only if the (m+1)st parameter has a default argument |
| 5568 | // (8.3.6). For the purposes of overload resolution, the |
| 5569 | // parameter list is truncated on the right, so that there are |
| 5570 | // exactly m parameters. |
| 5571 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5572 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5573 | // Not enough arguments. |
| 5574 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5575 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5576 | return; |
| 5577 | } |
| 5578 | |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5579 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5580 | if (getLangOpts().CUDA) |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5581 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5582 | if (CheckCUDATarget(Caller, Function)) { |
| 5583 | Candidate.Viable = false; |
| 5584 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5585 | return; |
| 5586 | } |
| 5587 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5588 | // Determine the implicit conversion sequences for each of the |
| 5589 | // arguments. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5590 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 5591 | if (ArgIdx < NumParams) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5592 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5593 | // exist for each argument an implicit conversion sequence |
| 5594 | // (13.3.3.1) that converts that argument to the corresponding |
| 5595 | // parameter of F. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 5596 | QualType ParamType = Proto->getParamType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5597 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5598 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5599 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5600 | /*InOverloadResolution=*/true, |
| 5601 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5602 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5603 | AllowExplicit); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5604 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5605 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5606 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5607 | return; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5608 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5609 | } else { |
| 5610 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5611 | // argument for which there is no corresponding parameter is |
| 5612 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5613 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5614 | } |
| 5615 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5616 | |
| 5617 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Function, Args)) { |
| 5618 | Candidate.Viable = false; |
| 5619 | Candidate.FailureKind = ovl_fail_enable_if; |
| 5620 | Candidate.DeductionFailure.Data = FailedAttr; |
| 5621 | return; |
| 5622 | } |
| 5623 | } |
| 5624 | |
| 5625 | static bool IsNotEnableIfAttr(Attr *A) { return !isa<EnableIfAttr>(A); } |
| 5626 | |
| 5627 | EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, |
| 5628 | bool MissingImplicitThis) { |
| 5629 | // FIXME: specific_attr_iterator<EnableIfAttr> iterates in reverse order, but |
| 5630 | // we need to find the first failing one. |
| 5631 | if (!Function->hasAttrs()) |
| 5632 | return 0; |
| 5633 | AttrVec Attrs = Function->getAttrs(); |
| 5634 | AttrVec::iterator E = std::remove_if(Attrs.begin(), Attrs.end(), |
| 5635 | IsNotEnableIfAttr); |
| 5636 | if (Attrs.begin() == E) |
| 5637 | return 0; |
| 5638 | std::reverse(Attrs.begin(), E); |
| 5639 | |
| 5640 | SFINAETrap Trap(*this); |
| 5641 | |
| 5642 | // Convert the arguments. |
| 5643 | SmallVector<Expr *, 16> ConvertedArgs; |
| 5644 | bool InitializationFailed = false; |
| 5645 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 5646 | if (i == 0 && !MissingImplicitThis && isa<CXXMethodDecl>(Function) && |
Nick Lewycky | b8336b7 | 2014-02-28 05:26:13 +0000 | [diff] [blame] | 5647 | !cast<CXXMethodDecl>(Function)->isStatic() && |
| 5648 | !isa<CXXConstructorDecl>(Function)) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5649 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Function); |
| 5650 | ExprResult R = |
| 5651 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 5652 | Method, Method); |
| 5653 | if (R.isInvalid()) { |
| 5654 | InitializationFailed = true; |
| 5655 | break; |
| 5656 | } |
| 5657 | ConvertedArgs.push_back(R.take()); |
| 5658 | } else { |
| 5659 | ExprResult R = |
| 5660 | PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 5661 | Context, |
| 5662 | Function->getParamDecl(i)), |
| 5663 | SourceLocation(), |
| 5664 | Args[i]); |
| 5665 | if (R.isInvalid()) { |
| 5666 | InitializationFailed = true; |
| 5667 | break; |
| 5668 | } |
| 5669 | ConvertedArgs.push_back(R.take()); |
| 5670 | } |
| 5671 | } |
| 5672 | |
| 5673 | if (InitializationFailed || Trap.hasErrorOccurred()) |
| 5674 | return cast<EnableIfAttr>(Attrs[0]); |
| 5675 | |
| 5676 | for (AttrVec::iterator I = Attrs.begin(); I != E; ++I) { |
| 5677 | APValue Result; |
| 5678 | EnableIfAttr *EIA = cast<EnableIfAttr>(*I); |
| 5679 | if (!EIA->getCond()->EvaluateWithSubstitution( |
| 5680 | Result, Context, Function, |
| 5681 | llvm::ArrayRef<const Expr*>(ConvertedArgs.data(), |
| 5682 | ConvertedArgs.size())) || |
| 5683 | !Result.isInt() || !Result.getInt().getBoolValue()) { |
| 5684 | return EIA; |
| 5685 | } |
| 5686 | } |
| 5687 | return 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5690 | /// \brief Add all of the function declarations in the given function set to |
Nick Lewycky | ed4265c | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 5691 | /// the overload candidate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5692 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5693 | ArrayRef<Expr *> Args, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5694 | OverloadCandidateSet& CandidateSet, |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5695 | bool SuppressUserConversions, |
| 5696 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5697 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5698 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5699 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5700 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5701 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5702 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5703 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5704 | Args.slice(1), CandidateSet, |
| 5705 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5706 | else |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5707 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5708 | SuppressUserConversions); |
| 5709 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5710 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5711 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5712 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5713 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5714 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5715 | ExplicitTemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5716 | Args[0]->getType(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5717 | Args[0]->Classify(Context), Args.slice(1), |
| 5718 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5719 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5720 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5721 | ExplicitTemplateArgs, Args, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5722 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5723 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5724 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5725 | } |
| 5726 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5727 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5728 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5729 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5730 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5731 | Expr::Classification ObjectClassification, |
Rafael Espindola | 51629df | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5732 | ArrayRef<Expr *> Args, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5733 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5734 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5735 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5736 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5737 | |
| 5738 | if (isa<UsingShadowDecl>(Decl)) |
| 5739 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5740 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5741 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5742 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5743 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5744 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5745 | /*ExplicitArgs*/ 0, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5746 | ObjectType, ObjectClassification, |
Rafael Espindola | 51629df | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5747 | Args, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5748 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5749 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5750 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5751 | ObjectType, ObjectClassification, |
Rafael Espindola | 51629df | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5752 | Args, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5753 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5754 | } |
| 5755 | } |
| 5756 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5757 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5758 | /// of candidate functions, using the given function call arguments |
| 5759 | /// and the object argument (@c Object). For example, in a call |
| 5760 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5761 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5762 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5763 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5764 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5765 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5766 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5767 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5768 | ArrayRef<Expr *> Args, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5769 | OverloadCandidateSet &CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5770 | bool SuppressUserConversions) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5771 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5772 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5773 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5774 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5775 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5776 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5777 | if (!CandidateSet.isNewCandidate(Method)) |
| 5778 | return; |
| 5779 | |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5780 | // C++11 [class.copy]p23: [DR1402] |
| 5781 | // A defaulted move assignment operator that is defined as deleted is |
| 5782 | // ignored by overload resolution. |
| 5783 | if (Method->isDefaulted() && Method->isDeleted() && |
| 5784 | Method->isMoveAssignmentOperator()) |
| 5785 | return; |
| 5786 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5787 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5788 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5789 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5790 | // Add this candidate |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5791 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5792 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5793 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5794 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5795 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5796 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5797 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 5798 | unsigned NumParams = Proto->getNumParams(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5799 | |
| 5800 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5801 | // parameters is viable only if it has an ellipsis in its parameter |
| 5802 | // list (8.3.5). |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 5803 | if (Args.size() > NumParams && !Proto->isVariadic()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5804 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5805 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5806 | return; |
| 5807 | } |
| 5808 | |
| 5809 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5810 | // is viable only if the (m+1)st parameter has a default argument |
| 5811 | // (8.3.6). For the purposes of overload resolution, the |
| 5812 | // parameter list is truncated on the right, so that there are |
| 5813 | // exactly m parameters. |
| 5814 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5815 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5816 | // Not enough arguments. |
| 5817 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5818 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5819 | return; |
| 5820 | } |
| 5821 | |
| 5822 | Candidate.Viable = true; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5823 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5824 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5825 | // The implicit object argument is ignored. |
| 5826 | Candidate.IgnoreObjectArgument = true; |
| 5827 | else { |
| 5828 | // Determine the implicit conversion sequence for the object |
| 5829 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5830 | Candidate.Conversions[0] |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5831 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5832 | Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5833 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5834 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5835 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5836 | return; |
| 5837 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | // Determine the implicit conversion sequences for each of the |
| 5841 | // arguments. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5842 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 5843 | if (ArgIdx < NumParams) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5844 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5845 | // exist for each argument an implicit conversion sequence |
| 5846 | // (13.3.3.1) that converts that argument to the corresponding |
| 5847 | // parameter of F. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 5848 | QualType ParamType = Proto->getParamType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5849 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5850 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5851 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5852 | /*InOverloadResolution=*/true, |
| 5853 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5854 | getLangOpts().ObjCAutoRefCount); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5855 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5856 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5857 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5858 | return; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5859 | } |
| 5860 | } else { |
| 5861 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5862 | // argument for which there is no corresponding parameter is |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5863 | // considered to "match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5864 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5865 | } |
| 5866 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 5867 | |
| 5868 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Method, Args, true)) { |
| 5869 | Candidate.Viable = false; |
| 5870 | Candidate.FailureKind = ovl_fail_enable_if; |
| 5871 | Candidate.DeductionFailure.Data = FailedAttr; |
| 5872 | return; |
| 5873 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5874 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5876 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5877 | /// set, using template argument deduction to produce an appropriate member |
| 5878 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5879 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5880 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5881 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5882 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5883 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5884 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5885 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5886 | ArrayRef<Expr *> Args, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5887 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5888 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5889 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5890 | return; |
| 5891 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5892 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5893 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5894 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5895 | // 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] | 5896 | // candidate functions in the usual way.113) A given name can refer to one |
| 5897 | // or more function templates and also to a set of overloaded non-template |
| 5898 | // functions. In such a case, the candidate functions generated from each |
| 5899 | // function template are combined with the set of non-template candidate |
| 5900 | // functions. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5901 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5902 | FunctionDecl *Specialization = 0; |
| 5903 | if (TemplateDeductionResult Result |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5904 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5905 | Specialization, Info)) { |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5906 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5907 | Candidate.FoundDecl = FoundDecl; |
| 5908 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5909 | Candidate.Viable = false; |
| 5910 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5911 | Candidate.IsSurrogate = false; |
| 5912 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5913 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5915 | Info); |
| 5916 | return; |
| 5917 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5918 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5919 | // Add the function template specialization produced by template argument |
| 5920 | // deduction as a candidate. |
| 5921 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5922 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5923 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5924 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5925 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5926 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5929 | /// \brief Add a C++ function template specialization as a candidate |
| 5930 | /// in the candidate set, using template argument deduction to produce |
| 5931 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5932 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5933 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5934 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5935 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5936 | ArrayRef<Expr *> Args, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5937 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5938 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5939 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5940 | return; |
| 5941 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5942 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5943 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5944 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5945 | // 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] | 5946 | // candidate functions in the usual way.113) A given name can refer to one |
| 5947 | // or more function templates and also to a set of overloaded non-template |
| 5948 | // functions. In such a case, the candidate functions generated from each |
| 5949 | // function template are combined with the set of non-template candidate |
| 5950 | // functions. |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5951 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5952 | FunctionDecl *Specialization = 0; |
| 5953 | if (TemplateDeductionResult Result |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5954 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5955 | Specialization, Info)) { |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5956 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5957 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5958 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5959 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5960 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5961 | Candidate.IsSurrogate = false; |
| 5962 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5963 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5965 | Info); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5966 | return; |
| 5967 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5968 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5969 | // Add the function template specialization produced by template argument |
| 5970 | // deduction as a candidate. |
| 5971 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5972 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5973 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5974 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5975 | |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5976 | /// Determine whether this is an allowable conversion from the result |
| 5977 | /// of an explicit conversion operator to the expected type, per C++ |
| 5978 | /// [over.match.conv]p1 and [over.match.ref]p1. |
| 5979 | /// |
| 5980 | /// \param ConvType The return type of the conversion function. |
| 5981 | /// |
| 5982 | /// \param ToType The type we are converting to. |
| 5983 | /// |
| 5984 | /// \param AllowObjCPointerConversion Allow a conversion from one |
| 5985 | /// Objective-C pointer to another. |
| 5986 | /// |
| 5987 | /// \returns true if the conversion is allowable, false otherwise. |
| 5988 | static bool isAllowableExplicitConversion(Sema &S, |
| 5989 | QualType ConvType, QualType ToType, |
| 5990 | bool AllowObjCPointerConversion) { |
| 5991 | QualType ToNonRefType = ToType.getNonReferenceType(); |
| 5992 | |
| 5993 | // Easy case: the types are the same. |
| 5994 | if (S.Context.hasSameUnqualifiedType(ConvType, ToNonRefType)) |
| 5995 | return true; |
| 5996 | |
| 5997 | // Allow qualification conversions. |
| 5998 | bool ObjCLifetimeConversion; |
| 5999 | if (S.IsQualificationConversion(ConvType, ToNonRefType, /*CStyle*/false, |
| 6000 | ObjCLifetimeConversion)) |
| 6001 | return true; |
| 6002 | |
| 6003 | // If we're not allowed to consider Objective-C pointer conversions, |
| 6004 | // we're done. |
| 6005 | if (!AllowObjCPointerConversion) |
| 6006 | return false; |
| 6007 | |
| 6008 | // Is this an Objective-C pointer conversion? |
| 6009 | bool IncompatibleObjC = false; |
| 6010 | QualType ConvertedType; |
| 6011 | return S.isObjCPointerConversion(ConvType, ToNonRefType, ConvertedType, |
| 6012 | IncompatibleObjC); |
| 6013 | } |
| 6014 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6015 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6016 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6017 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6018 | /// 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] | 6019 | /// (which may or may not be the same type as the type that the |
| 6020 | /// conversion function produces). |
| 6021 | void |
| 6022 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6023 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6024 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6025 | Expr *From, QualType ToType, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6026 | OverloadCandidateSet& CandidateSet, |
| 6027 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6028 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 6029 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6030 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6031 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6032 | return; |
| 6033 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 6034 | // If the conversion function has an undeduced return type, trigger its |
| 6035 | // deduction now. |
| 6036 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 6037 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 6038 | return; |
| 6039 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 6040 | } |
| 6041 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 6042 | // Per C++ [over.match.conv]p1, [over.match.ref]p1, an explicit conversion |
| 6043 | // operator is only a candidate if its return type is the target type or |
| 6044 | // can be converted to the target type with a qualification conversion. |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6045 | if (Conversion->isExplicit() && |
| 6046 | !isAllowableExplicitConversion(*this, ConvType, ToType, |
| 6047 | AllowObjCConversionOnExplicit)) |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 6048 | return; |
| 6049 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6050 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6051 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6052 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6053 | // Add this candidate |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6054 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6055 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6056 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6057 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6058 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6059 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 6060 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 6061 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6062 | Candidate.Viable = true; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6063 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6064 | |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 6065 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6066 | // For conversion functions, the function is considered to be a member of |
| 6067 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 6068 | // defining the type of the implicit object parameter. |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6069 | // |
| 6070 | // Determine the implicit conversion sequence for the implicit |
| 6071 | // object parameter. |
| 6072 | QualType ImplicitParamType = From->getType(); |
| 6073 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 6074 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 6075 | CXXRecordDecl *ConversionContext |
| 6076 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6077 | |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6078 | Candidate.Conversions[0] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6079 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 6080 | From->Classify(Context), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6081 | Conversion, ConversionContext); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6082 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6083 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6084 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6085 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6086 | return; |
| 6087 | } |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 6088 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6089 | // 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] | 6090 | // derived to base as such conversions are given Conversion Rank. They only |
| 6091 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 6092 | QualType FromCanon |
| 6093 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 6094 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 6095 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 6096 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6097 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 6098 | return; |
| 6099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6100 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6101 | // To determine what the conversion from the result of calling the |
| 6102 | // conversion function to the type we're eventually trying to |
| 6103 | // convert to (ToType), we need to synthesize a call to the |
| 6104 | // conversion function and attempt copy initialization from it. This |
| 6105 | // makes sure that we get the right semantics with respect to |
| 6106 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 6107 | // call on the stack and we don't need its arguments to be |
| 6108 | // well-formed. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 6109 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6110 | VK_LValue, From->getLocStart()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6111 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 6112 | Context.getPointerType(Conversion->getType()), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6113 | CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6114 | &ConversionRef, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6115 | |
Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6116 | QualType ConversionType = Conversion->getConversionType(); |
| 6117 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 72ebdab | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 6118 | Candidate.Viable = false; |
| 6119 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 6120 | return; |
| 6121 | } |
| 6122 | |
Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6123 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6124 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6125 | // 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] | 6126 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 6127 | // allocator). |
Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6128 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6129 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 6130 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6131 | ImplicitConversionSequence ICS = |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6132 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6133 | /*SuppressUserConversions=*/true, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6134 | /*InOverloadResolution=*/false, |
| 6135 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6136 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6137 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6138 | case ImplicitConversionSequence::StandardConversion: |
| 6139 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6140 | |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6141 | // C++ [over.ics.user]p3: |
| 6142 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6143 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6144 | // shall have exact match rank. |
| 6145 | if (Conversion->getPrimaryTemplate() && |
| 6146 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 6147 | Candidate.Viable = false; |
| 6148 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6149 | return; |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6150 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6151 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6152 | // C++0x [dcl.init.ref]p5: |
| 6153 | // In the second case, if the reference is an rvalue reference and |
| 6154 | // the second standard conversion sequence of the user-defined |
| 6155 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 6156 | // program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6157 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6158 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 6159 | Candidate.Viable = false; |
| 6160 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6161 | return; |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6162 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6163 | break; |
| 6164 | |
| 6165 | case ImplicitConversionSequence::BadConversion: |
| 6166 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6167 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6168 | return; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6169 | |
| 6170 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6171 | llvm_unreachable( |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6172 | "Can only end up with a standard conversion sequence or failure"); |
| 6173 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6174 | |
| 6175 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, ArrayRef<Expr*>())) { |
| 6176 | Candidate.Viable = false; |
| 6177 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6178 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6179 | return; |
| 6180 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6181 | } |
| 6182 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6183 | /// \brief Adds a conversion function template specialization |
| 6184 | /// candidate to the overload set, using template argument deduction |
| 6185 | /// to deduce the template arguments of the conversion function |
| 6186 | /// template from the type that we are converting to (C++ |
| 6187 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6188 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6189 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6190 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6191 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6192 | Expr *From, QualType ToType, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6193 | OverloadCandidateSet &CandidateSet, |
| 6194 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6195 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 6196 | "Only conversion function templates permitted here"); |
| 6197 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6198 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 6199 | return; |
| 6200 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6201 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6202 | CXXConversionDecl *Specialization = 0; |
| 6203 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6204 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6205 | Specialization, Info)) { |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6206 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6207 | Candidate.FoundDecl = FoundDecl; |
| 6208 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 6209 | Candidate.Viable = false; |
| 6210 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6211 | Candidate.IsSurrogate = false; |
| 6212 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6213 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6215 | Info); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6216 | return; |
| 6217 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6218 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6219 | // Add the conversion function template specialization produced by |
| 6220 | // template argument deduction as a candidate. |
| 6221 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6222 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6223 | CandidateSet, AllowObjCConversionOnExplicit); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6224 | } |
| 6225 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6226 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6227 | /// converts the given @c Object to a function pointer via the |
| 6228 | /// conversion function @c Conversion, and then attempts to call it |
| 6229 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6230 | /// the type of function that we'll eventually be calling. |
| 6231 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6232 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6233 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6234 | const FunctionProtoType *Proto, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6235 | Expr *Object, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6236 | ArrayRef<Expr *> Args, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6237 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6238 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6239 | return; |
| 6240 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6241 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6242 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6243 | |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6244 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6245 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6246 | Candidate.Function = 0; |
| 6247 | Candidate.Surrogate = Conversion; |
| 6248 | Candidate.Viable = true; |
| 6249 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6250 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6251 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6252 | |
| 6253 | // Determine the implicit conversion sequence for the implicit |
| 6254 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6255 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6257 | Object->Classify(Context), |
| 6258 | Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6259 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6260 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6261 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6262 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6263 | return; |
| 6264 | } |
| 6265 | |
| 6266 | // The first conversion is actually a user-defined conversion whose |
| 6267 | // first conversion is ObjectInit's standard conversion (which is |
| 6268 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6269 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6270 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6271 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6272 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6273 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6274 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6275 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6276 | = Candidate.Conversions[0].UserDefined.Before; |
| 6277 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6278 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6279 | // Find the |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 6280 | unsigned NumParams = Proto->getNumParams(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6281 | |
| 6282 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6283 | // parameters is viable only if it has an ellipsis in its parameter |
| 6284 | // list (8.3.5). |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 6285 | if (Args.size() > NumParams && !Proto->isVariadic()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6286 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6287 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6288 | return; |
| 6289 | } |
| 6290 | |
| 6291 | // Function types don't have any default arguments, so just check if |
| 6292 | // we have enough arguments. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 6293 | if (Args.size() < NumParams) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6294 | // Not enough arguments. |
| 6295 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6296 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6297 | return; |
| 6298 | } |
| 6299 | |
| 6300 | // Determine the implicit conversion sequences for each of the |
| 6301 | // arguments. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6302 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 6303 | if (ArgIdx < NumParams) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6304 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6305 | // exist for each argument an implicit conversion sequence |
| 6306 | // (13.3.3.1) that converts that argument to the corresponding |
| 6307 | // parameter of F. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6308 | QualType ParamType = Proto->getParamType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6309 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6310 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6311 | /*SuppressUserConversions=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6312 | /*InOverloadResolution=*/false, |
| 6313 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6314 | getLangOpts().ObjCAutoRefCount); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6315 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6316 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6317 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6318 | return; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6319 | } |
| 6320 | } else { |
| 6321 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6322 | // argument for which there is no corresponding parameter is |
| 6323 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6324 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6325 | } |
| 6326 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6327 | |
| 6328 | if (EnableIfAttr *FailedAttr = CheckEnableIf(Conversion, ArrayRef<Expr*>())) { |
| 6329 | Candidate.Viable = false; |
| 6330 | Candidate.FailureKind = ovl_fail_enable_if; |
| 6331 | Candidate.DeductionFailure.Data = FailedAttr; |
| 6332 | return; |
| 6333 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6336 | /// \brief Add overload candidates for overloaded operators that are |
| 6337 | /// member functions. |
| 6338 | /// |
| 6339 | /// Add the overloaded operator candidates that are member functions |
| 6340 | /// for the operator Op that was used in an operator expression such |
| 6341 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6342 | /// CandidateSet will store the added overload candidates. (C++ |
| 6343 | /// [over.match.oper]). |
| 6344 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6345 | SourceLocation OpLoc, |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6346 | ArrayRef<Expr *> Args, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6347 | OverloadCandidateSet& CandidateSet, |
| 6348 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6349 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6350 | |
| 6351 | // C++ [over.match.oper]p3: |
| 6352 | // For a unary operator @ with an operand of a type whose |
| 6353 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6354 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6355 | // a right operand of a type whose cv-unqualified version is T2, |
| 6356 | // three sets of candidate functions, designated member |
| 6357 | // candidates, non-member candidates and built-in candidates, are |
| 6358 | // constructed as follows: |
| 6359 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6360 | |
Richard Smith | 0feaf0c | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6361 | // -- If T1 is a complete class type or a class currently being |
| 6362 | // defined, the set of member candidates is the result of the |
| 6363 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6364 | // the set of member candidates is empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6365 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | 0feaf0c | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6366 | // Complete the type if it can be completed. |
| 6367 | RequireCompleteType(OpLoc, T1, 0); |
| 6368 | // If the type is neither complete nor being defined, bail out now. |
| 6369 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6370 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6371 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6372 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6373 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6374 | Operators.suppressDiagnostics(); |
| 6375 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6376 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6377 | OperEnd = Operators.end(); |
| 6378 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6379 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6380 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 51629df | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6381 | Args[0]->Classify(Context), |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6382 | Args.slice(1), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6383 | CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6384 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6385 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6388 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6389 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6390 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6391 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6392 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6393 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6394 | /// (at the beginning of the argument list) that will be contextually |
| 6395 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6396 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6397 | ArrayRef<Expr *> Args, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6398 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6399 | bool IsAssignmentOperator, |
| 6400 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6401 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6402 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6403 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6404 | // Add this candidate |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6405 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6406 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6407 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6408 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6409 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6410 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6411 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6412 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6413 | |
| 6414 | // Determine the implicit conversion sequences for each of the |
| 6415 | // arguments. |
| 6416 | Candidate.Viable = true; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6417 | Candidate.ExplicitCallArguments = Args.size(); |
| 6418 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6419 | // C++ [over.match.oper]p4: |
| 6420 | // For the built-in assignment operators, conversions of the |
| 6421 | // left operand are restricted as follows: |
| 6422 | // -- no temporaries are introduced to hold the left operand, and |
| 6423 | // -- no user-defined conversions are applied to the left |
| 6424 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6425 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6426 | // |
| 6427 | // We block these conversions by turning off user-defined |
| 6428 | // conversions, since that is the only way that initialization of |
| 6429 | // a reference to a non-class type can occur from something that |
| 6430 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6431 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6432 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6433 | "Contextual conversion to bool requires bool type"); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6434 | Candidate.Conversions[ArgIdx] |
| 6435 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6436 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6437 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6438 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6439 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6440 | /*InOverloadResolution=*/false, |
| 6441 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6442 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6443 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6444 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6445 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6446 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6447 | break; |
| 6448 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6449 | } |
| 6450 | } |
| 6451 | |
Craig Topper | cd7b033 | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6452 | namespace { |
| 6453 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6454 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6455 | /// candidate operator functions for built-in operators (C++ |
| 6456 | /// [over.built]). The types are separated into pointer types and |
| 6457 | /// enumeration types. |
| 6458 | class BuiltinCandidateTypeSet { |
| 6459 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6460 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6461 | |
| 6462 | /// PointerTypes - The set of pointer types that will be used in the |
| 6463 | /// built-in candidates. |
| 6464 | TypeSet PointerTypes; |
| 6465 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6466 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6467 | /// used in the built-in candidates. |
| 6468 | TypeSet MemberPointerTypes; |
| 6469 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6470 | /// EnumerationTypes - The set of enumeration types that will be |
| 6471 | /// used in the built-in candidates. |
| 6472 | TypeSet EnumerationTypes; |
| 6473 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6474 | /// \brief 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] | 6475 | /// candidates. |
| 6476 | TypeSet VectorTypes; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6477 | |
| 6478 | /// \brief A flag indicating non-record types are viable candidates |
| 6479 | bool HasNonRecordTypes; |
| 6480 | |
| 6481 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6482 | /// were present in the candidate set. |
| 6483 | bool HasArithmeticOrEnumeralTypes; |
| 6484 | |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6485 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6486 | /// candidate set. |
| 6487 | bool HasNullPtrType; |
| 6488 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6489 | /// Sema - The semantic analysis instance where we are building the |
| 6490 | /// candidate type set. |
| 6491 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6493 | /// Context - The AST context in which we will build the type sets. |
| 6494 | ASTContext &Context; |
| 6495 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6496 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6497 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6498 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6499 | |
| 6500 | public: |
| 6501 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6502 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6503 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6504 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6505 | : HasNonRecordTypes(false), |
| 6506 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6507 | HasNullPtrType(false), |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6508 | SemaRef(SemaRef), |
| 6509 | Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6510 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6511 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6512 | SourceLocation Loc, |
| 6513 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6514 | bool AllowExplicitConversions, |
| 6515 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6516 | |
| 6517 | /// pointer_begin - First pointer type found; |
| 6518 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6519 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6520 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6521 | iterator pointer_end() { return PointerTypes.end(); } |
| 6522 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6523 | /// member_pointer_begin - First member pointer type found; |
| 6524 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6525 | |
| 6526 | /// member_pointer_end - Past the last member pointer type found; |
| 6527 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6528 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6529 | /// enumeration_begin - First enumeration type found; |
| 6530 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6531 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6532 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6533 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6534 | |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6535 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6536 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6537 | |
| 6538 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6539 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6540 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6541 | }; |
| 6542 | |
Craig Topper | cd7b033 | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6543 | } // end anonymous namespace |
| 6544 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6545 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6546 | /// the set of pointer types along with any more-qualified variants of |
| 6547 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6548 | /// will add "int const *", "int const volatile *", "int const |
| 6549 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6550 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6551 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6552 | /// |
| 6553 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6554 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6555 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6556 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6557 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6558 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6559 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6560 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6561 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6562 | QualType PointeeTy; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6563 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6564 | bool buildObjCPtr = false; |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6565 | if (!PointerTy) { |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6566 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6567 | PointeeTy = PTy->getPointeeType(); |
| 6568 | buildObjCPtr = true; |
| 6569 | } else { |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6570 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6571 | } |
| 6572 | |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6573 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6574 | // (the qualifier would sink to the element type), and for another, the |
| 6575 | // only overload situation where it matters is subscript or pointer +- int, |
| 6576 | // and those shouldn't have qualifier variants anyway. |
| 6577 | if (PointeeTy->isArrayType()) |
| 6578 | return true; |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6579 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6580 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6581 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6582 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6583 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6584 | // Iterate through all strict supersets of BaseCVR. |
| 6585 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6586 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6587 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6588 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6589 | |
| 6590 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6591 | // the type cannot be restrict-qualified. |
| 6592 | if ((CVR & Qualifiers::Restrict) && |
| 6593 | (!hasRestrict || |
| 6594 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6595 | continue; |
| 6596 | |
| 6597 | // Build qualified pointee type. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6598 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6599 | |
| 6600 | // Build qualified pointer type. |
| 6601 | QualType QPointerTy; |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6602 | if (!buildObjCPtr) |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6603 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6604 | else |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6605 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6606 | |
| 6607 | // Insert qualified pointer type. |
| 6608 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6609 | } |
| 6610 | |
| 6611 | return true; |
| 6612 | } |
| 6613 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6614 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6615 | /// to the set of pointer types along with any more-qualified variants of |
| 6616 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6617 | /// will add "int const *", "int const volatile *", "int const |
| 6618 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6619 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6620 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6621 | /// |
| 6622 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6623 | bool |
| 6624 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6625 | QualType Ty) { |
| 6626 | // Insert this type. |
| 6627 | if (!MemberPointerTypes.insert(Ty)) |
| 6628 | return false; |
| 6629 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6630 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6631 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6632 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6633 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6634 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6635 | // (the qualifier would sink to the element type), and for another, the |
| 6636 | // only overload situation where it matters is subscript or pointer +- int, |
| 6637 | // and those shouldn't have qualifier variants anyway. |
| 6638 | if (PointeeTy->isArrayType()) |
| 6639 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6640 | const Type *ClassTy = PointerTy->getClass(); |
| 6641 | |
| 6642 | // Iterate through all strict supersets of the pointee type's CVR |
| 6643 | // qualifiers. |
| 6644 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6645 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6646 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6648 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6649 | MemberPointerTypes.insert( |
| 6650 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | return true; |
| 6654 | } |
| 6655 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6656 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6657 | /// 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] | 6658 | /// primarily interested in pointer types and enumeration types. We also |
| 6659 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6660 | /// AllowUserConversions is true if we should look at the conversion |
| 6661 | /// functions of a class type, and AllowExplicitConversions if we |
| 6662 | /// should also include the explicit conversion functions of a class |
| 6663 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6664 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6665 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6666 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6667 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6668 | bool AllowExplicitConversions, |
| 6669 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6670 | // Only deal with canonical types. |
| 6671 | Ty = Context.getCanonicalType(Ty); |
| 6672 | |
| 6673 | // Look through reference types; they aren't part of the type of an |
| 6674 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6675 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6676 | Ty = RefTy->getPointeeType(); |
| 6677 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6678 | // If we're dealing with an array type, decay to the pointer. |
| 6679 | if (Ty->isArrayType()) |
| 6680 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6681 | |
| 6682 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6683 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6684 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6685 | // Flag if we ever add a non-record type. |
| 6686 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6687 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6688 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6689 | // Flag if we encounter an arithmetic type. |
| 6690 | HasArithmeticOrEnumeralTypes = |
| 6691 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6692 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6693 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6694 | PointerTypes.insert(Ty); |
| 6695 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6696 | // Insert our type, and its more-qualified variants, into the set |
| 6697 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6698 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6699 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6700 | } else if (Ty->isMemberPointerType()) { |
| 6701 | // Member pointers are far easier, since the pointee can't be converted. |
| 6702 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6703 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6704 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6705 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6706 | EnumerationTypes.insert(Ty); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6707 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6708 | // We treat vector types as arithmetic types in many contexts as an |
| 6709 | // extension. |
| 6710 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6711 | VectorTypes.insert(Ty); |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6712 | } else if (Ty->isNullPtrType()) { |
| 6713 | HasNullPtrType = true; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6714 | } else if (AllowUserConversions && TyRec) { |
| 6715 | // No conversion functions in incomplete types. |
| 6716 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6717 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6718 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6719 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6720 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6721 | CXXRecordDecl::conversion_iterator> |
| 6722 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6723 | for (CXXRecordDecl::conversion_iterator |
| 6724 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6725 | NamedDecl *D = I.getDecl(); |
| 6726 | if (isa<UsingShadowDecl>(D)) |
| 6727 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6728 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6729 | // Skip conversion function templates; they don't tell us anything |
| 6730 | // about which builtin types we can convert to. |
| 6731 | if (isa<FunctionTemplateDecl>(D)) |
| 6732 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6733 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6734 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6735 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6736 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6737 | VisibleQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6738 | } |
| 6739 | } |
| 6740 | } |
| 6741 | } |
| 6742 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6743 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6744 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6745 | /// given type to the candidate set. |
| 6746 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6747 | QualType T, |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6748 | ArrayRef<Expr *> Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6749 | OverloadCandidateSet &CandidateSet) { |
| 6750 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6752 | // T& operator=(T&, T) |
| 6753 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6754 | ParamTypes[1] = T; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6755 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6756 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6757 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6758 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6759 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6760 | ParamTypes[0] |
| 6761 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6762 | ParamTypes[1] = T; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6763 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6764 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6765 | } |
| 6766 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6767 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6768 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6769 | /// 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] | 6770 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6771 | Qualifiers VRQuals; |
| 6772 | const RecordType *TyRec; |
| 6773 | if (const MemberPointerType *RHSMPType = |
| 6774 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6775 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6776 | else |
| 6777 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6778 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6779 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6780 | VRQuals.addVolatile(); |
| 6781 | VRQuals.addRestrict(); |
| 6782 | return VRQuals; |
| 6783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6784 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6785 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6786 | if (!ClassDecl->hasDefinition()) |
| 6787 | return VRQuals; |
| 6788 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6789 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6790 | CXXRecordDecl::conversion_iterator> |
| 6791 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6792 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6793 | for (CXXRecordDecl::conversion_iterator |
| 6794 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6795 | NamedDecl *D = I.getDecl(); |
| 6796 | if (isa<UsingShadowDecl>(D)) |
| 6797 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6798 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6799 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6800 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6801 | CanTy = ResTypeRef->getPointeeType(); |
| 6802 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6803 | // as see them. |
| 6804 | bool done = false; |
| 6805 | while (!done) { |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6806 | if (CanTy.isRestrictQualified()) |
| 6807 | VRQuals.addRestrict(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6808 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6809 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6810 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6811 | CanTy->getAs<MemberPointerType>()) |
| 6812 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6813 | else |
| 6814 | done = true; |
| 6815 | if (CanTy.isVolatileQualified()) |
| 6816 | VRQuals.addVolatile(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6817 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6818 | return VRQuals; |
| 6819 | } |
| 6820 | } |
| 6821 | } |
| 6822 | return VRQuals; |
| 6823 | } |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6824 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6825 | namespace { |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6826 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6827 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6828 | /// candidates. It provides shared state and utility methods used throughout |
| 6829 | /// the process, as well as a helper method to add each group of builtin |
| 6830 | /// operator overloads from the standard to a candidate set. |
| 6831 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6832 | // Common instance state available to all overload candidate addition methods. |
| 6833 | Sema &S; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6834 | ArrayRef<Expr *> Args; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6835 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6836 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6837 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6838 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6839 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6840 | // Define some constants used to index and iterate over the arithemetic types |
| 6841 | // provided via the getArithmeticType() method below. |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6842 | // The "promoted arithmetic types" are the arithmetic |
| 6843 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6844 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6845 | static const unsigned LastIntegralType = 20; |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6846 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6847 | LastPromotedIntegralType = 11; |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6848 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6849 | LastPromotedArithmeticType = 11; |
| 6850 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6851 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6852 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6853 | CanQualType getArithmeticType(unsigned index) { |
| 6854 | assert(index < NumArithmeticTypes); |
| 6855 | static CanQualType ASTContext::* const |
| 6856 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6857 | // Start of promoted types. |
| 6858 | &ASTContext::FloatTy, |
| 6859 | &ASTContext::DoubleTy, |
| 6860 | &ASTContext::LongDoubleTy, |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6861 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6862 | // Start of integral types. |
| 6863 | &ASTContext::IntTy, |
| 6864 | &ASTContext::LongTy, |
| 6865 | &ASTContext::LongLongTy, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6866 | &ASTContext::Int128Ty, |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6867 | &ASTContext::UnsignedIntTy, |
| 6868 | &ASTContext::UnsignedLongTy, |
| 6869 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6870 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6871 | // End of promoted types. |
| 6872 | |
| 6873 | &ASTContext::BoolTy, |
| 6874 | &ASTContext::CharTy, |
| 6875 | &ASTContext::WCharTy, |
| 6876 | &ASTContext::Char16Ty, |
| 6877 | &ASTContext::Char32Ty, |
| 6878 | &ASTContext::SignedCharTy, |
| 6879 | &ASTContext::ShortTy, |
| 6880 | &ASTContext::UnsignedCharTy, |
| 6881 | &ASTContext::UnsignedShortTy, |
| 6882 | // End of integral types. |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6883 | // FIXME: What about complex? What about half? |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6884 | }; |
| 6885 | return S.Context.*ArithmeticTypes[index]; |
| 6886 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6887 | |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6888 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6889 | /// converions for the given arithmetic types. |
| 6890 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6891 | // Accelerator table for performing the usual arithmetic conversions. |
| 6892 | // The rules are basically: |
| 6893 | // - if either is floating-point, use the wider floating-point |
| 6894 | // - if same signedness, use the higher rank |
| 6895 | // - if same size, use unsigned of the higher rank |
| 6896 | // - use the larger type |
| 6897 | // These rules, together with the axiom that higher ranks are |
| 6898 | // never smaller, are sufficient to precompute all of these results |
| 6899 | // *except* when dealing with signed types of higher rank. |
| 6900 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6901 | // better not to make any assumptions). |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6902 | // We assume that int128 has a higher rank than long long on all platforms. |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6903 | enum PromotedType { |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6904 | Dep=-1, |
| 6905 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6906 | }; |
Nuno Lopes | 9af6b03 | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6907 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6908 | [LastPromotedArithmeticType] = { |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6909 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6910 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6911 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6912 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6913 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6914 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6915 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6916 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6917 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6918 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6919 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6920 | }; |
| 6921 | |
| 6922 | assert(L < LastPromotedArithmeticType); |
| 6923 | assert(R < LastPromotedArithmeticType); |
| 6924 | int Idx = ConversionsTable[L][R]; |
| 6925 | |
| 6926 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6927 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6928 | |
| 6929 | // Slow path: we need to compare widths. |
| 6930 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6931 | CanQualType LT = getArithmeticType(L), |
| 6932 | RT = getArithmeticType(R); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6933 | unsigned LW = S.Context.getIntWidth(LT), |
| 6934 | RW = S.Context.getIntWidth(RT); |
| 6935 | |
| 6936 | // If they're different widths, use the signed type. |
| 6937 | if (LW > RW) return LT; |
| 6938 | else if (LW < RW) return RT; |
| 6939 | |
| 6940 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6941 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6942 | assert(L == SLL || R == SLL); |
| 6943 | return S.Context.UnsignedLongLongTy; |
| 6944 | } |
| 6945 | |
Chandler Carruth | 5659c0c | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6946 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6947 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6948 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6949 | bool HasVolatile, |
| 6950 | bool HasRestrict) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6951 | QualType ParamTypes[2] = { |
| 6952 | S.Context.getLValueReferenceType(CandidateTy), |
| 6953 | S.Context.IntTy |
| 6954 | }; |
| 6955 | |
| 6956 | // Non-volatile version. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6957 | if (Args.size() == 1) |
| 6958 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6959 | else |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6960 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6961 | |
| 6962 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6963 | // add volatile version only if there are conversions to a volatile type. |
| 6964 | if (HasVolatile) { |
| 6965 | ParamTypes[0] = |
| 6966 | S.Context.getLValueReferenceType( |
| 6967 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6968 | if (Args.size() == 1) |
| 6969 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6970 | else |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6971 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6972 | } |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6973 | |
| 6974 | // Add restrict version only if there are conversions to a restrict type |
| 6975 | // and our candidate type is a non-restrict-qualified pointer. |
| 6976 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6977 | !CandidateTy.isRestrictQualified()) { |
| 6978 | ParamTypes[0] |
| 6979 | = S.Context.getLValueReferenceType( |
| 6980 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6981 | if (Args.size() == 1) |
| 6982 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6983 | else |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6984 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6985 | |
| 6986 | if (HasVolatile) { |
| 6987 | ParamTypes[0] |
| 6988 | = S.Context.getLValueReferenceType( |
| 6989 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6990 | (Qualifiers::Volatile | |
| 6991 | Qualifiers::Restrict))); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6992 | if (Args.size() == 1) |
| 6993 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6994 | else |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6995 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6996 | } |
| 6997 | } |
| 6998 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | public: |
| 7002 | BuiltinOperatorOverloadBuilder( |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7003 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7004 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7005 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7006 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7007 | OverloadCandidateSet &CandidateSet) |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7008 | : S(S), Args(Args), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7009 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7010 | HasArithmeticOrEnumeralCandidateType( |
| 7011 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7012 | CandidateTypes(CandidateTypes), |
| 7013 | CandidateSet(CandidateSet) { |
| 7014 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7015 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7016 | "Invalid first promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7017 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7018 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7019 | "Invalid last promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7020 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 7021 | == S.Context.FloatTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7022 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7023 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 7024 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7025 | "Invalid last promoted arithmetic type"); |
| 7026 | } |
| 7027 | |
| 7028 | // C++ [over.built]p3: |
| 7029 | // |
| 7030 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 7031 | // is either volatile or empty, there exist candidate operator |
| 7032 | // functions of the form |
| 7033 | // |
| 7034 | // VQ T& operator++(VQ T&); |
| 7035 | // T operator++(VQ T&, int); |
| 7036 | // |
| 7037 | // C++ [over.built]p4: |
| 7038 | // |
| 7039 | // For every pair (T, VQ), where T is an arithmetic type other |
| 7040 | // than bool, and VQ is either volatile or empty, there exist |
| 7041 | // candidate operator functions of the form |
| 7042 | // |
| 7043 | // VQ T& operator--(VQ T&); |
| 7044 | // T operator--(VQ T&, int); |
| 7045 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7046 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7047 | return; |
| 7048 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7049 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 7050 | Arith < NumArithmeticTypes; ++Arith) { |
| 7051 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7052 | getArithmeticType(Arith), |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7053 | VisibleTypeConversionsQuals.hasVolatile(), |
| 7054 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7055 | } |
| 7056 | } |
| 7057 | |
| 7058 | // C++ [over.built]p5: |
| 7059 | // |
| 7060 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7061 | // cv-unqualified object type, and VQ is either volatile or |
| 7062 | // empty, there exist candidate operator functions of the form |
| 7063 | // |
| 7064 | // T*VQ& operator++(T*VQ&); |
| 7065 | // T*VQ& operator--(T*VQ&); |
| 7066 | // T* operator++(T*VQ&, int); |
| 7067 | // T* operator--(T*VQ&, int); |
| 7068 | void addPlusPlusMinusMinusPointerOverloads() { |
| 7069 | for (BuiltinCandidateTypeSet::iterator |
| 7070 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7071 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7072 | Ptr != PtrEnd; ++Ptr) { |
| 7073 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7074 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7075 | continue; |
| 7076 | |
| 7077 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7078 | (!(*Ptr).isVolatileQualified() && |
| 7079 | VisibleTypeConversionsQuals.hasVolatile()), |
| 7080 | (!(*Ptr).isRestrictQualified() && |
| 7081 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7082 | } |
| 7083 | } |
| 7084 | |
| 7085 | // C++ [over.built]p6: |
| 7086 | // For every cv-qualified or cv-unqualified object type T, there |
| 7087 | // exist candidate operator functions of the form |
| 7088 | // |
| 7089 | // T& operator*(T*); |
| 7090 | // |
| 7091 | // C++ [over.built]p7: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7092 | // 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] | 7093 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7094 | // T& operator*(T*); |
| 7095 | void addUnaryStarPointerOverloads() { |
| 7096 | for (BuiltinCandidateTypeSet::iterator |
| 7097 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7098 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7099 | Ptr != PtrEnd; ++Ptr) { |
| 7100 | QualType ParamTy = *Ptr; |
| 7101 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7102 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 7103 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7104 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 7105 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 7106 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 7107 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7108 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7109 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7110 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7111 | } |
| 7112 | } |
| 7113 | |
| 7114 | // C++ [over.built]p9: |
| 7115 | // For every promoted arithmetic type T, there exist candidate |
| 7116 | // operator functions of the form |
| 7117 | // |
| 7118 | // T operator+(T); |
| 7119 | // T operator-(T); |
| 7120 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7121 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7122 | return; |
| 7123 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7124 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 7125 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7126 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7127 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7128 | } |
| 7129 | |
| 7130 | // Extension: We also add these operators for vector types. |
| 7131 | for (BuiltinCandidateTypeSet::iterator |
| 7132 | Vec = CandidateTypes[0].vector_begin(), |
| 7133 | VecEnd = CandidateTypes[0].vector_end(); |
| 7134 | Vec != VecEnd; ++Vec) { |
| 7135 | QualType VecTy = *Vec; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7136 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7137 | } |
| 7138 | } |
| 7139 | |
| 7140 | // C++ [over.built]p8: |
| 7141 | // For every type T, there exist candidate operator functions of |
| 7142 | // the form |
| 7143 | // |
| 7144 | // T* operator+(T*); |
| 7145 | void addUnaryPlusPointerOverloads() { |
| 7146 | for (BuiltinCandidateTypeSet::iterator |
| 7147 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7148 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7149 | Ptr != PtrEnd; ++Ptr) { |
| 7150 | QualType ParamTy = *Ptr; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7151 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | // C++ [over.built]p10: |
| 7156 | // For every promoted integral type T, there exist candidate |
| 7157 | // operator functions of the form |
| 7158 | // |
| 7159 | // T operator~(T); |
| 7160 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7161 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7162 | return; |
| 7163 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7164 | for (unsigned Int = FirstPromotedIntegralType; |
| 7165 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7166 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7167 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7168 | } |
| 7169 | |
| 7170 | // Extension: We also add this operator for vector types. |
| 7171 | for (BuiltinCandidateTypeSet::iterator |
| 7172 | Vec = CandidateTypes[0].vector_begin(), |
| 7173 | VecEnd = CandidateTypes[0].vector_end(); |
| 7174 | Vec != VecEnd; ++Vec) { |
| 7175 | QualType VecTy = *Vec; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7176 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7177 | } |
| 7178 | } |
| 7179 | |
| 7180 | // C++ [over.match.oper]p16: |
| 7181 | // For every pointer to member type T, there exist candidate operator |
| 7182 | // functions of the form |
| 7183 | // |
| 7184 | // bool operator==(T,T); |
| 7185 | // bool operator!=(T,T); |
| 7186 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 7187 | /// Set of (canonical) types that we've already handled. |
| 7188 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7189 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7190 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7191 | for (BuiltinCandidateTypeSet::iterator |
| 7192 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7193 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7194 | MemPtr != MemPtrEnd; |
| 7195 | ++MemPtr) { |
| 7196 | // Don't add the same builtin candidate twice. |
| 7197 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7198 | continue; |
| 7199 | |
| 7200 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7201 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7202 | } |
| 7203 | } |
| 7204 | } |
| 7205 | |
| 7206 | // C++ [over.built]p15: |
| 7207 | // |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7208 | // For every T, where T is an enumeration type, a pointer type, or |
| 7209 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7210 | // |
| 7211 | // bool operator<(T, T); |
| 7212 | // bool operator>(T, T); |
| 7213 | // bool operator<=(T, T); |
| 7214 | // bool operator>=(T, T); |
| 7215 | // bool operator==(T, T); |
| 7216 | // bool operator!=(T, T); |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7217 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 14f082b | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7218 | // C++ [over.match.oper]p3: |
| 7219 | // [...]the built-in candidates include all of the candidate operator |
| 7220 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 7221 | // do not have the same parameter-type-list as any non-template non-member |
| 7222 | // candidate. |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7223 | // |
Eli Friedman | 14f082b | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7224 | // Note that in practice, this only affects enumeration types because there |
| 7225 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7226 | // must have an operand of record or enumeration type. Also, the only other |
| 7227 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7228 | // cannot be overloaded for enumeration types, so this is the only place |
| 7229 | // where we must suppress candidates like this. |
| 7230 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7231 | UserDefinedBinaryOperators; |
| 7232 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7233 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7234 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7235 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7236 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7237 | CEnd = CandidateSet.end(); |
| 7238 | C != CEnd; ++C) { |
| 7239 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7240 | continue; |
| 7241 | |
Eli Friedman | 14f082b | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7242 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7243 | continue; |
| 7244 | |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7245 | QualType FirstParamType = |
| 7246 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7247 | QualType SecondParamType = |
| 7248 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7249 | |
| 7250 | // Skip if either parameter isn't of enumeral type. |
| 7251 | if (!FirstParamType->isEnumeralType() || |
| 7252 | !SecondParamType->isEnumeralType()) |
| 7253 | continue; |
| 7254 | |
| 7255 | // Add this operator to the set of known user-defined operators. |
| 7256 | UserDefinedBinaryOperators.insert( |
| 7257 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7258 | S.Context.getCanonicalType(SecondParamType))); |
| 7259 | } |
| 7260 | } |
| 7261 | } |
| 7262 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7263 | /// Set of (canonical) types that we've already handled. |
| 7264 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7265 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7266 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7267 | for (BuiltinCandidateTypeSet::iterator |
| 7268 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7269 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7270 | Ptr != PtrEnd; ++Ptr) { |
| 7271 | // Don't add the same builtin candidate twice. |
| 7272 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7273 | continue; |
| 7274 | |
| 7275 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7276 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7277 | } |
| 7278 | for (BuiltinCandidateTypeSet::iterator |
| 7279 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7280 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7281 | Enum != EnumEnd; ++Enum) { |
| 7282 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7283 | |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7284 | // Don't add the same builtin candidate twice, or if a user defined |
| 7285 | // candidate exists. |
| 7286 | if (!AddedTypes.insert(CanonType) || |
| 7287 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7288 | CanonType))) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7289 | continue; |
| 7290 | |
| 7291 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7292 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7293 | } |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7294 | |
| 7295 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7296 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7297 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7298 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7299 | NullPtrTy))) { |
| 7300 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7301 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7302 | CandidateSet); |
| 7303 | } |
| 7304 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | // C++ [over.built]p13: |
| 7309 | // |
| 7310 | // For every cv-qualified or cv-unqualified object type T |
| 7311 | // there exist candidate operator functions of the form |
| 7312 | // |
| 7313 | // T* operator+(T*, ptrdiff_t); |
| 7314 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7315 | // T* operator-(T*, ptrdiff_t); |
| 7316 | // T* operator+(ptrdiff_t, T*); |
| 7317 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7318 | // |
| 7319 | // C++ [over.built]p14: |
| 7320 | // |
| 7321 | // For every T, where T is a pointer to object type, there |
| 7322 | // exist candidate operator functions of the form |
| 7323 | // |
| 7324 | // ptrdiff_t operator-(T, T); |
| 7325 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7326 | /// Set of (canonical) types that we've already handled. |
| 7327 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7328 | |
| 7329 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7330 | QualType AsymetricParamTypes[2] = { |
| 7331 | S.Context.getPointerDiffType(), |
| 7332 | S.Context.getPointerDiffType(), |
| 7333 | }; |
| 7334 | for (BuiltinCandidateTypeSet::iterator |
| 7335 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7336 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7337 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7338 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7339 | if (!PointeeTy->isObjectType()) |
| 7340 | continue; |
| 7341 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7342 | AsymetricParamTypes[Arg] = *Ptr; |
| 7343 | if (Arg == 0 || Op == OO_Plus) { |
| 7344 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7345 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7346 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7347 | } |
| 7348 | if (Op == OO_Minus) { |
| 7349 | // ptrdiff_t operator-(T, T); |
| 7350 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7351 | continue; |
| 7352 | |
| 7353 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7354 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7355 | Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7356 | } |
| 7357 | } |
| 7358 | } |
| 7359 | } |
| 7360 | |
| 7361 | // C++ [over.built]p12: |
| 7362 | // |
| 7363 | // For every pair of promoted arithmetic types L and R, there |
| 7364 | // exist candidate operator functions of the form |
| 7365 | // |
| 7366 | // LR operator*(L, R); |
| 7367 | // LR operator/(L, R); |
| 7368 | // LR operator+(L, R); |
| 7369 | // LR operator-(L, R); |
| 7370 | // bool operator<(L, R); |
| 7371 | // bool operator>(L, R); |
| 7372 | // bool operator<=(L, R); |
| 7373 | // bool operator>=(L, R); |
| 7374 | // bool operator==(L, R); |
| 7375 | // bool operator!=(L, R); |
| 7376 | // |
| 7377 | // where LR is the result of the usual arithmetic conversions |
| 7378 | // between types L and R. |
| 7379 | // |
| 7380 | // C++ [over.built]p24: |
| 7381 | // |
| 7382 | // For every pair of promoted arithmetic types L and R, there exist |
| 7383 | // candidate operator functions of the form |
| 7384 | // |
| 7385 | // LR operator?(bool, L, R); |
| 7386 | // |
| 7387 | // where LR is the result of the usual arithmetic conversions |
| 7388 | // between types L and R. |
| 7389 | // Our candidates ignore the first parameter. |
| 7390 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7391 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7392 | return; |
| 7393 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7394 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7395 | Left < LastPromotedArithmeticType; ++Left) { |
| 7396 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7397 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7398 | QualType LandR[2] = { getArithmeticType(Left), |
| 7399 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7400 | QualType Result = |
| 7401 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7402 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7403 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7404 | } |
| 7405 | } |
| 7406 | |
| 7407 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7408 | // conditional operator for vector types. |
| 7409 | for (BuiltinCandidateTypeSet::iterator |
| 7410 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7411 | Vec1End = CandidateTypes[0].vector_end(); |
| 7412 | Vec1 != Vec1End; ++Vec1) { |
| 7413 | for (BuiltinCandidateTypeSet::iterator |
| 7414 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7415 | Vec2End = CandidateTypes[1].vector_end(); |
| 7416 | Vec2 != Vec2End; ++Vec2) { |
| 7417 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7418 | QualType Result = S.Context.BoolTy; |
| 7419 | if (!isComparison) { |
| 7420 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7421 | Result = *Vec1; |
| 7422 | else |
| 7423 | Result = *Vec2; |
| 7424 | } |
| 7425 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7426 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7427 | } |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | // C++ [over.built]p17: |
| 7432 | // |
| 7433 | // For every pair of promoted integral types L and R, there |
| 7434 | // exist candidate operator functions of the form |
| 7435 | // |
| 7436 | // LR operator%(L, R); |
| 7437 | // LR operator&(L, R); |
| 7438 | // LR operator^(L, R); |
| 7439 | // LR operator|(L, R); |
| 7440 | // L operator<<(L, R); |
| 7441 | // L operator>>(L, R); |
| 7442 | // |
| 7443 | // where LR is the result of the usual arithmetic conversions |
| 7444 | // between types L and R. |
| 7445 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7446 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7447 | return; |
| 7448 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7449 | for (unsigned Left = FirstPromotedIntegralType; |
| 7450 | Left < LastPromotedIntegralType; ++Left) { |
| 7451 | for (unsigned Right = FirstPromotedIntegralType; |
| 7452 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7453 | QualType LandR[2] = { getArithmeticType(Left), |
| 7454 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7455 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7456 | ? LandR[0] |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7457 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7458 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | } |
| 7462 | |
| 7463 | // C++ [over.built]p20: |
| 7464 | // |
| 7465 | // For every pair (T, VQ), where T is an enumeration or |
| 7466 | // pointer to member type and VQ is either volatile or |
| 7467 | // empty, there exist candidate operator functions of the form |
| 7468 | // |
| 7469 | // VQ T& operator=(VQ T&, T); |
| 7470 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7471 | /// Set of (canonical) types that we've already handled. |
| 7472 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7473 | |
| 7474 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7475 | for (BuiltinCandidateTypeSet::iterator |
| 7476 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7477 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7478 | Enum != EnumEnd; ++Enum) { |
| 7479 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7480 | continue; |
| 7481 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7482 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7483 | } |
| 7484 | |
| 7485 | for (BuiltinCandidateTypeSet::iterator |
| 7486 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7487 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7488 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7489 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7490 | continue; |
| 7491 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7492 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7493 | } |
| 7494 | } |
| 7495 | } |
| 7496 | |
| 7497 | // C++ [over.built]p19: |
| 7498 | // |
| 7499 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7500 | // volatile or empty, there exist candidate operator functions |
| 7501 | // of the form |
| 7502 | // |
| 7503 | // T*VQ& operator=(T*VQ&, T*); |
| 7504 | // |
| 7505 | // C++ [over.built]p21: |
| 7506 | // |
| 7507 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7508 | // cv-unqualified object type and VQ is either volatile or |
| 7509 | // empty, there exist candidate operator functions of the form |
| 7510 | // |
| 7511 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7512 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7513 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7514 | /// Set of (canonical) types that we've already handled. |
| 7515 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7516 | |
| 7517 | for (BuiltinCandidateTypeSet::iterator |
| 7518 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7519 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7520 | Ptr != PtrEnd; ++Ptr) { |
| 7521 | // If this is operator=, keep track of the builtin candidates we added. |
| 7522 | if (isEqualOp) |
| 7523 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7524 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7525 | continue; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7526 | |
| 7527 | // non-volatile version |
| 7528 | QualType ParamTypes[2] = { |
| 7529 | S.Context.getLValueReferenceType(*Ptr), |
| 7530 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7531 | }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7532 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7533 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7534 | |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7535 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7536 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7537 | if (NeedVolatile) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7538 | // volatile version |
| 7539 | ParamTypes[0] = |
| 7540 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7541 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7542 | /*IsAssigmentOperator=*/isEqualOp); |
| 7543 | } |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7544 | |
| 7545 | if (!(*Ptr).isRestrictQualified() && |
| 7546 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7547 | // restrict version |
| 7548 | ParamTypes[0] |
| 7549 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7550 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7551 | /*IsAssigmentOperator=*/isEqualOp); |
| 7552 | |
| 7553 | if (NeedVolatile) { |
| 7554 | // volatile restrict version |
| 7555 | ParamTypes[0] |
| 7556 | = S.Context.getLValueReferenceType( |
| 7557 | S.Context.getCVRQualifiedType(*Ptr, |
| 7558 | (Qualifiers::Volatile | |
| 7559 | Qualifiers::Restrict))); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7560 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7561 | /*IsAssigmentOperator=*/isEqualOp); |
| 7562 | } |
| 7563 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | if (isEqualOp) { |
| 7567 | for (BuiltinCandidateTypeSet::iterator |
| 7568 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7569 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7570 | Ptr != PtrEnd; ++Ptr) { |
| 7571 | // Make sure we don't add the same candidate twice. |
| 7572 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7573 | continue; |
| 7574 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7575 | QualType ParamTypes[2] = { |
| 7576 | S.Context.getLValueReferenceType(*Ptr), |
| 7577 | *Ptr, |
| 7578 | }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7579 | |
| 7580 | // non-volatile version |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7581 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7582 | /*IsAssigmentOperator=*/true); |
| 7583 | |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7584 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7585 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7586 | if (NeedVolatile) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7587 | // volatile version |
| 7588 | ParamTypes[0] = |
| 7589 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7590 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7591 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7592 | } |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7593 | |
| 7594 | if (!(*Ptr).isRestrictQualified() && |
| 7595 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7596 | // restrict version |
| 7597 | ParamTypes[0] |
| 7598 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7599 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7600 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7601 | |
| 7602 | if (NeedVolatile) { |
| 7603 | // volatile restrict version |
| 7604 | ParamTypes[0] |
| 7605 | = S.Context.getLValueReferenceType( |
| 7606 | S.Context.getCVRQualifiedType(*Ptr, |
| 7607 | (Qualifiers::Volatile | |
| 7608 | Qualifiers::Restrict))); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7609 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7610 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7611 | } |
| 7612 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7613 | } |
| 7614 | } |
| 7615 | } |
| 7616 | |
| 7617 | // C++ [over.built]p18: |
| 7618 | // |
| 7619 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7620 | // VQ is either volatile or empty, and R is a promoted |
| 7621 | // arithmetic type, there exist candidate operator functions of |
| 7622 | // the form |
| 7623 | // |
| 7624 | // VQ L& operator=(VQ L&, R); |
| 7625 | // VQ L& operator*=(VQ L&, R); |
| 7626 | // VQ L& operator/=(VQ L&, R); |
| 7627 | // VQ L& operator+=(VQ L&, R); |
| 7628 | // VQ L& operator-=(VQ L&, R); |
| 7629 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7630 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7631 | return; |
| 7632 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7633 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7634 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7635 | Right < LastPromotedArithmeticType; ++Right) { |
| 7636 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7637 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7638 | |
| 7639 | // Add this built-in operator as a candidate (VQ is empty). |
| 7640 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7641 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7642 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7643 | /*IsAssigmentOperator=*/isEqualOp); |
| 7644 | |
| 7645 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7646 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7647 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7648 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7649 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7650 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7651 | /*IsAssigmentOperator=*/isEqualOp); |
| 7652 | } |
| 7653 | } |
| 7654 | } |
| 7655 | |
| 7656 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7657 | for (BuiltinCandidateTypeSet::iterator |
| 7658 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7659 | Vec1End = CandidateTypes[0].vector_end(); |
| 7660 | Vec1 != Vec1End; ++Vec1) { |
| 7661 | for (BuiltinCandidateTypeSet::iterator |
| 7662 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7663 | Vec2End = CandidateTypes[1].vector_end(); |
| 7664 | Vec2 != Vec2End; ++Vec2) { |
| 7665 | QualType ParamTypes[2]; |
| 7666 | ParamTypes[1] = *Vec2; |
| 7667 | // Add this built-in operator as a candidate (VQ is empty). |
| 7668 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7669 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7670 | /*IsAssigmentOperator=*/isEqualOp); |
| 7671 | |
| 7672 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7673 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7674 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7675 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7676 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7677 | /*IsAssigmentOperator=*/isEqualOp); |
| 7678 | } |
| 7679 | } |
| 7680 | } |
| 7681 | } |
| 7682 | |
| 7683 | // C++ [over.built]p22: |
| 7684 | // |
| 7685 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7686 | // is either volatile or empty, and R is a promoted integral |
| 7687 | // type, there exist candidate operator functions of the form |
| 7688 | // |
| 7689 | // VQ L& operator%=(VQ L&, R); |
| 7690 | // VQ L& operator<<=(VQ L&, R); |
| 7691 | // VQ L& operator>>=(VQ L&, R); |
| 7692 | // VQ L& operator&=(VQ L&, R); |
| 7693 | // VQ L& operator^=(VQ L&, R); |
| 7694 | // VQ L& operator|=(VQ L&, R); |
| 7695 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7696 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7697 | return; |
| 7698 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7699 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7700 | for (unsigned Right = FirstPromotedIntegralType; |
| 7701 | Right < LastPromotedIntegralType; ++Right) { |
| 7702 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7703 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7704 | |
| 7705 | // Add this built-in operator as a candidate (VQ is empty). |
| 7706 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7707 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7708 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7709 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7710 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7711 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7712 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7713 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7714 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7715 | } |
| 7716 | } |
| 7717 | } |
| 7718 | } |
| 7719 | |
| 7720 | // C++ [over.operator]p23: |
| 7721 | // |
| 7722 | // There also exist candidate operator functions of the form |
| 7723 | // |
| 7724 | // bool operator!(bool); |
| 7725 | // bool operator&&(bool, bool); |
| 7726 | // bool operator||(bool, bool); |
| 7727 | void addExclaimOverload() { |
| 7728 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7729 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7730 | /*IsAssignmentOperator=*/false, |
| 7731 | /*NumContextualBoolArguments=*/1); |
| 7732 | } |
| 7733 | void addAmpAmpOrPipePipeOverload() { |
| 7734 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7735 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7736 | /*IsAssignmentOperator=*/false, |
| 7737 | /*NumContextualBoolArguments=*/2); |
| 7738 | } |
| 7739 | |
| 7740 | // C++ [over.built]p13: |
| 7741 | // |
| 7742 | // For every cv-qualified or cv-unqualified object type T there |
| 7743 | // exist candidate operator functions of the form |
| 7744 | // |
| 7745 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7746 | // T& operator[](T*, ptrdiff_t); |
| 7747 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7748 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7749 | // T& operator[](ptrdiff_t, T*); |
| 7750 | void addSubscriptOverloads() { |
| 7751 | for (BuiltinCandidateTypeSet::iterator |
| 7752 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7753 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7754 | Ptr != PtrEnd; ++Ptr) { |
| 7755 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7756 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7757 | if (!PointeeType->isObjectType()) |
| 7758 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7759 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7760 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7761 | |
| 7762 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7763 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7764 | } |
| 7765 | |
| 7766 | for (BuiltinCandidateTypeSet::iterator |
| 7767 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7768 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7769 | Ptr != PtrEnd; ++Ptr) { |
| 7770 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7771 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7772 | if (!PointeeType->isObjectType()) |
| 7773 | continue; |
| 7774 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7775 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7776 | |
| 7777 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7778 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7779 | } |
| 7780 | } |
| 7781 | |
| 7782 | // C++ [over.built]p11: |
| 7783 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7784 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7785 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7786 | // there exist candidate operator functions of the form |
| 7787 | // |
| 7788 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7789 | // |
| 7790 | // where CV12 is the union of CV1 and CV2. |
| 7791 | void addArrowStarOverloads() { |
| 7792 | for (BuiltinCandidateTypeSet::iterator |
| 7793 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7794 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7795 | Ptr != PtrEnd; ++Ptr) { |
| 7796 | QualType C1Ty = (*Ptr); |
| 7797 | QualType C1; |
| 7798 | QualifierCollector Q1; |
| 7799 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7800 | if (!isa<RecordType>(C1)) |
| 7801 | continue; |
| 7802 | // heuristic to reduce number of builtin candidates in the set. |
| 7803 | // Add volatile/restrict version only if there are conversions to a |
| 7804 | // volatile/restrict type. |
| 7805 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7806 | continue; |
| 7807 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7808 | continue; |
| 7809 | for (BuiltinCandidateTypeSet::iterator |
| 7810 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7811 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7812 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7813 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7814 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7815 | C2 = C2.getUnqualifiedType(); |
| 7816 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7817 | break; |
| 7818 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7819 | // build CV12 T& |
| 7820 | QualType T = mptr->getPointeeType(); |
| 7821 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7822 | T.isVolatileQualified()) |
| 7823 | continue; |
| 7824 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7825 | T.isRestrictQualified()) |
| 7826 | continue; |
| 7827 | T = Q1.apply(S.Context, T); |
| 7828 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7829 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7830 | } |
| 7831 | } |
| 7832 | } |
| 7833 | |
| 7834 | // Note that we don't consider the first argument, since it has been |
| 7835 | // contextually converted to bool long ago. The candidates below are |
| 7836 | // therefore added as binary. |
| 7837 | // |
| 7838 | // C++ [over.built]p25: |
| 7839 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7840 | // enumeration type, there exist candidate operator functions of the form |
| 7841 | // |
| 7842 | // T operator?(bool, T, T); |
| 7843 | // |
| 7844 | void addConditionalOperatorOverloads() { |
| 7845 | /// Set of (canonical) types that we've already handled. |
| 7846 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7847 | |
| 7848 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7849 | for (BuiltinCandidateTypeSet::iterator |
| 7850 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7851 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7852 | Ptr != PtrEnd; ++Ptr) { |
| 7853 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7854 | continue; |
| 7855 | |
| 7856 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7857 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7858 | } |
| 7859 | |
| 7860 | for (BuiltinCandidateTypeSet::iterator |
| 7861 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7862 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7863 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7864 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7865 | continue; |
| 7866 | |
| 7867 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7868 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7871 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7872 | for (BuiltinCandidateTypeSet::iterator |
| 7873 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7874 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7875 | Enum != EnumEnd; ++Enum) { |
| 7876 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7877 | continue; |
| 7878 | |
| 7879 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7880 | continue; |
| 7881 | |
| 7882 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7883 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7884 | } |
| 7885 | } |
| 7886 | } |
| 7887 | } |
| 7888 | }; |
| 7889 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7890 | } // end anonymous namespace |
| 7891 | |
| 7892 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7893 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7894 | /// on the operator @p Op and the arguments given. For example, if the |
| 7895 | /// operator is a binary '+', this routine might add "int |
| 7896 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7897 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7898 | SourceLocation OpLoc, |
| 7899 | ArrayRef<Expr *> Args, |
| 7900 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7901 | // Find all of the types that the arguments can convert to, but only |
| 7902 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7903 | // that make use of these types. Also record whether we encounter non-record |
| 7904 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7905 | Qualifiers VisibleTypeConversionsQuals; |
| 7906 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7907 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7908 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7909 | |
| 7910 | bool HasNonRecordCandidateType = false; |
| 7911 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7912 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7913 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7914 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7915 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7916 | OpLoc, |
| 7917 | true, |
| 7918 | (Op == OO_Exclaim || |
| 7919 | Op == OO_AmpAmp || |
| 7920 | Op == OO_PipePipe), |
| 7921 | VisibleTypeConversionsQuals); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7922 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7923 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7924 | HasArithmeticOrEnumeralCandidateType = |
| 7925 | HasArithmeticOrEnumeralCandidateType || |
| 7926 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7927 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7928 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7929 | // Exit early when no non-record types have been added to the candidate set |
| 7930 | // for any of the arguments to the operator. |
Douglas Gregor | 877d4eb | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7931 | // |
| 7932 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7933 | // 'bool' overloads. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7934 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 877d4eb | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7935 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7936 | return; |
| 7937 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7938 | // Setup an object to manage the common state for building overloads. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7939 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7940 | VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7941 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7942 | CandidateTypes, CandidateSet); |
| 7943 | |
| 7944 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7945 | switch (Op) { |
| 7946 | case OO_None: |
| 7947 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7948 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7949 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7950 | case OO_New: |
| 7951 | case OO_Delete: |
| 7952 | case OO_Array_New: |
| 7953 | case OO_Array_Delete: |
| 7954 | case OO_Call: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7955 | llvm_unreachable( |
| 7956 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7957 | |
| 7958 | case OO_Comma: |
| 7959 | case OO_Arrow: |
| 7960 | // C++ [over.match.oper]p3: |
| 7961 | // -- For the operator ',', the unary operator '&', or the |
| 7962 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7963 | break; |
| 7964 | |
| 7965 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7966 | if (Args.size() == 1) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7967 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7968 | // Fall through. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7969 | |
| 7970 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7971 | if (Args.size() == 1) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7972 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7973 | } else { |
| 7974 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7975 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7976 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7977 | break; |
| 7978 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7979 | case OO_Star: // '*' is either unary or binary |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7980 | if (Args.size() == 1) |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7981 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7982 | else |
| 7983 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7984 | break; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7985 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7986 | case OO_Slash: |
| 7987 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7988 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7989 | |
| 7990 | case OO_PlusPlus: |
| 7991 | case OO_MinusMinus: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7992 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7993 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7994 | break; |
| 7995 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7996 | case OO_EqualEqual: |
| 7997 | case OO_ExclaimEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7998 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7999 | // Fall through. |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 8000 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8001 | case OO_Less: |
| 8002 | case OO_Greater: |
| 8003 | case OO_LessEqual: |
| 8004 | case OO_GreaterEqual: |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 8005 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 8006 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 8007 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8008 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8009 | case OO_Percent: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8010 | case OO_Caret: |
| 8011 | case OO_Pipe: |
| 8012 | case OO_LessLess: |
| 8013 | case OO_GreaterGreater: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8014 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8015 | break; |
| 8016 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8017 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 8018 | if (Args.size() == 1) |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 8019 | // C++ [over.match.oper]p3: |
| 8020 | // -- For the operator ',', the unary operator '&', or the |
| 8021 | // operator '->', the built-in candidates set is empty. |
| 8022 | break; |
| 8023 | |
| 8024 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 8025 | break; |
| 8026 | |
| 8027 | case OO_Tilde: |
| 8028 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 8029 | break; |
| 8030 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8031 | case OO_Equal: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8032 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 8033 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8034 | |
| 8035 | case OO_PlusEqual: |
| 8036 | case OO_MinusEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8037 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8038 | // Fall through. |
| 8039 | |
| 8040 | case OO_StarEqual: |
| 8041 | case OO_SlashEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8042 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8043 | break; |
| 8044 | |
| 8045 | case OO_PercentEqual: |
| 8046 | case OO_LessLessEqual: |
| 8047 | case OO_GreaterGreaterEqual: |
| 8048 | case OO_AmpEqual: |
| 8049 | case OO_CaretEqual: |
| 8050 | case OO_PipeEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8051 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8052 | break; |
| 8053 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8054 | case OO_Exclaim: |
| 8055 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8056 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8057 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8058 | case OO_AmpAmp: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8059 | case OO_PipePipe: |
| 8060 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8061 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8062 | |
| 8063 | case OO_Subscript: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8064 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8065 | break; |
| 8066 | |
| 8067 | case OO_ArrowStar: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8068 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8069 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8070 | |
| 8071 | case OO_Conditional: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 8072 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 8073 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 8074 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8075 | } |
| 8076 | } |
| 8077 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8078 | /// \brief Add function candidates found via argument-dependent lookup |
| 8079 | /// to the set of overloading candidates. |
| 8080 | /// |
| 8081 | /// This routine performs argument-dependent name lookup based on the |
| 8082 | /// given function name (which may also be an operator name) and adds |
| 8083 | /// all of the overload candidates found by ADL to the overload |
| 8084 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8085 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8086 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | e06a2c1 | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 8087 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8088 | ArrayRef<Expr *> Args, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8089 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8090 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 8091 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8092 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8093 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 8094 | // FIXME: This approach for uniquing ADL results (and removing |
| 8095 | // redundant candidates from the set) relies on pointer-equality, |
| 8096 | // which means we need to key off the canonical decl. However, |
| 8097 | // always going back to the canonical decl might not get us the |
| 8098 | // right set of default arguments. What default arguments are |
| 8099 | // we supposed to consider on ADL candidates, anyway? |
| 8100 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8101 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 8102 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8103 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8104 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8105 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 8106 | CandEnd = CandidateSet.end(); |
| 8107 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8108 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8109 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8110 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8111 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8112 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 8113 | |
| 8114 | // For each of the ADL candidates we found, add it to the overload |
| 8115 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 8116 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8117 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8118 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8119 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8120 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8121 | |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8122 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 8123 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8124 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8125 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8126 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8127 | Args, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 8128 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8131 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 8132 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8133 | bool |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8134 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8135 | const OverloadCandidate &Cand1, |
| 8136 | const OverloadCandidate &Cand2, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8137 | SourceLocation Loc, |
| 8138 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8139 | // Define viable functions to be better candidates than non-viable |
| 8140 | // functions. |
| 8141 | if (!Cand2.Viable) |
| 8142 | return Cand1.Viable; |
| 8143 | else if (!Cand1.Viable) |
| 8144 | return false; |
| 8145 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8146 | // C++ [over.match.best]p1: |
| 8147 | // |
| 8148 | // -- if F is a static member function, ICS1(F) is defined such |
| 8149 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 8150 | // any function G, and, symmetrically, ICS1(G) is neither |
| 8151 | // better nor worse than ICS1(F). |
| 8152 | unsigned StartArg = 0; |
| 8153 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 8154 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8155 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8156 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8157 | // A viable function F1 is defined to be a better function than another |
| 8158 | // 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] | 8159 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8160 | unsigned NumArgs = Cand1.NumConversions; |
| 8161 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8162 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8163 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8164 | switch (CompareImplicitConversionSequences(S, |
| 8165 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8166 | Cand2.Conversions[ArgIdx])) { |
| 8167 | case ImplicitConversionSequence::Better: |
| 8168 | // Cand1 has a better conversion sequence. |
| 8169 | HasBetterConversion = true; |
| 8170 | break; |
| 8171 | |
| 8172 | case ImplicitConversionSequence::Worse: |
| 8173 | // Cand1 can't be better than Cand2. |
| 8174 | return false; |
| 8175 | |
| 8176 | case ImplicitConversionSequence::Indistinguishable: |
| 8177 | // Do nothing. |
| 8178 | break; |
| 8179 | } |
| 8180 | } |
| 8181 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8182 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8183 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8184 | if (HasBetterConversion) |
| 8185 | return true; |
| 8186 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8187 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8188 | // specialization, or, if not that, |
Douglas Gregor | ce21919b | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 8189 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8190 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 8191 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8192 | |
| 8193 | // -- F1 and F2 are function template specializations, and the function |
| 8194 | // template for F1 is more specialized than the template for F2 |
| 8195 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8196 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 8197 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8198 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8199 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8200 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 8201 | Cand2.Function->getPrimaryTemplate(), |
| 8202 | Loc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8203 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 8204 | : TPOC_Call, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 8205 | Cand1.ExplicitCallArguments, |
| 8206 | Cand2.ExplicitCallArguments)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8207 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8208 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8209 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8210 | // -- the context is an initialization by user-defined conversion |
| 8211 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 8212 | // from the return type of F1 to the destination type (i.e., |
| 8213 | // the type of the entity being initialized) is a better |
| 8214 | // conversion sequence than the standard conversion sequence |
| 8215 | // from the return type of F2 to the destination type. |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8216 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8217 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8218 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 8219 | // First check whether we prefer one of the conversion functions over the |
| 8220 | // other. This only distinguishes the results in non-standard, extension |
| 8221 | // cases such as the conversion from a lambda closure type to a function |
| 8222 | // pointer or block. |
| 8223 | ImplicitConversionSequence::CompareKind FuncResult |
| 8224 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 8225 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 8226 | return FuncResult; |
| 8227 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8228 | switch (CompareStandardConversionSequences(S, |
| 8229 | Cand1.FinalConversion, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8230 | Cand2.FinalConversion)) { |
| 8231 | case ImplicitConversionSequence::Better: |
| 8232 | // Cand1 has a better conversion sequence. |
| 8233 | return true; |
| 8234 | |
| 8235 | case ImplicitConversionSequence::Worse: |
| 8236 | // Cand1 can't be better than Cand2. |
| 8237 | return false; |
| 8238 | |
| 8239 | case ImplicitConversionSequence::Indistinguishable: |
| 8240 | // Do nothing |
| 8241 | break; |
| 8242 | } |
| 8243 | } |
| 8244 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 8245 | // Check for enable_if value-based overload resolution. |
| 8246 | if (Cand1.Function && Cand2.Function && |
| 8247 | (Cand1.Function->hasAttr<EnableIfAttr>() || |
| 8248 | Cand2.Function->hasAttr<EnableIfAttr>())) { |
| 8249 | // FIXME: The next several lines are just |
| 8250 | // specific_attr_iterator<EnableIfAttr> but going in declaration order, |
| 8251 | // instead of reverse order which is how they're stored in the AST. |
| 8252 | AttrVec Cand1Attrs; |
| 8253 | AttrVec::iterator Cand1E = Cand1Attrs.end(); |
| 8254 | if (Cand1.Function->hasAttrs()) { |
| 8255 | Cand1Attrs = Cand1.Function->getAttrs(); |
| 8256 | Cand1E = std::remove_if(Cand1Attrs.begin(), Cand1Attrs.end(), |
| 8257 | IsNotEnableIfAttr); |
| 8258 | std::reverse(Cand1Attrs.begin(), Cand1E); |
| 8259 | } |
| 8260 | |
| 8261 | AttrVec Cand2Attrs; |
| 8262 | AttrVec::iterator Cand2E = Cand2Attrs.end(); |
| 8263 | if (Cand2.Function->hasAttrs()) { |
| 8264 | Cand2Attrs = Cand2.Function->getAttrs(); |
| 8265 | Cand2E = std::remove_if(Cand2Attrs.begin(), Cand2Attrs.end(), |
| 8266 | IsNotEnableIfAttr); |
| 8267 | std::reverse(Cand2Attrs.begin(), Cand2E); |
| 8268 | } |
| 8269 | for (AttrVec::iterator |
| 8270 | Cand1I = Cand1Attrs.begin(), Cand2I = Cand2Attrs.begin(); |
| 8271 | Cand1I != Cand1E || Cand2I != Cand2E; ++Cand1I, ++Cand2I) { |
| 8272 | if (Cand1I == Cand1E) |
| 8273 | return false; |
| 8274 | if (Cand2I == Cand2E) |
| 8275 | return true; |
| 8276 | llvm::FoldingSetNodeID Cand1ID, Cand2ID; |
| 8277 | cast<EnableIfAttr>(*Cand1I)->getCond()->Profile(Cand1ID, |
| 8278 | S.getASTContext(), true); |
| 8279 | cast<EnableIfAttr>(*Cand2I)->getCond()->Profile(Cand2ID, |
| 8280 | S.getASTContext(), true); |
Nick Lewycky | d950ae7 | 2014-01-21 01:30:30 +0000 | [diff] [blame] | 8281 | if (Cand1ID != Cand2ID) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 8282 | return false; |
| 8283 | } |
| 8284 | } |
| 8285 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8286 | return false; |
| 8287 | } |
| 8288 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8289 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8290 | /// within an overload candidate set. |
| 8291 | /// |
James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8292 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8293 | /// which overload resolution occurs. |
| 8294 | /// |
James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8295 | /// \param Best If overload resolution was successful or found a deleted |
| 8296 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8297 | /// |
| 8298 | /// \returns The result of overload resolution. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8299 | OverloadingResult |
| 8300 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8301 | iterator &Best, |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8302 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8303 | // Find the best viable function. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8304 | Best = end(); |
| 8305 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8306 | if (Cand->Viable) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8307 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8308 | UserDefinedConversion)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8309 | Best = Cand; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8310 | } |
| 8311 | |
| 8312 | // If we didn't find any viable functions, abort. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8313 | if (Best == end()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8314 | return OR_No_Viable_Function; |
| 8315 | |
| 8316 | // Make sure that this function is better than every other viable |
| 8317 | // function. If not, we have an ambiguity. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8318 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8319 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8320 | Cand != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8321 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8322 | UserDefinedConversion)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8323 | Best = end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8324 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8325 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8326 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8327 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8328 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8329 | if (Best->Function && |
Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8330 | (Best->Function->isDeleted() || |
| 8331 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8332 | return OR_Deleted; |
| 8333 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8334 | return OR_Success; |
| 8335 | } |
| 8336 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8337 | namespace { |
| 8338 | |
| 8339 | enum OverloadCandidateKind { |
| 8340 | oc_function, |
| 8341 | oc_method, |
| 8342 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8343 | oc_function_template, |
| 8344 | oc_method_template, |
| 8345 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8346 | oc_implicit_default_constructor, |
| 8347 | oc_implicit_copy_constructor, |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8348 | oc_implicit_move_constructor, |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8349 | oc_implicit_copy_assignment, |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8350 | oc_implicit_move_assignment, |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8351 | oc_implicit_inherited_constructor |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8352 | }; |
| 8353 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8354 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8355 | FunctionDecl *Fn, |
| 8356 | std::string &Description) { |
| 8357 | bool isTemplate = false; |
| 8358 | |
| 8359 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8360 | isTemplate = true; |
| 8361 | Description = S.getTemplateArgumentBindingsText( |
| 8362 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8363 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8364 | |
| 8365 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8366 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8367 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8368 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8369 | if (Ctor->getInheritedConstructor()) |
| 8370 | return oc_implicit_inherited_constructor; |
| 8371 | |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8372 | if (Ctor->isDefaultConstructor()) |
| 8373 | return oc_implicit_default_constructor; |
| 8374 | |
| 8375 | if (Ctor->isMoveConstructor()) |
| 8376 | return oc_implicit_move_constructor; |
| 8377 | |
| 8378 | assert(Ctor->isCopyConstructor() && |
| 8379 | "unexpected sort of implicit constructor"); |
| 8380 | return oc_implicit_copy_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8381 | } |
| 8382 | |
| 8383 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8384 | // This actually gets spelled 'candidate function' for now, but |
| 8385 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8386 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8387 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8388 | |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8389 | if (Meth->isMoveAssignmentOperator()) |
| 8390 | return oc_implicit_move_assignment; |
| 8391 | |
Douglas Gregor | 1269510 | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8392 | if (Meth->isCopyAssignmentOperator()) |
| 8393 | return oc_implicit_copy_assignment; |
| 8394 | |
| 8395 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8396 | return oc_method; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8397 | } |
| 8398 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8399 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8400 | } |
| 8401 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8402 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8403 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8404 | if (!Ctor) return; |
| 8405 | |
| 8406 | Ctor = Ctor->getInheritedConstructor(); |
| 8407 | if (!Ctor) return; |
| 8408 | |
| 8409 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8410 | } |
| 8411 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8412 | } // end anonymous namespace |
| 8413 | |
| 8414 | // Notes the location of an overload candidate. |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8415 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8416 | std::string FnDesc; |
| 8417 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8418 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8419 | << (unsigned) K << FnDesc; |
| 8420 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8421 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8422 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8423 | } |
| 8424 | |
Nick Lewycky | ed4265c | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 8425 | // Notes the location of all overload candidates designated through |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8426 | // OverloadedExpr |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8427 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8428 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8429 | |
| 8430 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8431 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8432 | |
| 8433 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8434 | IEnd = OvlExpr->decls_end(); |
| 8435 | I != IEnd; ++I) { |
| 8436 | if (FunctionTemplateDecl *FunTmpl = |
| 8437 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8438 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8439 | } else if (FunctionDecl *Fun |
| 8440 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8441 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8442 | } |
| 8443 | } |
| 8444 | } |
| 8445 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8446 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8447 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8448 | /// target types of the conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8449 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8450 | Sema &S, |
| 8451 | SourceLocation CaretLoc, |
| 8452 | const PartialDiagnostic &PDiag) const { |
| 8453 | S.Diag(CaretLoc, PDiag) |
| 8454 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 641bd89 | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8455 | // FIXME: The note limiting machinery is borrowed from |
| 8456 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8457 | // refactoring here. |
| 8458 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8459 | unsigned CandsShown = 0; |
| 8460 | AmbiguousConversionSequence::const_iterator I, E; |
| 8461 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8462 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8463 | break; |
| 8464 | ++CandsShown; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8465 | S.NoteOverloadCandidate(*I); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8466 | } |
Matt Beaumont-Gay | 641bd89 | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8467 | if (I != E) |
| 8468 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8469 | } |
| 8470 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8471 | namespace { |
| 8472 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8473 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8474 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8475 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8476 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8477 | FunctionDecl *Fn = Cand->Function; |
| 8478 | |
| 8479 | // There's a conversion slot for the object argument if this is a |
| 8480 | // non-constructor method. Note that 'I' corresponds the |
| 8481 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8482 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8483 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8484 | if (I == 0) |
| 8485 | isObjectArgument = true; |
| 8486 | else |
| 8487 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8488 | } |
| 8489 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8490 | std::string FnDesc; |
| 8491 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8492 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8493 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8494 | QualType FromTy = Conv.Bad.getFromType(); |
| 8495 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8496 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8497 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8498 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8499 | Expr *E = FromExpr->IgnoreParens(); |
| 8500 | if (isa<UnaryOperator>(E)) |
| 8501 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8502 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8503 | |
| 8504 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8505 | << (unsigned) FnKind << FnDesc |
| 8506 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8507 | << ToTy << Name << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8508 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8509 | return; |
| 8510 | } |
| 8511 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8512 | // Do some hand-waving analysis to see if the non-viability is due |
| 8513 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8514 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8515 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8516 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8517 | CToTy = RT->getPointeeType(); |
| 8518 | else { |
| 8519 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8520 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8521 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8522 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8523 | } |
| 8524 | |
| 8525 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8526 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8527 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8528 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8529 | |
| 8530 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8531 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8532 | << (unsigned) FnKind << FnDesc |
| 8533 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8534 | << FromTy |
| 8535 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8536 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8537 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8538 | return; |
| 8539 | } |
| 8540 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8541 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8542 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8543 | << (unsigned) FnKind << FnDesc |
| 8544 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8545 | << FromTy |
| 8546 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8547 | << (unsigned) isObjectArgument << I+1; |
| 8548 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8549 | return; |
| 8550 | } |
| 8551 | |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8552 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8553 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8554 | << (unsigned) FnKind << FnDesc |
| 8555 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8556 | << FromTy |
| 8557 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8558 | << (unsigned) isObjectArgument << I+1; |
| 8559 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8560 | return; |
| 8561 | } |
| 8562 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8563 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8564 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8565 | |
| 8566 | if (isObjectArgument) { |
| 8567 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8568 | << (unsigned) FnKind << FnDesc |
| 8569 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8570 | << FromTy << (CVR - 1); |
| 8571 | } else { |
| 8572 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8573 | << (unsigned) FnKind << FnDesc |
| 8574 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8575 | << FromTy << (CVR - 1) << I+1; |
| 8576 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8577 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8578 | return; |
| 8579 | } |
| 8580 | |
Sebastian Redl | a72462c | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8581 | // Special diagnostic for failure to convert an initializer list, since |
| 8582 | // telling the user that it has type void is not useful. |
| 8583 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8584 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8585 | << (unsigned) FnKind << FnDesc |
| 8586 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8587 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8588 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8589 | return; |
| 8590 | } |
| 8591 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8592 | // Diagnose references or pointers to incomplete types differently, |
| 8593 | // since it's far from impossible that the incompleteness triggered |
| 8594 | // the failure. |
| 8595 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8596 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8597 | TempFromTy = PTy->getPointeeType(); |
| 8598 | if (TempFromTy->isIncompleteType()) { |
| 8599 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8600 | << (unsigned) FnKind << FnDesc |
| 8601 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8602 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8603 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8604 | return; |
| 8605 | } |
| 8606 | |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8607 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8608 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8609 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8610 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8611 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8612 | FromPtrTy->getPointeeType()) && |
| 8613 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8614 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8615 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8616 | FromPtrTy->getPointeeType())) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8617 | BaseToDerivedConversion = 1; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8618 | } |
| 8619 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8620 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8621 | if (const ObjCObjectPointerType *ToPtrTy |
| 8622 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8623 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8624 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8625 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8626 | FromPtrTy->getPointeeType()) && |
| 8627 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8628 | BaseToDerivedConversion = 2; |
| 8629 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8630 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8631 | !FromTy->isIncompleteType() && |
| 8632 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8633 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8634 | BaseToDerivedConversion = 3; |
| 8635 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8636 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8637 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8638 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8639 | << (unsigned) FnKind << FnDesc |
| 8640 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8641 | << (unsigned) isObjectArgument << I + 1; |
| 8642 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8643 | return; |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8644 | } |
Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8645 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8646 | |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8647 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8648 | S.Diag(Fn->getLocation(), |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8649 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8650 | << (unsigned) FnKind << FnDesc |
| 8651 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8652 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8653 | << FromTy << ToTy << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8654 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8655 | return; |
| 8656 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8657 | |
Fariborz Jahanian | a644f9c | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8658 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8659 | isa<PointerType>(CToTy)) { |
| 8660 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8661 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8662 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8663 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8664 | << (unsigned) FnKind << FnDesc |
| 8665 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8666 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8667 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8668 | return; |
| 8669 | } |
| 8670 | } |
| 8671 | |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8672 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8673 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8674 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8675 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8676 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8677 | << (unsigned) (Cand->Fix.Kind); |
| 8678 | |
| 8679 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 490afa6 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8680 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8681 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8682 | FDiag << *HI; |
| 8683 | S.Diag(Fn->getLocation(), FDiag); |
| 8684 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8685 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8686 | } |
| 8687 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8688 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8689 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8690 | /// over a candidate in any candidate set. |
| 8691 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8692 | unsigned NumArgs) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8693 | FunctionDecl *Fn = Cand->Function; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8694 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8695 | |
Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8696 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8697 | // 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] | 8698 | // right number of arguments, because only overloaded operators have |
| 8699 | // the weird behavior of overloading member and non-member functions. |
| 8700 | // Just don't report anything. |
| 8701 | if (Fn->isInvalidDecl() && |
| 8702 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8703 | return true; |
| 8704 | |
| 8705 | if (NumArgs < MinParams) { |
| 8706 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8707 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8708 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8709 | } else { |
| 8710 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8711 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8712 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8713 | } |
| 8714 | |
| 8715 | return false; |
| 8716 | } |
| 8717 | |
| 8718 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8719 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8720 | assert(isa<FunctionDecl>(D) && |
| 8721 | "The templated declaration should at least be a function" |
| 8722 | " when diagnosing bad template argument deduction due to too many" |
| 8723 | " or too few arguments"); |
| 8724 | |
| 8725 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8726 | |
| 8727 | // TODO: treat calls to a missing default constructor as a special case |
| 8728 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8729 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8730 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8731 | // at least / at most / exactly |
| 8732 | unsigned mode, modeCount; |
| 8733 | if (NumFormalArgs < MinParams) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8734 | if (MinParams != FnTy->getNumParams() || FnTy->isVariadic() || |
| 8735 | FnTy->isTemplateVariadic()) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8736 | mode = 0; // "at least" |
| 8737 | else |
| 8738 | mode = 2; // "exactly" |
| 8739 | modeCount = MinParams; |
| 8740 | } else { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8741 | if (MinParams != FnTy->getNumParams()) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8742 | mode = 1; // "at most" |
| 8743 | else |
| 8744 | mode = 2; // "exactly" |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8745 | modeCount = FnTy->getNumParams(); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | std::string Description; |
| 8749 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8750 | |
Richard Smith | 10ff50d | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8751 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8752 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8753 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8754 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8755 | else |
| 8756 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8757 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8758 | << modeCount << NumFormalArgs; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8759 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8760 | } |
| 8761 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8762 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8763 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8764 | unsigned NumFormalArgs) { |
| 8765 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8766 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8767 | } |
Larisse Voufo | 47c0845 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8768 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8769 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8770 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8771 | return FD->getDescribedFunctionTemplate(); |
| 8772 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8773 | return RD->getDescribedClassTemplate(); |
| 8774 | |
| 8775 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8776 | " for bad deduction diagnosis"); |
| 8777 | } |
| 8778 | |
| 8779 | /// Diagnose a failed template-argument deduction. |
| 8780 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8781 | DeductionFailureInfo &DeductionFailure, |
| 8782 | unsigned NumArgs) { |
| 8783 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8784 | NamedDecl *ParamD; |
| 8785 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8786 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8787 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8788 | switch (DeductionFailure.Result) { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8789 | case Sema::TDK_Success: |
| 8790 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8791 | |
| 8792 | case Sema::TDK_Incomplete: { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8793 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8794 | S.Diag(Templated->getLocation(), |
| 8795 | diag::note_ovl_candidate_incomplete_deduction) |
| 8796 | << ParamD->getDeclName(); |
| 8797 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8798 | return; |
| 8799 | } |
| 8800 | |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8801 | case Sema::TDK_Underqualified: { |
| 8802 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8803 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8804 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8805 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8806 | |
| 8807 | // Param will have been canonicalized, but it should just be a |
| 8808 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8809 | QualifierCollector Qs; |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8810 | Qs.strip(Param); |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8811 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8812 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8813 | |
| 8814 | // Arg has also been canonicalized, but there's nothing we can do |
| 8815 | // about that. It also doesn't matter as much, because it won't |
| 8816 | // have any template parameters in it (because deduction isn't |
| 8817 | // done on dependent types). |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8818 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8819 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8820 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8821 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8822 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8823 | return; |
| 8824 | } |
| 8825 | |
| 8826 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8827 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8828 | int which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8829 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8830 | which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8831 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8832 | which = 1; |
| 8833 | else { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8834 | which = 2; |
| 8835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8836 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8837 | S.Diag(Templated->getLocation(), |
| 8838 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8839 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8840 | << *DeductionFailure.getSecondArg(); |
| 8841 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8842 | return; |
| 8843 | } |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8844 | |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8845 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8846 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8847 | if (ParamD->getDeclName()) |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8848 | S.Diag(Templated->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8849 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8850 | << ParamD->getDeclName(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8851 | else { |
| 8852 | int index = 0; |
| 8853 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8854 | index = TTP->getIndex(); |
| 8855 | else if (NonTypeTemplateParmDecl *NTTP |
| 8856 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8857 | index = NTTP->getIndex(); |
| 8858 | else |
| 8859 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8860 | S.Diag(Templated->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8861 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8862 | << (index + 1); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8863 | } |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8864 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8865 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8866 | |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8867 | case Sema::TDK_TooManyArguments: |
| 8868 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8869 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8870 | return; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8871 | |
| 8872 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8873 | S.Diag(Templated->getLocation(), |
| 8874 | diag::note_ovl_candidate_instantiation_depth); |
| 8875 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8876 | return; |
| 8877 | |
| 8878 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8879 | // Format the template argument list into the argument string. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8880 | SmallString<128> TemplateArgString; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8881 | if (TemplateArgumentList *Args = |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8882 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8883 | TemplateArgString = " "; |
| 8884 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8885 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8886 | } |
| 8887 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8888 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8889 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8890 | if (PDiag && PDiag->second.getDiagID() == |
| 8891 | diag::err_typename_nested_not_found_enable_if) { |
| 8892 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8893 | // name of the enable_if template. These are both present in PDiag. |
| 8894 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8895 | << "'enable_if'" << TemplateArgString; |
| 8896 | return; |
| 8897 | } |
| 8898 | |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8899 | // Format the SFINAE diagnostic into the argument string. |
| 8900 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8901 | // formatted message in another diagnostic. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8902 | SmallString<128> SFINAEArgString; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8903 | SourceRange R; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8904 | if (PDiag) { |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8905 | SFINAEArgString = ": "; |
| 8906 | R = SourceRange(PDiag->first, PDiag->first); |
| 8907 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8908 | } |
| 8909 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8910 | S.Diag(Templated->getLocation(), |
| 8911 | diag::note_ovl_candidate_substitution_failure) |
| 8912 | << TemplateArgString << SFINAEArgString << R; |
| 8913 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8914 | return; |
| 8915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8916 | |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8917 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8918 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8919 | S.Diag(Templated->getLocation(), |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8920 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8921 | << R.Expression->getName(); |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8922 | return; |
| 8923 | } |
| 8924 | |
Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8925 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8926 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8927 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8928 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8929 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8930 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8931 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8932 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8933 | if (FirstTN.getKind() == TemplateName::Template && |
| 8934 | SecondTN.getKind() == TemplateName::Template) { |
| 8935 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8936 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8937 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8938 | // the same. This particular case is a bit difficult since: |
| 8939 | // 1) It is passed as a string to the diagnostic printer. |
| 8940 | // 2) The diagnostic printer only attempts to find a better |
| 8941 | // name for types, not decls. |
| 8942 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8943 | S.Diag(Templated->getLocation(), |
Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8944 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8945 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8946 | return; |
| 8947 | } |
| 8948 | } |
| 8949 | } |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 8950 | // FIXME: For generic lambda parameters, check if the function is a lambda |
| 8951 | // call operator, and if so, emit a prettier and more informative |
| 8952 | // diagnostic that mentions 'auto' and lambda in addition to |
| 8953 | // (or instead of?) the canonical template type parameters. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8954 | S.Diag(Templated->getLocation(), |
| 8955 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8956 | << FirstTA << SecondTA; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8957 | return; |
Richard Trieu | e373235 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8958 | } |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8959 | // TODO: diagnose these individually, then kill off |
| 8960 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8961 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8962 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8963 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8964 | return; |
| 8965 | } |
| 8966 | } |
| 8967 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8968 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8969 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8970 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8971 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8972 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8973 | return; |
| 8974 | } |
| 8975 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8976 | Cand->DeductionFailure, NumArgs); |
| 8977 | } |
| 8978 | |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8979 | /// CUDA: diagnose an invalid call across targets. |
| 8980 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8981 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8982 | FunctionDecl *Callee = Cand->Function; |
| 8983 | |
| 8984 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8985 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8986 | |
| 8987 | std::string FnDesc; |
| 8988 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8989 | |
| 8990 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8991 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8992 | } |
| 8993 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 8994 | void DiagnoseFailedEnableIfAttr(Sema &S, OverloadCandidate *Cand) { |
| 8995 | FunctionDecl *Callee = Cand->Function; |
| 8996 | EnableIfAttr *Attr = static_cast<EnableIfAttr*>(Cand->DeductionFailure.Data); |
| 8997 | |
| 8998 | S.Diag(Callee->getLocation(), |
| 8999 | diag::note_ovl_candidate_disabled_by_enable_if_attr) |
| 9000 | << Attr->getCond()->getSourceRange() << Attr->getMessage(); |
| 9001 | } |
| 9002 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9003 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 9004 | /// already generated a primary error at the call site. |
| 9005 | /// |
| 9006 | /// It really does need to be a single diagnostic with its caret |
| 9007 | /// pointed at the candidate declaration. Yes, this creates some |
| 9008 | /// major challenges of technical writing. Yes, this makes pointing |
| 9009 | /// out problems with specific arguments quite awkward. It's still |
| 9010 | /// better than generating twenty screens of text for every failed |
| 9011 | /// overload. |
| 9012 | /// |
| 9013 | /// It would be great to be able to express per-candidate problems |
| 9014 | /// more richly for those diagnostic clients that cared, but we'd |
| 9015 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9016 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9017 | unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9018 | FunctionDecl *Fn = Cand->Function; |
| 9019 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9020 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 9021 | if (Cand->Viable && (Fn->isDeleted() || |
| 9022 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9023 | std::string FnDesc; |
| 9024 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 9025 | |
| 9026 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 9027 | << FnKind << FnDesc |
| 9028 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9029 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9030 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9031 | } |
| 9032 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9033 | // We don't really have anything else to say about viable candidates. |
| 9034 | if (Cand->Viable) { |
| 9035 | S.NoteOverloadCandidate(Fn); |
| 9036 | return; |
| 9037 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9038 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9039 | switch (Cand->FailureKind) { |
| 9040 | case ovl_fail_too_many_arguments: |
| 9041 | case ovl_fail_too_few_arguments: |
| 9042 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9043 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9044 | case ovl_fail_bad_deduction: |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9045 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 9046 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9047 | case ovl_fail_trivial_conversion: |
| 9048 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 9049 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9050 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9051 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9052 | case ovl_fail_bad_conversion: { |
| 9053 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9054 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9055 | if (Cand->Conversions[I].isBad()) |
| 9056 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9057 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 9058 | // FIXME: this currently happens when we're called from SemaInit |
| 9059 | // when user-conversion overload fails. Figure out how to handle |
| 9060 | // those conditions and diagnose them well. |
| 9061 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 9062 | } |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9063 | |
| 9064 | case ovl_fail_bad_target: |
| 9065 | return DiagnoseBadTarget(S, Cand); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9066 | |
| 9067 | case ovl_fail_enable_if: |
| 9068 | return DiagnoseFailedEnableIfAttr(S, Cand); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9069 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9070 | } |
| 9071 | |
| 9072 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 9073 | // Desugar the type of the surrogate down to a function type, |
| 9074 | // retaining as many typedefs as possible while still showing |
| 9075 | // the function type (and, therefore, its parameter types). |
| 9076 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 9077 | bool isLValueReference = false; |
| 9078 | bool isRValueReference = false; |
| 9079 | bool isPointer = false; |
| 9080 | if (const LValueReferenceType *FnTypeRef = |
| 9081 | FnType->getAs<LValueReferenceType>()) { |
| 9082 | FnType = FnTypeRef->getPointeeType(); |
| 9083 | isLValueReference = true; |
| 9084 | } else if (const RValueReferenceType *FnTypeRef = |
| 9085 | FnType->getAs<RValueReferenceType>()) { |
| 9086 | FnType = FnTypeRef->getPointeeType(); |
| 9087 | isRValueReference = true; |
| 9088 | } |
| 9089 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 9090 | FnType = FnTypePtr->getPointeeType(); |
| 9091 | isPointer = true; |
| 9092 | } |
| 9093 | // Desugar down to a function type. |
| 9094 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 9095 | // Reconstruct the pointer/reference as appropriate. |
| 9096 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 9097 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 9098 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 9099 | |
| 9100 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 9101 | << FnType; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9102 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9103 | } |
| 9104 | |
| 9105 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 1d202a6 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9106 | StringRef Opc, |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9107 | SourceLocation OpLoc, |
| 9108 | OverloadCandidate *Cand) { |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9109 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9110 | std::string TypeStr("operator"); |
| 9111 | TypeStr += Opc; |
| 9112 | TypeStr += "("; |
| 9113 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9114 | if (Cand->NumConversions == 1) { |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9115 | TypeStr += ")"; |
| 9116 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 9117 | } else { |
| 9118 | TypeStr += ", "; |
| 9119 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 9120 | TypeStr += ")"; |
| 9121 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 9122 | } |
| 9123 | } |
| 9124 | |
| 9125 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 9126 | OverloadCandidate *Cand) { |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9127 | unsigned NoOperands = Cand->NumConversions; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9128 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 9129 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9130 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 9131 | if (!ICS.isAmbiguous()) continue; |
| 9132 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9133 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9134 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9135 | } |
| 9136 | } |
| 9137 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9138 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9139 | if (Cand->Function) |
| 9140 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 9141 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9142 | return Cand->Surrogate->getLocation(); |
| 9143 | return SourceLocation(); |
| 9144 | } |
| 9145 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9146 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 73fddfe | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 9147 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9148 | case Sema::TDK_Success: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9149 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 9150 | |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 9151 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9152 | case Sema::TDK_Incomplete: |
| 9153 | return 1; |
| 9154 | |
| 9155 | case Sema::TDK_Underqualified: |
| 9156 | case Sema::TDK_Inconsistent: |
| 9157 | return 2; |
| 9158 | |
| 9159 | case Sema::TDK_SubstitutionFailure: |
| 9160 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 9161 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9162 | return 3; |
| 9163 | |
| 9164 | case Sema::TDK_InstantiationDepth: |
| 9165 | case Sema::TDK_FailedOverloadResolution: |
| 9166 | return 4; |
| 9167 | |
| 9168 | case Sema::TDK_InvalidExplicitArguments: |
| 9169 | return 5; |
| 9170 | |
| 9171 | case Sema::TDK_TooManyArguments: |
| 9172 | case Sema::TDK_TooFewArguments: |
| 9173 | return 6; |
| 9174 | } |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 9175 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9176 | } |
| 9177 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9178 | struct CompareOverloadCandidatesForDisplay { |
| 9179 | Sema &S; |
| 9180 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9181 | |
| 9182 | bool operator()(const OverloadCandidate *L, |
| 9183 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 9184 | // Fast-path this check. |
| 9185 | if (L == R) return false; |
| 9186 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9187 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9188 | if (L->Viable) { |
| 9189 | if (!R->Viable) return true; |
| 9190 | |
| 9191 | // TODO: introduce a tri-valued comparison for overload |
| 9192 | // candidates. Would be more worthwhile if we had a sort |
| 9193 | // that could exploit it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9194 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 9195 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9196 | } else if (R->Viable) |
| 9197 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9198 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9199 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9200 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9201 | // Criteria by which we can sort non-viable candidates: |
| 9202 | if (!L->Viable) { |
| 9203 | // 1. Arity mismatches come after other candidates. |
| 9204 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 9205 | L->FailureKind == ovl_fail_too_few_arguments) |
| 9206 | return false; |
| 9207 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 9208 | R->FailureKind == ovl_fail_too_few_arguments) |
| 9209 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9210 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9211 | // 2. Bad conversions come first and are ordered by the number |
| 9212 | // of bad conversions and quality of good conversions. |
| 9213 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 9214 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 9215 | return true; |
| 9216 | |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9217 | // The conversion that can be fixed with a smaller number of changes, |
| 9218 | // comes first. |
| 9219 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 9220 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 9221 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 9222 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | 9ccf84e | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 9223 | if (numLFixes != numRFixes) { |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9224 | if (numLFixes < numRFixes) |
| 9225 | return true; |
| 9226 | else |
| 9227 | return false; |
Anna Zaks | 9ccf84e | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 9228 | } |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9229 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9230 | // If there's any ordering between the defined conversions... |
| 9231 | // FIXME: this might not be transitive. |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9232 | assert(L->NumConversions == R->NumConversions); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9233 | |
| 9234 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 9235 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9236 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9237 | switch (CompareImplicitConversionSequences(S, |
| 9238 | L->Conversions[I], |
| 9239 | R->Conversions[I])) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9240 | case ImplicitConversionSequence::Better: |
| 9241 | leftBetter++; |
| 9242 | break; |
| 9243 | |
| 9244 | case ImplicitConversionSequence::Worse: |
| 9245 | leftBetter--; |
| 9246 | break; |
| 9247 | |
| 9248 | case ImplicitConversionSequence::Indistinguishable: |
| 9249 | break; |
| 9250 | } |
| 9251 | } |
| 9252 | if (leftBetter > 0) return true; |
| 9253 | if (leftBetter < 0) return false; |
| 9254 | |
| 9255 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 9256 | return false; |
| 9257 | |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9258 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 9259 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 9260 | return true; |
| 9261 | |
| 9262 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9263 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | 1e7a0c6 | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 9264 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | e2c600c | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 9265 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 9266 | return false; |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9267 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9268 | // TODO: others? |
| 9269 | } |
| 9270 | |
| 9271 | // Sort everything else by location. |
| 9272 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9273 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9274 | |
| 9275 | // Put candidates without locations (e.g. builtins) at the end. |
| 9276 | if (LLoc.isInvalid()) return false; |
| 9277 | if (RLoc.isInvalid()) return true; |
| 9278 | |
| 9279 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9280 | } |
| 9281 | }; |
| 9282 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9283 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9284 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9285 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9286 | ArrayRef<Expr *> Args) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9287 | assert(!Cand->Viable); |
| 9288 | |
| 9289 | // Don't do anything on failures other than bad conversion. |
| 9290 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9291 | |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9292 | // We only want the FixIts if all the arguments can be corrected. |
| 9293 | bool Unfixable = false; |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9294 | // Use a implicit copy initialization to check conversion fixes. |
| 9295 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9296 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9297 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9298 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9299 | unsigned ConvCount = Cand->NumConversions; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9300 | while (true) { |
| 9301 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9302 | ConvIdx++; |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9303 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9304 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9305 | break; |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9306 | } |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9307 | } |
| 9308 | |
| 9309 | if (ConvIdx == ConvCount) |
| 9310 | return; |
| 9311 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9312 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9313 | "remaining conversion is initialized?"); |
| 9314 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9315 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9316 | // operation somehow. |
| 9317 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9318 | |
| 9319 | const FunctionProtoType* Proto; |
| 9320 | unsigned ArgIdx = ConvIdx; |
| 9321 | |
| 9322 | if (Cand->IsSurrogate) { |
| 9323 | QualType ConvType |
| 9324 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9325 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9326 | ConvType = ConvPtrType->getPointeeType(); |
| 9327 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9328 | ArgIdx--; |
| 9329 | } else if (Cand->Function) { |
| 9330 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9331 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9332 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9333 | ArgIdx--; |
| 9334 | } else { |
| 9335 | // Builtin binary operator with a bad first conversion. |
| 9336 | assert(ConvCount <= 3); |
| 9337 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9338 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9339 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9340 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9341 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9342 | /*InOverloadResolution*/ true, |
| 9343 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9344 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9345 | return; |
| 9346 | } |
| 9347 | |
| 9348 | // Fill in the rest of the conversions. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 9349 | unsigned NumParams = Proto->getNumParams(); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9350 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 9351 | if (ArgIdx < NumParams) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9352 | Cand->Conversions[ConvIdx] = TryCopyInitialization( |
| 9353 | S, Args[ArgIdx], Proto->getParamType(ArgIdx), SuppressUserConversions, |
| 9354 | /*InOverloadResolution=*/true, |
| 9355 | /*AllowObjCWritebackConversion=*/ |
| 9356 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9357 | // Store the FixIt in the candidate if it exists. |
| 9358 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9359 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9360 | } |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9361 | else |
| 9362 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9363 | } |
| 9364 | } |
| 9365 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9366 | } // end anonymous namespace |
| 9367 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9368 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9369 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9370 | /// set. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9371 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9372 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9373 | ArrayRef<Expr *> Args, |
David Blaikie | 1d202a6 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9374 | StringRef Opc, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9375 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9376 | // Sort the candidates by viability and position. Sorting directly would |
| 9377 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9378 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9379 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9380 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9381 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9382 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9383 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9384 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9385 | if (Cand->Function || Cand->IsSurrogate) |
| 9386 | Cands.push_back(Cand); |
| 9387 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9388 | // want to list every possible builtin candidate. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9389 | } |
| 9390 | } |
| 9391 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9392 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9393 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9394 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9395 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9396 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9397 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | 7959178 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9398 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9399 | unsigned CandsShown = 0; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9400 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9401 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9402 | |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9403 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9404 | // the user with. FIXME: This limit should depend on details of the |
| 9405 | // candidate list. |
Douglas Gregor | 7959178 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9406 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9407 | break; |
| 9408 | } |
| 9409 | ++CandsShown; |
| 9410 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9411 | if (Cand->Function) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9412 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9413 | else if (Cand->IsSurrogate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9414 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9415 | else { |
| 9416 | assert(Cand->Viable && |
| 9417 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9418 | // Generally we only see ambiguities including viable builtin |
| 9419 | // operators if overload resolution got screwed up by an |
| 9420 | // ambiguous user-defined conversion. |
| 9421 | // |
| 9422 | // FIXME: It's quite possible for different conversions to see |
| 9423 | // different ambiguities, though. |
| 9424 | if (!ReportedAmbiguousConversions) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9425 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9426 | ReportedAmbiguousConversions = true; |
| 9427 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9428 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9429 | // If this is a viable builtin, print it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9430 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9431 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9432 | } |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9433 | |
| 9434 | if (I != E) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9435 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9436 | } |
| 9437 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9438 | static SourceLocation |
| 9439 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9440 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9441 | : SourceLocation(); |
| 9442 | } |
| 9443 | |
| 9444 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9445 | Sema &S; |
| 9446 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9447 | |
| 9448 | bool operator()(const TemplateSpecCandidate *L, |
| 9449 | const TemplateSpecCandidate *R) { |
| 9450 | // Fast-path this check. |
| 9451 | if (L == R) |
| 9452 | return false; |
| 9453 | |
| 9454 | // Assuming that both candidates are not matches... |
| 9455 | |
| 9456 | // Sort by the ranking of deduction failures. |
| 9457 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9458 | return RankDeductionFailure(L->DeductionFailure) < |
| 9459 | RankDeductionFailure(R->DeductionFailure); |
| 9460 | |
| 9461 | // Sort everything else by location. |
| 9462 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9463 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9464 | |
| 9465 | // Put candidates without locations (e.g. builtins) at the end. |
| 9466 | if (LLoc.isInvalid()) |
| 9467 | return false; |
| 9468 | if (RLoc.isInvalid()) |
| 9469 | return true; |
| 9470 | |
| 9471 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9472 | } |
| 9473 | }; |
| 9474 | |
| 9475 | /// Diagnose a template argument deduction failure. |
| 9476 | /// We are treating these failures as overload failures due to bad |
| 9477 | /// deductions. |
| 9478 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9479 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9480 | DeductionFailure, /*NumArgs=*/0); |
| 9481 | } |
| 9482 | |
| 9483 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9484 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9485 | i->DeductionFailure.Destroy(); |
| 9486 | } |
| 9487 | } |
| 9488 | |
| 9489 | void TemplateSpecCandidateSet::clear() { |
| 9490 | destroyCandidates(); |
| 9491 | Candidates.clear(); |
| 9492 | } |
| 9493 | |
| 9494 | /// NoteCandidates - When no template specialization match is found, prints |
| 9495 | /// diagnostic messages containing the non-matching specializations that form |
| 9496 | /// the candidate set. |
| 9497 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9498 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9499 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9500 | // Sort the candidates by position (assuming no candidate is a match). |
| 9501 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9502 | // and sort those. |
| 9503 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9504 | Cands.reserve(size()); |
| 9505 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9506 | if (Cand->Specialization) |
| 9507 | Cands.push_back(Cand); |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 9508 | // Otherwise, this is a non-matching builtin candidate. We do not, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9509 | // in general, want to list every possible builtin candidate. |
| 9510 | } |
| 9511 | |
| 9512 | std::sort(Cands.begin(), Cands.end(), |
| 9513 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9514 | |
| 9515 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9516 | // for generalization purposes (?). |
| 9517 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9518 | |
| 9519 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9520 | unsigned CandsShown = 0; |
| 9521 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9522 | TemplateSpecCandidate *Cand = *I; |
| 9523 | |
| 9524 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9525 | // the user with. FIXME: This limit should depend on details of the |
| 9526 | // candidate list. |
| 9527 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9528 | break; |
| 9529 | ++CandsShown; |
| 9530 | |
| 9531 | assert(Cand->Specialization && |
| 9532 | "Non-matching built-in candidates are not added to Cands."); |
| 9533 | Cand->NoteDeductionFailure(S); |
| 9534 | } |
| 9535 | |
| 9536 | if (I != E) |
| 9537 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9538 | } |
| 9539 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9540 | // [PossiblyAFunctionType] --> [Return] |
| 9541 | // NonFunctionType --> NonFunctionType |
| 9542 | // R (A) --> R(A) |
| 9543 | // R (*)(A) --> R (A) |
| 9544 | // R (&)(A) --> R (A) |
| 9545 | // R (S::*)(A) --> R (A) |
| 9546 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9547 | QualType Ret = PossiblyAFunctionType; |
| 9548 | if (const PointerType *ToTypePtr = |
| 9549 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9550 | Ret = ToTypePtr->getPointeeType(); |
| 9551 | else if (const ReferenceType *ToTypeRef = |
| 9552 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9553 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9554 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9555 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9556 | Ret = MemTypePtr->getPointeeType(); |
| 9557 | Ret = |
| 9558 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9559 | return Ret; |
| 9560 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9561 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9562 | // A helper class to help with address of function resolution |
| 9563 | // - allows us to avoid passing around all those ugly parameters |
| 9564 | class AddressOfFunctionResolver |
| 9565 | { |
| 9566 | Sema& S; |
| 9567 | Expr* SourceExpr; |
| 9568 | const QualType& TargetType; |
| 9569 | QualType TargetFunctionType; // Extracted function type from target type |
| 9570 | |
| 9571 | bool Complain; |
| 9572 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9573 | ASTContext& Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9574 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9575 | bool TargetTypeIsNonStaticMemberFunction; |
| 9576 | bool FoundNonTemplateFunction; |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9577 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9578 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9579 | OverloadExpr::FindResult OvlExprInfo; |
| 9580 | OverloadExpr *OvlExpr; |
| 9581 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9582 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9583 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9584 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9585 | public: |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9586 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9587 | const QualType &TargetType, bool Complain) |
| 9588 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9589 | Complain(Complain), Context(S.getASTContext()), |
| 9590 | TargetTypeIsNonStaticMemberFunction( |
| 9591 | !!TargetType->getAs<MemberPointerType>()), |
| 9592 | FoundNonTemplateFunction(false), |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9593 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9594 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9595 | OvlExpr(OvlExprInfo.Expression), |
| 9596 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9597 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9598 | |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9599 | if (TargetFunctionType->isFunctionType()) { |
| 9600 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9601 | if (!UME->isImplicitAccess() && |
| 9602 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9603 | StaticMemberFunctionFromBoundPointer = true; |
| 9604 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9605 | DeclAccessPair dap; |
| 9606 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9607 | OvlExpr, false, &dap)) { |
| 9608 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9609 | if (!Method->isStatic()) { |
| 9610 | // If the target type is a non-function type and the function found |
| 9611 | // is a non-static member function, pretend as if that was the |
| 9612 | // target, it's the only possible type to end up with. |
| 9613 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9614 | |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9615 | // And skip adding the function if its not in the proper form. |
| 9616 | // We'll diagnose this due to an empty set of functions. |
| 9617 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9618 | return; |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9619 | } |
| 9620 | |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9621 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9622 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9623 | return; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9624 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9625 | |
| 9626 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9627 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9628 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9629 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9630 | // C++ [over.over]p4: |
| 9631 | // If more than one function is selected, [...] |
| 9632 | if (Matches.size() > 1) { |
| 9633 | if (FoundNonTemplateFunction) |
| 9634 | EliminateAllTemplateMatches(); |
| 9635 | else |
| 9636 | EliminateAllExceptMostSpecializedTemplate(); |
| 9637 | } |
| 9638 | } |
| 9639 | } |
| 9640 | |
| 9641 | private: |
| 9642 | bool isTargetTypeAFunction() const { |
| 9643 | return TargetFunctionType->isFunctionType(); |
| 9644 | } |
| 9645 | |
| 9646 | // [ToType] [Return] |
| 9647 | |
| 9648 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9649 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9650 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9651 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9652 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9653 | } |
| 9654 | |
| 9655 | // return true if any matching specializations were found |
| 9656 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9657 | const DeclAccessPair& CurAccessFunPair) { |
| 9658 | if (CXXMethodDecl *Method |
| 9659 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9660 | // Skip non-static function templates when converting to pointer, and |
| 9661 | // static when converting to member pointer. |
| 9662 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9663 | return false; |
| 9664 | } |
| 9665 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9666 | return false; |
| 9667 | |
| 9668 | // C++ [over.over]p2: |
| 9669 | // If the name is a function template, template argument deduction is |
| 9670 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9671 | // resulting template argument list is used to generate a single |
| 9672 | // function template specialization, which is added to the set of |
| 9673 | // overloaded functions considered. |
| 9674 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9675 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9676 | if (Sema::TemplateDeductionResult Result |
| 9677 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9678 | &OvlExplicitTemplateArgs, |
| 9679 | TargetFunctionType, Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9680 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9681 | // Make a note of the failed deduction for diagnostics. |
| 9682 | FailedCandidates.addCandidate() |
| 9683 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9684 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9685 | return false; |
| 9686 | } |
| 9687 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9688 | // Template argument deduction ensures that we have an exact match or |
| 9689 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9690 | // This function template specicalization works. |
| 9691 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9692 | assert(S.isSameOrCompatibleFunctionType( |
| 9693 | Context.getCanonicalType(Specialization->getType()), |
| 9694 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9695 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9696 | return true; |
| 9697 | } |
| 9698 | |
| 9699 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9700 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9701 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9702 | // Skip non-static functions when converting to pointer, and static |
| 9703 | // when converting to member pointer. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9704 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9705 | return false; |
| 9706 | } |
| 9707 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9708 | return false; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9709 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9710 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9711 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9712 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9713 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9714 | return false; |
| 9715 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9716 | // If any candidate has a placeholder return type, trigger its deduction |
| 9717 | // now. |
| 9718 | if (S.getLangOpts().CPlusPlus1y && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9719 | FunDecl->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9720 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9721 | return false; |
| 9722 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9723 | QualType ResultTy; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9724 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9725 | FunDecl->getType()) || |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9726 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9727 | ResultTy)) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9728 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9729 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9730 | FoundNonTemplateFunction = true; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9731 | return true; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9732 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9733 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9734 | |
| 9735 | return false; |
| 9736 | } |
| 9737 | |
| 9738 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9739 | bool Ret = false; |
| 9740 | |
| 9741 | // If the overload expression doesn't have the form of a pointer to |
| 9742 | // member, don't try to convert it to a pointer-to-member type. |
| 9743 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9744 | return false; |
| 9745 | |
| 9746 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9747 | E = OvlExpr->decls_end(); |
| 9748 | I != E; ++I) { |
| 9749 | // Look through any using declarations to find the underlying function. |
| 9750 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9751 | |
| 9752 | // C++ [over.over]p3: |
| 9753 | // Non-member functions and static member functions match |
| 9754 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9755 | // Nonstatic member functions match targets of |
| 9756 | // type "pointer-to-member-function." |
| 9757 | // Note that according to DR 247, the containing class does not matter. |
| 9758 | if (FunctionTemplateDecl *FunctionTemplate |
| 9759 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9760 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9761 | Ret = true; |
| 9762 | } |
| 9763 | // If we have explicit template arguments supplied, skip non-templates. |
| 9764 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9765 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9766 | Ret = true; |
| 9767 | } |
| 9768 | assert(Ret || Matches.empty()); |
| 9769 | return Ret; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9770 | } |
| 9771 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9772 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9773 | // [...] and any given function template specialization F1 is |
| 9774 | // eliminated if the set contains a second function template |
| 9775 | // specialization whose function template is more specialized |
| 9776 | // than the function template of F1 according to the partial |
| 9777 | // ordering rules of 14.5.5.2. |
| 9778 | |
| 9779 | // The algorithm specified above is quadratic. We instead use a |
| 9780 | // two-pass algorithm (similar to the one used to identify the |
| 9781 | // best viable function in an overload set) that identifies the |
| 9782 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9783 | |
| 9784 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9785 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9786 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9787 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9788 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9789 | // here, since the no_viable diagnostic has index 0. |
| 9790 | UnresolvedSetIterator Result = S.getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 9791 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9792 | SourceExpr->getLocStart(), S.PDiag(), |
| 9793 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9794 | .second->getDeclName(), |
| 9795 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9796 | Complain, TargetFunctionType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9797 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9798 | if (Result != MatchesCopy.end()) { |
| 9799 | // Make it the first and only element |
| 9800 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9801 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9802 | Matches.resize(1); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9803 | } |
| 9804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9805 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9806 | void EliminateAllTemplateMatches() { |
| 9807 | // [...] any function template specializations in the set are |
| 9808 | // eliminated if the set also contains a non-template function, [...] |
| 9809 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9810 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9811 | ++I; |
| 9812 | else { |
| 9813 | Matches[I] = Matches[--N]; |
| 9814 | Matches.set_size(N); |
| 9815 | } |
| 9816 | } |
| 9817 | } |
| 9818 | |
| 9819 | public: |
| 9820 | void ComplainNoMatchesFound() const { |
| 9821 | assert(Matches.empty()); |
| 9822 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9823 | << OvlExpr->getName() << TargetFunctionType |
| 9824 | << OvlExpr->getSourceRange(); |
Richard Smith | 0d90547 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 9825 | if (FailedCandidates.empty()) |
| 9826 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 9827 | else { |
| 9828 | // We have some deduction failure messages. Use them to diagnose |
| 9829 | // the function templates, and diagnose the non-template candidates |
| 9830 | // normally. |
| 9831 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9832 | IEnd = OvlExpr->decls_end(); |
| 9833 | I != IEnd; ++I) |
| 9834 | if (FunctionDecl *Fun = |
| 9835 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 9836 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 9837 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 9838 | } |
| 9839 | } |
| 9840 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9841 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9842 | return TargetTypeIsNonStaticMemberFunction && |
| 9843 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9844 | } |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9845 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9846 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9847 | // TODO: Should we condition this on whether any functions might |
| 9848 | // have matched, or is it more appropriate to do that in callers? |
| 9849 | // TODO: a fixit wouldn't hurt. |
| 9850 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9851 | << TargetType << OvlExpr->getSourceRange(); |
| 9852 | } |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9853 | |
| 9854 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9855 | return StaticMemberFunctionFromBoundPointer; |
| 9856 | } |
| 9857 | |
| 9858 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9859 | S.Diag(OvlExpr->getLocStart(), |
| 9860 | diag::err_invalid_form_pointer_member_function) |
| 9861 | << OvlExpr->getSourceRange(); |
| 9862 | } |
| 9863 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9864 | void ComplainOfInvalidConversion() const { |
| 9865 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9866 | << OvlExpr->getName() << TargetType; |
| 9867 | } |
| 9868 | |
| 9869 | void ComplainMultipleMatchesFound() const { |
| 9870 | assert(Matches.size() > 1); |
| 9871 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9872 | << OvlExpr->getName() |
| 9873 | << OvlExpr->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9874 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9875 | } |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9876 | |
| 9877 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9878 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9879 | int getNumMatches() const { return Matches.size(); } |
| 9880 | |
| 9881 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9882 | if (Matches.size() != 1) return 0; |
| 9883 | return Matches[0].second; |
| 9884 | } |
| 9885 | |
| 9886 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9887 | if (Matches.size() != 1) return 0; |
| 9888 | return &Matches[0].first; |
| 9889 | } |
| 9890 | }; |
| 9891 | |
| 9892 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9893 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9894 | /// expression with overloaded function type and @p ToType is the type |
| 9895 | /// we're trying to resolve to. For example: |
| 9896 | /// |
| 9897 | /// @code |
| 9898 | /// int f(double); |
| 9899 | /// int f(int); |
| 9900 | /// |
| 9901 | /// int (*pfd)(double) = f; // selects f(double) |
| 9902 | /// @endcode |
| 9903 | /// |
| 9904 | /// This routine returns the resulting FunctionDecl if it could be |
| 9905 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9906 | /// routine will emit diagnostics if there is an error. |
| 9907 | FunctionDecl * |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9908 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9909 | QualType TargetType, |
| 9910 | bool Complain, |
| 9911 | DeclAccessPair &FoundResult, |
| 9912 | bool *pHadMultipleCandidates) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9913 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9914 | |
| 9915 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9916 | Complain); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9917 | int NumMatches = Resolver.getNumMatches(); |
| 9918 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9919 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9920 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9921 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9922 | else |
| 9923 | Resolver.ComplainNoMatchesFound(); |
| 9924 | } |
| 9925 | else if (NumMatches > 1 && Complain) |
| 9926 | Resolver.ComplainMultipleMatchesFound(); |
| 9927 | else if (NumMatches == 1) { |
| 9928 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9929 | assert(Fn); |
| 9930 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | a4f7c7a | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9931 | if (Complain) { |
| 9932 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9933 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9934 | else |
| 9935 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9936 | } |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9937 | } |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9938 | |
| 9939 | if (pHadMultipleCandidates) |
| 9940 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9941 | return Fn; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9944 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9945 | /// resolve that overloaded function expression down to a single function. |
| 9946 | /// |
| 9947 | /// This routine can only resolve template-ids that refer to a single function |
| 9948 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9949 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9950 | /// as described in C++0x [temp.arg.explicit]p3. |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 9951 | /// |
| 9952 | /// If no template-ids are found, no diagnostics are emitted and NULL is |
| 9953 | /// returned. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9954 | FunctionDecl * |
| 9955 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9956 | bool Complain, |
| 9957 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9958 | // C++ [over.over]p1: |
| 9959 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9960 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9961 | // C++ [over.over]p1: |
| 9962 | // [...] The overloaded function name can be preceded by the & |
| 9963 | // operator. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9964 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9965 | // If we didn't actually find any template-ids, we're done. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9966 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9967 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9968 | |
| 9969 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9970 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9971 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9972 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9973 | // Look through all of the overloaded functions, searching for one |
| 9974 | // whose type matches exactly. |
| 9975 | FunctionDecl *Matched = 0; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9976 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9977 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9978 | // C++0x [temp.arg.explicit]p3: |
| 9979 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9980 | // where deduction is not done, if a template argument list is |
| 9981 | // specified and it, along with any default template arguments, |
| 9982 | // identifies a single function template specialization, then the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9983 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | eebe721 | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9984 | FunctionTemplateDecl *FunctionTemplate |
| 9985 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9986 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9987 | // C++ [over.over]p2: |
| 9988 | // If the name is a function template, template argument deduction is |
| 9989 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9990 | // resulting template argument list is used to generate a single |
| 9991 | // function template specialization, which is added to the set of |
| 9992 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9993 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9994 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9995 | if (TemplateDeductionResult Result |
| 9996 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9997 | Specialization, Info, |
| 9998 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9999 | // Make a note of the failed deduction for diagnostics. |
| 10000 | // TODO: Actually use the failed-deduction info? |
| 10001 | FailedCandidates.addCandidate() |
| 10002 | .set(FunctionTemplate->getTemplatedDecl(), |
| 10003 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10004 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10005 | } |
| 10006 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10007 | assert(Specialization && "no specialization and no error?"); |
| 10008 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10009 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10010 | if (Matched) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10011 | if (Complain) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10012 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 10013 | << ovl->getName(); |
| 10014 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10015 | } |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10016 | return 0; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10017 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 10018 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10019 | Matched = Specialization; |
| 10020 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10022 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 10023 | if (Matched && getLangOpts().CPlusPlus1y && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10024 | Matched->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 10025 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 10026 | return 0; |
| 10027 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 10028 | return Matched; |
| 10029 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10030 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10031 | |
| 10032 | |
| 10033 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10034 | // Resolve and fix an overloaded expression that can be resolved |
| 10035 | // because it identifies a single function template specialization. |
| 10036 | // |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10037 | // Last three arguments should only be supplied if Complain = true |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10038 | // |
| 10039 | // Return true if it was logically possible to so resolve the |
| 10040 | // expression, regardless of whether or not it succeeded. Always |
| 10041 | // returns true if 'complain' is set. |
| 10042 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 10043 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 10044 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10045 | QualType DestTypeForComplaining, |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10046 | unsigned DiagIDForComplaining) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10047 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10048 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10049 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10050 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10051 | DeclAccessPair found; |
| 10052 | ExprResult SingleFunctionExpression; |
| 10053 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 10054 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10055 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10056 | SrcExpr = ExprError(); |
| 10057 | return true; |
| 10058 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10059 | |
| 10060 | // It is only correct to resolve to an instance method if we're |
| 10061 | // resolving a form that's permitted to be a pointer to member. |
| 10062 | // Otherwise we'll end up making a bound member expression, which |
| 10063 | // is illegal in all the contexts we resolve like this. |
| 10064 | if (!ovl.HasFormOfMemberPointer && |
| 10065 | isa<CXXMethodDecl>(fn) && |
| 10066 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10067 | if (!complain) return false; |
| 10068 | |
| 10069 | Diag(ovl.Expression->getExprLoc(), |
| 10070 | diag::err_bound_member_function) |
| 10071 | << 0 << ovl.Expression->getSourceRange(); |
| 10072 | |
| 10073 | // TODO: I believe we only end up here if there's a mix of |
| 10074 | // static and non-static candidates (otherwise the expression |
| 10075 | // would have 'bound member' type, not 'overload' type). |
| 10076 | // Ideally we would note which candidate was chosen and why |
| 10077 | // the static candidates were rejected. |
| 10078 | SrcExpr = ExprError(); |
| 10079 | return true; |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10080 | } |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10081 | |
Sylvestre Ledru | a520266 | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 10082 | // Fix the expression to refer to 'fn'. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10083 | SingleFunctionExpression = |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10084 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10085 | |
| 10086 | // If desired, do function-to-pointer decay. |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10087 | if (doFunctionPointerConverion) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10088 | SingleFunctionExpression = |
| 10089 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10090 | if (SingleFunctionExpression.isInvalid()) { |
| 10091 | SrcExpr = ExprError(); |
| 10092 | return true; |
| 10093 | } |
| 10094 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10095 | } |
| 10096 | |
| 10097 | if (!SingleFunctionExpression.isUsable()) { |
| 10098 | if (complain) { |
| 10099 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 10100 | << ovl.Expression->getName() |
| 10101 | << DestTypeForComplaining |
| 10102 | << OpRangeForComplaining |
| 10103 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10104 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 10105 | |
| 10106 | SrcExpr = ExprError(); |
| 10107 | return true; |
| 10108 | } |
| 10109 | |
| 10110 | return false; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10111 | } |
| 10112 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10113 | SrcExpr = SingleFunctionExpression; |
| 10114 | return true; |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 10115 | } |
| 10116 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10117 | /// \brief Add a single candidate to the overload set. |
| 10118 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10119 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 10120 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10121 | ArrayRef<Expr *> Args, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10122 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10123 | bool PartialOverloading, |
| 10124 | bool KnownValid) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10125 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10126 | if (isa<UsingShadowDecl>(Callee)) |
| 10127 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 10128 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10129 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10130 | if (ExplicitTemplateArgs) { |
| 10131 | assert(!KnownValid && "Explicit template arguments?"); |
| 10132 | return; |
| 10133 | } |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10134 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 10135 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10136 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10137 | } |
| 10138 | |
| 10139 | if (FunctionTemplateDecl *FuncTemplate |
| 10140 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10141 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10142 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10143 | return; |
| 10144 | } |
| 10145 | |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10146 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10147 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10148 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10149 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 10150 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10151 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10152 | ArrayRef<Expr *> Args, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10153 | OverloadCandidateSet &CandidateSet, |
| 10154 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10155 | |
| 10156 | #ifndef NDEBUG |
| 10157 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 10158 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10159 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10160 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 10161 | // and let Y be the lookup set produced by argument dependent |
| 10162 | // lookup (defined as follows). If X contains |
| 10163 | // |
| 10164 | // -- a declaration of a class member, or |
| 10165 | // |
| 10166 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10167 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10168 | // |
| 10169 | // -- a declaration that is neither a function or a function |
| 10170 | // template |
| 10171 | // |
| 10172 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10173 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10174 | if (ULE->requiresADL()) { |
| 10175 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 10176 | E = ULE->decls_end(); I != E; ++I) { |
| 10177 | assert(!(*I)->getDeclContext()->isRecord()); |
| 10178 | assert(isa<UsingShadowDecl>(*I) || |
| 10179 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 10180 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10181 | } |
| 10182 | } |
| 10183 | #endif |
| 10184 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10185 | // It would be nice to avoid this copy. |
| 10186 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 10187 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10188 | if (ULE->hasExplicitTemplateArgs()) { |
| 10189 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10190 | ExplicitTemplateArgs = &TABuffer; |
| 10191 | } |
| 10192 | |
| 10193 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 10194 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10195 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 10196 | CandidateSet, PartialOverloading, |
| 10197 | /*KnownValid*/ true); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10198 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10199 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10200 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | e06a2c1 | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 10201 | ULE->getExprLoc(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10202 | Args, ExplicitTemplateArgs, |
Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10203 | CandidateSet, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10204 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10205 | |
Richard Smith | 0603bbb | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10206 | /// Determine whether a declaration with the specified name could be moved into |
| 10207 | /// a different namespace. |
| 10208 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 10209 | switch (Name.getCXXOverloadedOperator()) { |
| 10210 | case OO_New: case OO_Array_New: |
| 10211 | case OO_Delete: case OO_Array_Delete: |
| 10212 | return false; |
| 10213 | |
| 10214 | default: |
| 10215 | return true; |
| 10216 | } |
| 10217 | } |
| 10218 | |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10219 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 10220 | /// template, where the non-dependent name was declared after the template |
| 10221 | /// was defined. This is common in code written for a compilers which do not |
| 10222 | /// correctly implement two-stage name lookup. |
| 10223 | /// |
| 10224 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10225 | static bool |
| 10226 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 10227 | const CXXScopeSpec &SS, LookupResult &R, |
| 10228 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10229 | ArrayRef<Expr *> Args) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10230 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 10231 | return false; |
| 10232 | |
| 10233 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | fcd5e7a | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 10234 | if (DC->isTransparentContext()) |
| 10235 | continue; |
| 10236 | |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10237 | SemaRef.LookupQualifiedName(R, DC); |
| 10238 | |
| 10239 | if (!R.empty()) { |
| 10240 | R.suppressDiagnostics(); |
| 10241 | |
| 10242 | if (isa<CXXRecordDecl>(DC)) { |
| 10243 | // Don't diagnose names we find in classes; we get much better |
| 10244 | // diagnostics for these from DiagnoseEmptyLookup. |
| 10245 | R.clear(); |
| 10246 | return false; |
| 10247 | } |
| 10248 | |
| 10249 | OverloadCandidateSet Candidates(FnLoc); |
| 10250 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 10251 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10252 | ExplicitTemplateArgs, Args, |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10253 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10254 | |
| 10255 | OverloadCandidateSet::iterator Best; |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10256 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10257 | // No viable functions. Don't bother the user with notes for functions |
| 10258 | // which don't work and shouldn't be found anyway. |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10259 | R.clear(); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10260 | return false; |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10261 | } |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10262 | |
| 10263 | // Find the namespaces where ADL would have looked, and suggest |
| 10264 | // declaring the function there instead. |
| 10265 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 10266 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 7d8b041 | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 10267 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10268 | AssociatedNamespaces, |
| 10269 | AssociatedClasses); |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10270 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | 0603bbb | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10271 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 10272 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 10273 | for (Sema::AssociatedNamespaceSet::iterator |
| 10274 | it = AssociatedNamespaces.begin(), |
| 10275 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 10276 | // Never suggest declaring a function within namespace 'std'. |
| 10277 | if (Std && Std->Encloses(*it)) |
| 10278 | continue; |
Richard Smith | 21bae43 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 10279 | |
Richard Smith | 0603bbb | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10280 | // Never suggest declaring a function within a namespace with a |
| 10281 | // reserved name, like __gnu_cxx. |
| 10282 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 10283 | if (NS && |
| 10284 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 10285 | continue; |
| 10286 | |
| 10287 | SuggestedNamespaces.insert(*it); |
| 10288 | } |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10289 | } |
| 10290 | |
| 10291 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 10292 | << R.getLookupName(); |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10293 | if (SuggestedNamespaces.empty()) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10294 | SemaRef.Diag(Best->Function->getLocation(), |
| 10295 | diag::note_not_found_by_two_phase_lookup) |
| 10296 | << R.getLookupName() << 0; |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10297 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10298 | SemaRef.Diag(Best->Function->getLocation(), |
| 10299 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10300 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10301 | } else { |
| 10302 | // FIXME: It would be useful to list the associated namespaces here, |
| 10303 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10304 | // a localized representation of a list of items. |
| 10305 | SemaRef.Diag(Best->Function->getLocation(), |
| 10306 | diag::note_not_found_by_two_phase_lookup) |
| 10307 | << R.getLookupName() << 2; |
| 10308 | } |
| 10309 | |
| 10310 | // Try to recover by calling this function. |
| 10311 | return true; |
| 10312 | } |
| 10313 | |
| 10314 | R.clear(); |
| 10315 | } |
| 10316 | |
| 10317 | return false; |
| 10318 | } |
| 10319 | |
| 10320 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10321 | /// template, where the non-dependent operator was declared after the template |
| 10322 | /// was defined. |
| 10323 | /// |
| 10324 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10325 | static bool |
| 10326 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10327 | SourceLocation OpLoc, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10328 | ArrayRef<Expr *> Args) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10329 | DeclarationName OpName = |
| 10330 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10331 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10332 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10333 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10334 | } |
| 10335 | |
Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10336 | namespace { |
Richard Smith | 88d67f3 | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10337 | class BuildRecoveryCallExprRAII { |
| 10338 | Sema &SemaRef; |
| 10339 | public: |
| 10340 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10341 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10342 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10343 | } |
| 10344 | |
| 10345 | ~BuildRecoveryCallExprRAII() { |
| 10346 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10347 | } |
| 10348 | }; |
| 10349 | |
Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10350 | } |
| 10351 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10352 | /// Attempts to recover from a call where no functions were found. |
| 10353 | /// |
| 10354 | /// Returns true if new candidates were found. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10355 | static ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10356 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10357 | UnresolvedLookupExpr *ULE, |
| 10358 | SourceLocation LParenLoc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10359 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10360 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10361 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | 88d67f3 | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10362 | // Do not try to recover if it is already building a recovery call. |
| 10363 | // This stops infinite loops for template instantiations like |
| 10364 | // |
| 10365 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10366 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10367 | // |
| 10368 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10369 | return ExprError(); |
| 10370 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10371 | |
| 10372 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10373 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10374 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10375 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10376 | TemplateArgumentListInfo TABuffer; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10377 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10378 | if (ULE->hasExplicitTemplateArgs()) { |
| 10379 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10380 | ExplicitTemplateArgs = &TABuffer; |
| 10381 | } |
| 10382 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10383 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10384 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 53e7219 | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10385 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
Kaelyn Uhrain | b4b1475 | 2014-02-28 18:12:42 +0000 | [diff] [blame] | 10386 | ExplicitTemplateArgs != 0, false); |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10387 | NoTypoCorrectionCCC RejectAll; |
| 10388 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10389 | (CorrectionCandidateCallback*)&Validator : |
| 10390 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10391 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10392 | ExplicitTemplateArgs, Args) && |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10393 | (!EmptyLookup || |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10394 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10395 | ExplicitTemplateArgs, Args))) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10396 | return ExprError(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10397 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10398 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10399 | |
| 10400 | // Build an implicit member call if appropriate. Just drop the |
| 10401 | // casts and such from the call, we don't really care. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10402 | ExprResult NewFn = ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10403 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10404 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10405 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10406 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10407 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10408 | ExplicitTemplateArgs); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10409 | else |
| 10410 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10411 | |
| 10412 | if (NewFn.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10413 | return ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10414 | |
| 10415 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10416 | // an expression with viable lookup results, which should never |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10417 | // end up here. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10418 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10419 | MultiExprArg(Args.data(), Args.size()), |
| 10420 | RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10421 | } |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10422 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10423 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10424 | /// the given function. |
| 10425 | /// \returns true when an the ExprResult output parameter has been set. |
| 10426 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10427 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10428 | MultiExprArg Args, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10429 | SourceLocation RParenLoc, |
| 10430 | OverloadCandidateSet *CandidateSet, |
| 10431 | ExprResult *Result) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10432 | #ifndef NDEBUG |
| 10433 | if (ULE->requiresADL()) { |
| 10434 | // To do ADL, we must have found an unqualified name. |
| 10435 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10436 | |
| 10437 | // We don't perform ADL for implicit declarations of builtins. |
| 10438 | // Verify that this was correctly set up. |
| 10439 | FunctionDecl *F; |
| 10440 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10441 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10442 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10443 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10444 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10445 | // We don't perform ADL in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10446 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10447 | } |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10448 | #endif |
| 10449 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10450 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10451 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10452 | *Result = ExprError(); |
| 10453 | return true; |
| 10454 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10455 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10456 | // Add the functions denoted by the callee to the set of candidate |
| 10457 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10458 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10459 | |
| 10460 | // If we found nothing, try to recover. |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10461 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10462 | // out if it fails. |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10463 | if (CandidateSet->empty()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10464 | // In Microsoft mode, if we are inside a template class member function then |
| 10465 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10466 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10467 | // classes. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 10468 | if (getLangOpts().MSVCCompat && CurContext->isDependentContext() && |
Francois Pichet | de232cb | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10469 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10470 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10471 | Context.DependentTy, VK_RValue, |
| 10472 | RParenLoc); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10473 | CE->setTypeDependent(true); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10474 | *Result = Owned(CE); |
| 10475 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10476 | } |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10477 | return false; |
Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10478 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10479 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10480 | UnbridgedCasts.restore(); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10481 | return false; |
| 10482 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10483 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10484 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10485 | /// the completed call expression. If overload resolution fails, emits |
| 10486 | /// diagnostics and returns ExprError() |
| 10487 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10488 | UnresolvedLookupExpr *ULE, |
| 10489 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10490 | MultiExprArg Args, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10491 | SourceLocation RParenLoc, |
| 10492 | Expr *ExecConfig, |
| 10493 | OverloadCandidateSet *CandidateSet, |
| 10494 | OverloadCandidateSet::iterator *Best, |
| 10495 | OverloadingResult OverloadResult, |
| 10496 | bool AllowTypoCorrection) { |
| 10497 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10498 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10499 | RParenLoc, /*EmptyLookup=*/true, |
| 10500 | AllowTypoCorrection); |
| 10501 | |
| 10502 | switch (OverloadResult) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10503 | case OR_Success: { |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10504 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10505 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10506 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10507 | return ExprError(); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10508 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10509 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10510 | ExecConfig); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10511 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10512 | |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10513 | case OR_No_Viable_Function: { |
| 10514 | // Try to recover by looking for viable functions which the user might |
| 10515 | // have meant to call. |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10516 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10517 | Args, RParenLoc, |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10518 | /*EmptyLookup=*/false, |
| 10519 | AllowTypoCorrection); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10520 | if (!Recovery.isInvalid()) |
| 10521 | return Recovery; |
| 10522 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10523 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10524 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10525 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10526 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10527 | break; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10528 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10529 | |
| 10530 | case OR_Ambiguous: |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10531 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10532 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10533 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10534 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10535 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10536 | case OR_Deleted: { |
| 10537 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10538 | << (*Best)->Function->isDeleted() |
| 10539 | << ULE->getName() |
| 10540 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10541 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10542 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 3eaa22a | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10543 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10544 | // We emitted an error for the unvailable/deleted function call but keep |
| 10545 | // the call in the AST. |
| 10546 | FunctionDecl *FDecl = (*Best)->Function; |
| 10547 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10548 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10549 | ExecConfig); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10550 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10551 | } |
| 10552 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10553 | // Overload resolution failed. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10554 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10555 | } |
| 10556 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10557 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10558 | /// (which eventually refers to the declaration Func) and the call |
| 10559 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10560 | /// to a specific function. If overload resolution succeeds, returns |
| 10561 | /// the call expression produced by overload resolution. |
| 10562 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10563 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10564 | UnresolvedLookupExpr *ULE, |
| 10565 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10566 | MultiExprArg Args, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10567 | SourceLocation RParenLoc, |
| 10568 | Expr *ExecConfig, |
| 10569 | bool AllowTypoCorrection) { |
| 10570 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10571 | ExprResult result; |
| 10572 | |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10573 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10574 | &result)) |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10575 | return result; |
| 10576 | |
| 10577 | OverloadCandidateSet::iterator Best; |
| 10578 | OverloadingResult OverloadResult = |
| 10579 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10580 | |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10581 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10582 | RParenLoc, ExecConfig, &CandidateSet, |
| 10583 | &Best, OverloadResult, |
| 10584 | AllowTypoCorrection); |
| 10585 | } |
| 10586 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10587 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10588 | return Functions.size() > 1 || |
| 10589 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10590 | } |
| 10591 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10592 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10593 | /// operator. |
| 10594 | /// |
| 10595 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10596 | /// |
| 10597 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10598 | /// operator. |
| 10599 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10600 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10601 | /// considered by overload resolution. The caller needs to build this |
| 10602 | /// set based on the context using, e.g., |
| 10603 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10604 | /// set should not contain any member functions; those will be added |
| 10605 | /// by CreateOverloadedUnaryOp(). |
| 10606 | /// |
James Dennett | 91738ff | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10607 | /// \param Input The input argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10608 | ExprResult |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10609 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10610 | const UnresolvedSetImpl &Fns, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10611 | Expr *Input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10612 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10613 | |
| 10614 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10615 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10616 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10617 | // TODO: provide better source location info. |
| 10618 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10619 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10620 | if (checkPlaceholderForOverload(*this, Input)) |
| 10621 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10622 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10623 | Expr *Args[2] = { Input, 0 }; |
| 10624 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10625 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10626 | // For post-increment and post-decrement, add the implicit '0' as |
| 10627 | // the second argument, so that we know this is a post-increment or |
| 10628 | // post-decrement. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10629 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10630 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10631 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10632 | SourceLocation()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10633 | NumArgs = 2; |
| 10634 | } |
| 10635 | |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10636 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10637 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10638 | if (Input->isTypeDependent()) { |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10639 | if (Fns.empty()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10640 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10641 | Opc, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10642 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10643 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10644 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10645 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10646 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10647 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10648 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10649 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10650 | /*ADL*/ true, IsOverloaded(Fns), |
| 10651 | Fns.begin(), Fns.end()); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10652 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10653 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10654 | VK_RValue, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10655 | OpLoc, false)); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10656 | } |
| 10657 | |
| 10658 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10659 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10660 | |
| 10661 | // Add the candidates from the given function set. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10662 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10663 | |
| 10664 | // Add operator candidates that are member functions. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10665 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10666 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10667 | // Add candidates from ADL. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10668 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10669 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10670 | CandidateSet); |
| 10671 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10672 | // Add builtin operator candidates. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10673 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10674 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10675 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10676 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10677 | // Perform overload resolution. |
| 10678 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10679 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10680 | case OR_Success: { |
| 10681 | // We found a built-in operator or an overloaded operator. |
| 10682 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10683 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10684 | if (FnDecl) { |
| 10685 | // We matched an overloaded operator. Build a call to that |
| 10686 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10687 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10688 | // Convert the arguments. |
| 10689 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10690 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10691 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10692 | ExprResult InputRes = |
| 10693 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10694 | Best->FoundDecl, Method); |
| 10695 | if (InputRes.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10696 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10697 | Input = InputRes.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10698 | } else { |
| 10699 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10700 | ExprResult InputInit |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10701 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10702 | Context, |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10703 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10704 | SourceLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10705 | Input); |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10706 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10707 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10708 | Input = InputInit.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10709 | } |
| 10710 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10711 | // Build the actual expression node. |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10712 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10713 | HadMultipleCandidates, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10714 | if (FnExpr.isInvalid()) |
| 10715 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10716 | |
Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 10717 | // Determine the result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10718 | QualType ResultTy = FnDecl->getReturnType(); |
Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 10719 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10720 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10721 | |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10722 | Args[0] = Input; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10723 | CallExpr *TheCall = |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10724 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10725 | ResultTy, VK, OpLoc, false); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10726 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10727 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, FnDecl)) |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10728 | return ExprError(); |
| 10729 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10730 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10731 | } else { |
| 10732 | // We matched a built-in operator. Convert the arguments, then |
| 10733 | // break out so that we will build the appropriate built-in |
| 10734 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10735 | ExprResult InputRes = |
| 10736 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10737 | Best->Conversions[0], AA_Passing); |
| 10738 | if (InputRes.isInvalid()) |
| 10739 | return ExprError(); |
| 10740 | Input = InputRes.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10741 | break; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10742 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10743 | } |
| 10744 | |
| 10745 | case OR_No_Viable_Function: |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10746 | // This is an erroneous use of an operator which can be overloaded by |
| 10747 | // a non-member function. Check for non-member operators which were |
| 10748 | // defined too late to be candidates. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10749 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10750 | // FIXME: Recover by calling the found function. |
| 10751 | return ExprError(); |
| 10752 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10753 | // No viable function; fall through to handling this as a |
| 10754 | // built-in operator, which will produce an error message for us. |
| 10755 | break; |
| 10756 | |
| 10757 | case OR_Ambiguous: |
| 10758 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10759 | << UnaryOperator::getOpcodeStr(Opc) |
| 10760 | << Input->getType() |
| 10761 | << Input->getSourceRange(); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10762 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10763 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10764 | return ExprError(); |
| 10765 | |
| 10766 | case OR_Deleted: |
| 10767 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10768 | << Best->Function->isDeleted() |
| 10769 | << UnaryOperator::getOpcodeStr(Opc) |
| 10770 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10771 | << Input->getSourceRange(); |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10772 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 79b2d3a | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10773 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10774 | return ExprError(); |
| 10775 | } |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10776 | |
| 10777 | // Either we found no viable overloaded operator or we matched a |
| 10778 | // built-in operator. In either case, fall through to trying to |
| 10779 | // build a built-in operation. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10780 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10781 | } |
| 10782 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10783 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10784 | /// operator. |
| 10785 | /// |
| 10786 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10787 | /// |
| 10788 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10789 | /// operator. |
| 10790 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10791 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10792 | /// considered by overload resolution. The caller needs to build this |
| 10793 | /// set based on the context using, e.g., |
| 10794 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10795 | /// set should not contain any member functions; those will be added |
| 10796 | /// by CreateOverloadedBinOp(). |
| 10797 | /// |
| 10798 | /// \param LHS Left-hand argument. |
| 10799 | /// \param RHS Right-hand argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10800 | ExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10801 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10802 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10803 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10804 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10805 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10806 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10807 | |
| 10808 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10809 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10810 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10811 | |
| 10812 | // If either side is type-dependent, create an appropriate dependent |
| 10813 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10814 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10815 | if (Fns.empty()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10816 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10817 | // BinaryOperator or CompoundAssignment. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10818 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10819 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10820 | Context.DependentTy, |
| 10821 | VK_RValue, OK_Ordinary, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10822 | OpLoc, |
| 10823 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10824 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10825 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10826 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10827 | VK_LValue, |
| 10828 | OK_Ordinary, |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10829 | Context.DependentTy, |
| 10830 | Context.DependentTy, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10831 | OpLoc, |
| 10832 | FPFeatures.fp_contract)); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10833 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10834 | |
| 10835 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10836 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10837 | // TODO: provide better source location info in DNLoc component. |
| 10838 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10839 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10840 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10841 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10842 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10843 | Fns.begin(), Fns.end()); |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10844 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10845 | Context.DependentTy, VK_RValue, |
| 10846 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10847 | } |
| 10848 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10849 | // Always do placeholder-like conversions on the RHS. |
| 10850 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10851 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10852 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10853 | // Do placeholder-like conversion on the LHS; note that we should |
| 10854 | // not get here with a PseudoObject LHS. |
| 10855 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10856 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10857 | return ExprError(); |
| 10858 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10859 | // If this is the assignment operator, we only perform overload resolution |
| 10860 | // if the left-hand side is a class or enumeration type. This is actually |
| 10861 | // a hack. The standard requires that we do overload resolution between the |
| 10862 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10863 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10864 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10865 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10866 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10867 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10868 | // If this is the .* operator, which is not overloadable, just |
| 10869 | // create a built-in binary operator. |
| 10870 | if (Opc == BO_PtrMemD) |
| 10871 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10872 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10873 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10874 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10875 | |
| 10876 | // Add the candidates from the given function set. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10877 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10878 | |
| 10879 | // Add operator candidates that are member functions. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10880 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10881 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10882 | // Add candidates from ADL. |
| 10883 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10884 | OpLoc, Args, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10885 | /*ExplicitTemplateArgs*/ 0, |
| 10886 | CandidateSet); |
| 10887 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10888 | // Add builtin operator candidates. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10889 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10890 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10891 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10892 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10893 | // Perform overload resolution. |
| 10894 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10895 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10896 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10897 | // We found a built-in operator or an overloaded operator. |
| 10898 | FunctionDecl *FnDecl = Best->Function; |
| 10899 | |
| 10900 | if (FnDecl) { |
| 10901 | // We matched an overloaded operator. Build a call to that |
| 10902 | // operator. |
| 10903 | |
| 10904 | // Convert the arguments. |
| 10905 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10906 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10907 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10908 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10909 | ExprResult Arg1 = |
| 10910 | PerformCopyInitialization( |
| 10911 | InitializedEntity::InitializeParameter(Context, |
| 10912 | FnDecl->getParamDecl(0)), |
| 10913 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10914 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10915 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10916 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10917 | ExprResult Arg0 = |
| 10918 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10919 | Best->FoundDecl, Method); |
| 10920 | if (Arg0.isInvalid()) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10921 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10922 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10923 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10924 | } else { |
| 10925 | // Convert the arguments. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10926 | ExprResult Arg0 = PerformCopyInitialization( |
| 10927 | InitializedEntity::InitializeParameter(Context, |
| 10928 | FnDecl->getParamDecl(0)), |
| 10929 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10930 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10931 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10932 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10933 | ExprResult Arg1 = |
| 10934 | PerformCopyInitialization( |
| 10935 | InitializedEntity::InitializeParameter(Context, |
| 10936 | FnDecl->getParamDecl(1)), |
| 10937 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10938 | if (Arg1.isInvalid()) |
| 10939 | return ExprError(); |
| 10940 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10941 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10942 | } |
| 10943 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10944 | // Build the actual expression node. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10945 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10946 | Best->FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10947 | HadMultipleCandidates, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10948 | if (FnExpr.isInvalid()) |
| 10949 | return ExprError(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10950 | |
Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 10951 | // Determine the result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10952 | QualType ResultTy = FnDecl->getReturnType(); |
Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 10953 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10954 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10955 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10956 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10957 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10958 | Args, ResultTy, VK, OpLoc, |
| 10959 | FPFeatures.fp_contract); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10960 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10961 | if (CheckCallReturnType(FnDecl->getReturnType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10962 | FnDecl)) |
| 10963 | return ExprError(); |
| 10964 | |
Nick Lewycky | d24d5f2 | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10965 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10966 | // Cut off the implicit 'this'. |
| 10967 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10968 | ArgsArray = ArgsArray.slice(1); |
| 10969 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10970 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10971 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10972 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10973 | } else { |
| 10974 | // We matched a built-in operator. Convert the arguments, then |
| 10975 | // break out so that we will build the appropriate built-in |
| 10976 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10977 | ExprResult ArgsRes0 = |
| 10978 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10979 | Best->Conversions[0], AA_Passing); |
| 10980 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10981 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10982 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10983 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10984 | ExprResult ArgsRes1 = |
| 10985 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10986 | Best->Conversions[1], AA_Passing); |
| 10987 | if (ArgsRes1.isInvalid()) |
| 10988 | return ExprError(); |
| 10989 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10990 | break; |
| 10991 | } |
| 10992 | } |
| 10993 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10994 | case OR_No_Viable_Function: { |
| 10995 | // C++ [over.match.oper]p9: |
| 10996 | // If the operator is the operator , [...] and there are no |
| 10997 | // viable functions, then the operator is assumed to be the |
| 10998 | // built-in operator and interpreted according to clause 5. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10999 | if (Opc == BO_Comma) |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11000 | break; |
| 11001 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11002 | // For class as left operand for assignment or compound assigment |
| 11003 | // operator do not fall through to handling in built-in, but report that |
| 11004 | // no overloaded assignment operator found |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11005 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11006 | if (Args[0]->getType()->isRecordType() && |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11007 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 11008 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 11009 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11010 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | a31efa0 | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 11011 | if (Args[0]->getType()->isIncompleteType()) { |
| 11012 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 11013 | << Args[0]->getType() |
| 11014 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11015 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11016 | } else { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11017 | // This is an erroneous use of an operator which can be overloaded by |
| 11018 | // a non-member function. Check for non-member operators which were |
| 11019 | // defined too late to be candidates. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11020 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 11021 | // FIXME: Recover by calling the found function. |
| 11022 | return ExprError(); |
| 11023 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11024 | // No viable function; try to create a built-in operation, which will |
| 11025 | // produce an error. Then, show the non-viable candidates. |
| 11026 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 11027 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11028 | assert(Result.isInvalid() && |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11029 | "C++ binary operator overloading is missing candidates!"); |
| 11030 | if (Result.isInvalid()) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11031 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11032 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11033 | return Result; |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11034 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11035 | |
| 11036 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11037 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11038 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11039 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11040 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11041 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11042 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11043 | return ExprError(); |
| 11044 | |
| 11045 | case OR_Deleted: |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 11046 | if (isImplicitlyDeleted(Best->Function)) { |
| 11047 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 11048 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | de1a487 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 11049 | << Context.getRecordType(Method->getParent()) |
| 11050 | << getSpecialMember(Method); |
Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 11051 | |
Richard Smith | de1a487 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 11052 | // The user probably meant to call this special member. Just |
| 11053 | // explain why it's deleted. |
| 11054 | NoteDeletedFunction(Method); |
| 11055 | return ExprError(); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 11056 | } else { |
| 11057 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11058 | << Best->Function->isDeleted() |
| 11059 | << BinaryOperator::getOpcodeStr(Opc) |
| 11060 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 11061 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11062 | } |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11063 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 79b2d3a | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 11064 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11065 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 11066 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11067 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 11068 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 11069 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 11070 | } |
| 11071 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11072 | ExprResult |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11073 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 11074 | SourceLocation RLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11075 | Expr *Base, Expr *Idx) { |
| 11076 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11077 | DeclarationName OpName = |
| 11078 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 11079 | |
| 11080 | // If either side is type-dependent, create an appropriate dependent |
| 11081 | // expression. |
| 11082 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 11083 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 11084 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11085 | // CHECKME: no 'operator' keyword? |
| 11086 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 11087 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11088 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 11089 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11090 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 11091 | /*ADL*/ true, /*Overloaded*/ false, |
| 11092 | UnresolvedSetIterator(), |
| 11093 | UnresolvedSetIterator()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11094 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11095 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11096 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11097 | Args, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11098 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11099 | VK_RValue, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11100 | RLoc, false)); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11101 | } |
| 11102 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11103 | // Handle placeholders on both operands. |
| 11104 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 11105 | return ExprError(); |
| 11106 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 11107 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11108 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11109 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11110 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11111 | |
| 11112 | // Subscript can only be overloaded as a member function. |
| 11113 | |
| 11114 | // Add operator candidates that are member functions. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11115 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11116 | |
| 11117 | // Add builtin operator candidates. |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11118 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11119 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11120 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11121 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11122 | // Perform overload resolution. |
| 11123 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11124 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11125 | case OR_Success: { |
| 11126 | // We found a built-in operator or an overloaded operator. |
| 11127 | FunctionDecl *FnDecl = Best->Function; |
| 11128 | |
| 11129 | if (FnDecl) { |
| 11130 | // We matched an overloaded operator. Build a call to that |
| 11131 | // operator. |
| 11132 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11133 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 11134 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11135 | // Convert the arguments. |
| 11136 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11137 | ExprResult Arg0 = |
| 11138 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 11139 | Best->FoundDecl, Method); |
| 11140 | if (Arg0.isInvalid()) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11141 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11142 | Args[0] = Arg0.take(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11143 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11144 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11145 | ExprResult InputInit |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11146 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11147 | Context, |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11148 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11149 | SourceLocation(), |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 11150 | Owned(Args[1])); |
| 11151 | if (InputInit.isInvalid()) |
| 11152 | return ExprError(); |
| 11153 | |
| 11154 | Args[1] = InputInit.takeAs<Expr>(); |
| 11155 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11156 | // Build the actual expression node. |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11157 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 11158 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11159 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11160 | Best->FoundDecl, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11161 | HadMultipleCandidates, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11162 | OpLocInfo.getLoc(), |
| 11163 | OpLocInfo.getInfo()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11164 | if (FnExpr.isInvalid()) |
| 11165 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11166 | |
Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11167 | // Determine the result type |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11168 | QualType ResultTy = FnDecl->getReturnType(); |
Richard Smith | c156470 | 2013-11-15 02:58:23 +0000 | [diff] [blame] | 11169 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11170 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11171 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11172 | CXXOperatorCallExpr *TheCall = |
| 11173 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11174 | FnExpr.take(), Args, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11175 | ResultTy, VK, RLoc, |
| 11176 | false); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11177 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11178 | if (CheckCallReturnType(FnDecl->getReturnType(), LLoc, TheCall, FnDecl)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11179 | return ExprError(); |
| 11180 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11181 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11182 | } else { |
| 11183 | // We matched a built-in operator. Convert the arguments, then |
| 11184 | // break out so that we will build the appropriate built-in |
| 11185 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11186 | ExprResult ArgsRes0 = |
| 11187 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 11188 | Best->Conversions[0], AA_Passing); |
| 11189 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11190 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11191 | Args[0] = ArgsRes0.take(); |
| 11192 | |
| 11193 | ExprResult ArgsRes1 = |
| 11194 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 11195 | Best->Conversions[1], AA_Passing); |
| 11196 | if (ArgsRes1.isInvalid()) |
| 11197 | return ExprError(); |
| 11198 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11199 | |
| 11200 | break; |
| 11201 | } |
| 11202 | } |
| 11203 | |
| 11204 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11205 | if (CandidateSet.empty()) |
| 11206 | Diag(LLoc, diag::err_ovl_no_oper) |
| 11207 | << Args[0]->getType() << /*subscript*/ 0 |
| 11208 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11209 | else |
| 11210 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 11211 | << Args[0]->getType() |
| 11212 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11213 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11214 | "[]", LLoc); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11215 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11216 | } |
| 11217 | |
| 11218 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11219 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11220 | << "[]" |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11221 | << Args[0]->getType() << Args[1]->getType() |
| 11222 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11223 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11224 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11225 | return ExprError(); |
| 11226 | |
| 11227 | case OR_Deleted: |
| 11228 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 11229 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11230 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11231 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11232 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11233 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11234 | return ExprError(); |
| 11235 | } |
| 11236 | |
| 11237 | // We matched a built-in operator; build it. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11238 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11239 | } |
| 11240 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11241 | /// BuildCallToMemberFunction - Build a call to a member |
| 11242 | /// function. MemExpr is the expression that refers to the member |
| 11243 | /// function (and includes the object parameter), Args/NumArgs are the |
| 11244 | /// arguments to the function call (not including the object |
| 11245 | /// parameter). The caller needs to validate that the member |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11246 | /// expression refers to a non-static member function or an overloaded |
| 11247 | /// member function. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11248 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11249 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11250 | SourceLocation LParenLoc, |
| 11251 | MultiExprArg Args, |
| 11252 | SourceLocation RParenLoc) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11253 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 11254 | MemExprE->getType() == Context.OverloadTy); |
| 11255 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11256 | // Dig out the member expression. This holds both the object |
| 11257 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11258 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11259 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11260 | // Determine whether this is a call to a pointer-to-member function. |
| 11261 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 11262 | assert(op->getType() == Context.BoundMemberTy); |
| 11263 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 11264 | |
| 11265 | QualType fnType = |
| 11266 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 11267 | |
| 11268 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 11269 | QualType resultType = proto->getCallResultType(Context); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11270 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getReturnType()); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11271 | |
| 11272 | // Check that the object type isn't more qualified than the |
| 11273 | // member function we're calling. |
| 11274 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 11275 | |
| 11276 | QualType objectType = op->getLHS()->getType(); |
| 11277 | if (op->getOpcode() == BO_PtrMemI) |
| 11278 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 11279 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 11280 | |
| 11281 | Qualifiers difference = objectQuals - funcQuals; |
| 11282 | difference.removeObjCGCAttr(); |
| 11283 | difference.removeAddressSpace(); |
| 11284 | if (difference) { |
| 11285 | std::string qualsString = difference.getAsString(); |
| 11286 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 11287 | << fnType.getUnqualifiedType() |
| 11288 | << qualsString |
| 11289 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 11290 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11291 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11292 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11293 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11294 | resultType, valueKind, RParenLoc); |
| 11295 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11296 | if (CheckCallReturnType(proto->getReturnType(), op->getRHS()->getLocStart(), |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11297 | call, 0)) |
| 11298 | return ExprError(); |
| 11299 | |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11300 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11301 | return ExprError(); |
| 11302 | |
Richard Trieu | 9be9c68 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11303 | if (CheckOtherCall(call, proto)) |
| 11304 | return ExprError(); |
| 11305 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11306 | return MaybeBindToTemporary(call); |
| 11307 | } |
| 11308 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11309 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11310 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11311 | return ExprError(); |
| 11312 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11313 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11314 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11315 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11316 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11317 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11318 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11319 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11320 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11321 | Qualifier = MemExpr->getQualifier(); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11322 | UnbridgedCasts.restore(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11323 | } else { |
| 11324 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11325 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11326 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11327 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11328 | Expr::Classification ObjectClassification |
| 11329 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11330 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11331 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11332 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11333 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11334 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11335 | // FIXME: avoid copy. |
| 11336 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11337 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11338 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11339 | TemplateArgs = &TemplateArgsBuffer; |
| 11340 | } |
| 11341 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11342 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11343 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11344 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11345 | NamedDecl *Func = *I; |
| 11346 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11347 | if (isa<UsingShadowDecl>(Func)) |
| 11348 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11349 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11350 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11351 | // Microsoft supports direct constructor calls. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11352 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11353 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11354 | Args, CandidateSet); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11355 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11356 | // If explicit template arguments were provided, we can't call a |
| 11357 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11358 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11359 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11360 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11361 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11362 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11363 | /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11364 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11365 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11366 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11367 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11368 | Args, CandidateSet, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11369 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11370 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11371 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11372 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11373 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11374 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11375 | UnbridgedCasts.restore(); |
| 11376 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11377 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11378 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11379 | Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11380 | case OR_Success: |
| 11381 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11382 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11383 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11384 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11385 | return ExprError(); |
Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11386 | // If FoundDecl is different from Method (such as if one is a template |
| 11387 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11388 | // called on both. |
| 11389 | // FIXME: This would be more comprehensively addressed by modifying |
| 11390 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11391 | // being used. |
| 11392 | if (Method != FoundDecl.getDecl() && |
| 11393 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11394 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11395 | break; |
| 11396 | |
| 11397 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11398 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11399 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11400 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11401 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11402 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11403 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11404 | |
| 11405 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11406 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11407 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11408 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11409 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11410 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11411 | |
| 11412 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11413 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11414 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11415 | << DeclName |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11416 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11417 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11418 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11419 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11420 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11421 | } |
| 11422 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11423 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11424 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11425 | // If overload resolution picked a static member, build a |
| 11426 | // non-member call based on that function. |
| 11427 | if (Method->isStatic()) { |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11428 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11429 | RParenLoc); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11430 | } |
| 11431 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11432 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11433 | } |
| 11434 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11435 | QualType ResultType = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11436 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11437 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11438 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11439 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11440 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11441 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11442 | ResultType, VK, RParenLoc); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11443 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11444 | // Check for a valid return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11445 | if (CheckCallReturnType(Method->getReturnType(), MemExpr->getMemberLoc(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11446 | TheCall, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11447 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11448 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11449 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11450 | // We only need to do this if there was actually an overload; otherwise |
| 11451 | // it was done at lookup. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11452 | if (!Method->isStatic()) { |
| 11453 | ExprResult ObjectArg = |
| 11454 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11455 | FoundDecl, Method); |
| 11456 | if (ObjectArg.isInvalid()) |
| 11457 | return ExprError(); |
| 11458 | MemExpr->setBase(ObjectArg.take()); |
| 11459 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11460 | |
| 11461 | // Convert the rest of the arguments |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11462 | const FunctionProtoType *Proto = |
| 11463 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11464 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11465 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11466 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11467 | |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11468 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11469 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11470 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11471 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11472 | |
Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11473 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11474 | isa<CXXDestructorDecl>(CurContext)) && |
| 11475 | TheCall->getMethodDecl()->isPure()) { |
| 11476 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11477 | |
Chandler Carruth | 5925926 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11478 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11479 | Diag(MemExpr->getLocStart(), |
| 11480 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11481 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11482 | << MD->getParent()->getDeclName(); |
| 11483 | |
| 11484 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | 5925926 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11485 | } |
Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11486 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11487 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11488 | } |
| 11489 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11490 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11491 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11492 | /// overloaded function call operator (@c operator()) or performing a |
| 11493 | /// user-defined conversion on the object argument. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11494 | ExprResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11495 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11496 | SourceLocation LParenLoc, |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11497 | MultiExprArg Args, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11498 | SourceLocation RParenLoc) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11499 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11500 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11501 | ExprResult Object = Owned(Obj); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11502 | |
| 11503 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11504 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11505 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11506 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11507 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11508 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11509 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11510 | // C++ [over.call.object]p1: |
| 11511 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11512 | // 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] | 11513 | // candidate functions includes at least the function call |
| 11514 | // operators of T. The function call operators of T are obtained by |
| 11515 | // ordinary lookup of the name operator() in the context of |
| 11516 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11517 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11518 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11519 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11520 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11521 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11522 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11523 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11524 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11525 | LookupQualifiedName(R, Record->getDecl()); |
| 11526 | R.suppressDiagnostics(); |
| 11527 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11528 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11529 | Oper != OperEnd; ++Oper) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11530 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11531 | Object.get()->Classify(Context), |
| 11532 | Args, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11533 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11534 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11535 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11536 | // C++ [over.call.object]p2: |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11537 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11538 | // declared in T of the form |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11539 | // |
| 11540 | // operator conversion-type-id () cv-qualifier; |
| 11541 | // |
| 11542 | // where cv-qualifier is the same cv-qualification as, or a |
| 11543 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11544 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11545 | // R", or the type "reference to pointer to function of |
| 11546 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11547 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11548 | // is also considered as a candidate function. Similarly, |
| 11549 | // surrogate call functions are added to the set of candidate |
| 11550 | // functions for each conversion function declared in an |
| 11551 | // accessible base class provided the function is not hidden |
| 11552 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11553 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11554 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11555 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11556 | for (CXXRecordDecl::conversion_iterator |
| 11557 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11558 | NamedDecl *D = *I; |
| 11559 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11560 | if (isa<UsingShadowDecl>(D)) |
| 11561 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11562 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11563 | // Skip over templated conversion functions; they aren't |
| 11564 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11565 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11566 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11567 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11568 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11569 | if (!Conv->isExplicit()) { |
| 11570 | // Strip the reference type (if any) and then the pointer type (if |
| 11571 | // any) to get down to what might be a function type. |
| 11572 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11573 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11574 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11575 | |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11576 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11577 | { |
| 11578 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11579 | Object.get(), Args, CandidateSet); |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11580 | } |
| 11581 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11582 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11583 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11584 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11585 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11586 | // Perform overload resolution. |
| 11587 | OverloadCandidateSet::iterator Best; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11588 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11589 | Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11590 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11591 | // Overload resolution succeeded; we'll build the appropriate call |
| 11592 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11593 | break; |
| 11594 | |
| 11595 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11596 | if (CandidateSet.empty()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11597 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11598 | << Object.get()->getType() << /*call*/ 1 |
| 11599 | << Object.get()->getSourceRange(); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11600 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11601 | Diag(Object.get()->getLocStart(), |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11602 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11603 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11604 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11605 | break; |
| 11606 | |
| 11607 | case OR_Ambiguous: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11608 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11609 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11610 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11611 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11612 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11613 | |
| 11614 | case OR_Deleted: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11615 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11616 | diag::err_ovl_deleted_object_call) |
| 11617 | << Best->Function->isDeleted() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11618 | << Object.get()->getType() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11619 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11620 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11621 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11622 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11623 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11624 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11625 | if (Best == CandidateSet.end()) |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11626 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11627 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11628 | UnbridgedCasts.restore(); |
| 11629 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11630 | if (Best->Function == 0) { |
| 11631 | // Since there is no function declaration, this is one of the |
| 11632 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11633 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11634 | = cast<CXXConversionDecl>( |
| 11635 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11636 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11637 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11638 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11639 | return ExprError(); |
Faisal Vali | d667641 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11640 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11641 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11642 | // We selected one of the surrogate functions that converts the |
| 11643 | // object parameter to a function pointer. Perform the conversion |
| 11644 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11645 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11646 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11647 | // and then call it. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11648 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11649 | Conv, HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11650 | if (Call.isInvalid()) |
| 11651 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11652 | // Record usage of conversion in an implicit cast. |
| 11653 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11654 | CK_UserDefinedConversion, |
| 11655 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11656 | |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11657 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11658 | } |
| 11659 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11660 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11661 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11662 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11663 | // that calls this method, using Object for the implicit object |
| 11664 | // parameter and passing along the remaining arguments. |
| 11665 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | 1fefe41 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11666 | |
| 11667 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 9512d3f | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11668 | if (Method->isInvalidDecl()) |
Nico Weber | 1fefe41 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11669 | return ExprError(); |
| 11670 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11671 | const FunctionProtoType *Proto = |
| 11672 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11673 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 11674 | unsigned NumParams = Proto->getNumParams(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11675 | |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11676 | DeclarationNameInfo OpLocInfo( |
| 11677 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11678 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11679 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11680 | HadMultipleCandidates, |
| 11681 | OpLocInfo.getLoc(), |
| 11682 | OpLocInfo.getInfo()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11683 | if (NewFn.isInvalid()) |
| 11684 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11685 | |
Benjamin Kramer | 8b1a6bd | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11686 | // Build the full argument list for the method call (the implicit object |
| 11687 | // parameter is placed at the beginning of the list). |
| 11688 | llvm::OwningArrayPtr<Expr *> MethodArgs(new Expr*[Args.size() + 1]); |
| 11689 | MethodArgs[0] = Object.get(); |
| 11690 | std::copy(Args.begin(), Args.end(), &MethodArgs[1]); |
| 11691 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11692 | // Once we've built TheCall, all of the expressions are properly |
| 11693 | // owned. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11694 | QualType ResultTy = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11695 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11696 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11697 | |
Benjamin Kramer | 8b1a6bd | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11698 | CXXOperatorCallExpr *TheCall = new (Context) |
| 11699 | CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
| 11700 | llvm::makeArrayRef(MethodArgs.get(), Args.size() + 1), |
| 11701 | ResultTy, VK, RParenLoc, false); |
| 11702 | MethodArgs.reset(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11703 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11704 | if (CheckCallReturnType(Method->getReturnType(), LParenLoc, TheCall, Method)) |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11705 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11706 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11707 | // We may have default arguments. If so, we need to allocate more |
| 11708 | // slots in the call for them. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 11709 | if (Args.size() < NumParams) |
| 11710 | TheCall->setNumArgs(Context, NumParams + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11711 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11712 | bool IsError = false; |
| 11713 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11714 | // Initialize the implicit object parameter. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11715 | ExprResult ObjRes = |
| 11716 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11717 | Best->FoundDecl, Method); |
| 11718 | if (ObjRes.isInvalid()) |
| 11719 | IsError = true; |
| 11720 | else |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11721 | Object = ObjRes; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11722 | TheCall->setArg(0, Object.take()); |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11723 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11724 | // Check the argument types. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 11725 | for (unsigned i = 0; i != NumParams; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11726 | Expr *Arg; |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11727 | if (i < Args.size()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11728 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11729 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11730 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11731 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11732 | ExprResult InputInit |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11733 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11734 | Context, |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11735 | Method->getParamDecl(i)), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11736 | SourceLocation(), Arg); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11737 | |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11738 | IsError |= InputInit.isInvalid(); |
| 11739 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11740 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11741 | ExprResult DefArg |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11742 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11743 | if (DefArg.isInvalid()) { |
| 11744 | IsError = true; |
| 11745 | break; |
| 11746 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11747 | |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11748 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11749 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11750 | |
| 11751 | TheCall->setArg(i + 1, Arg); |
| 11752 | } |
| 11753 | |
| 11754 | // If this is a variadic call, handle args passed through "...". |
| 11755 | if (Proto->isVariadic()) { |
| 11756 | // Promote the arguments (C99 6.5.2.2p7). |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 11757 | for (unsigned i = NumParams, e = Args.size(); i < e; i++) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11758 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11759 | IsError |= Arg.isInvalid(); |
| 11760 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11761 | } |
| 11762 | } |
| 11763 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11764 | if (IsError) return true; |
| 11765 | |
Dmitri Gribenko | d3b7556 | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11766 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11767 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11768 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11769 | return true; |
| 11770 | |
John McCall | e172be5 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11771 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11772 | } |
| 11773 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11774 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11775 | /// (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] | 11776 | /// @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] | 11777 | ExprResult |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11778 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11779 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11780 | assert(Base->getType()->isRecordType() && |
| 11781 | "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11782 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11783 | if (checkPlaceholderForOverload(*this, Base)) |
| 11784 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11785 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11786 | SourceLocation Loc = Base->getExprLoc(); |
| 11787 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11788 | // C++ [over.ref]p1: |
| 11789 | // |
| 11790 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11791 | // for a class object x of type T if T::operator->() exists and if |
| 11792 | // the operator is selected as the best match function by the |
| 11793 | // overload resolution mechanism (13.3). |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11794 | DeclarationName OpName = |
| 11795 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11796 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11797 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11798 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11799 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11800 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11801 | return ExprError(); |
| 11802 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11803 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11804 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11805 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11806 | |
| 11807 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11808 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11809 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11810 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11811 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11812 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11813 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11814 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11815 | // Perform overload resolution. |
| 11816 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11817 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11818 | case OR_Success: |
| 11819 | // Overload resolution succeeded; we'll build the call below. |
| 11820 | break; |
| 11821 | |
| 11822 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11823 | if (CandidateSet.empty()) { |
| 11824 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | 0c51de4 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11825 | if (NoArrowOperatorFound) { |
| 11826 | // Report this specific error to the caller instead of emitting a |
| 11827 | // diagnostic, as requested. |
| 11828 | *NoArrowOperatorFound = true; |
| 11829 | return ExprError(); |
| 11830 | } |
Kaelyn Uhrain | bad7fb0 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11831 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11832 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11833 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | bad7fb0 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11834 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11835 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 1bb5dbf | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11836 | } |
| 11837 | } else |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11838 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11839 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11840 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11841 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11842 | |
| 11843 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11844 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11845 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11846 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11847 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11848 | |
| 11849 | case OR_Deleted: |
| 11850 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11851 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11852 | << "->" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11853 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11854 | << Base->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11855 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11856 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11857 | } |
| 11858 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11859 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11860 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11861 | // Convert the object parameter. |
| 11862 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11863 | ExprResult BaseResult = |
| 11864 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11865 | Best->FoundDecl, Method); |
| 11866 | if (BaseResult.isInvalid()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11867 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11868 | Base = BaseResult.take(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11869 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11870 | // Build the operator call. |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11871 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11872 | HadMultipleCandidates, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11873 | if (FnExpr.isInvalid()) |
| 11874 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11875 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11876 | QualType ResultTy = Method->getReturnType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11877 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11878 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11879 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11880 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11881 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11882 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11883 | if (CheckCallReturnType(Method->getReturnType(), OpLoc, TheCall, Method)) |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11884 | return ExprError(); |
Eli Friedman | 2d9c47e | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11885 | |
| 11886 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11887 | } |
| 11888 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11889 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11890 | /// a literal operator described by the provided lookup results. |
| 11891 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11892 | DeclarationNameInfo &SuffixInfo, |
| 11893 | ArrayRef<Expr*> Args, |
| 11894 | SourceLocation LitEndLoc, |
| 11895 | TemplateArgumentListInfo *TemplateArgs) { |
| 11896 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11897 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11898 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11899 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11900 | TemplateArgs); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11901 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11902 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11903 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11904 | // Perform overload resolution. This will usually be trivial, but might need |
| 11905 | // to perform substitutions for a literal operator template. |
| 11906 | OverloadCandidateSet::iterator Best; |
| 11907 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11908 | case OR_Success: |
| 11909 | case OR_Deleted: |
| 11910 | break; |
| 11911 | |
| 11912 | case OR_No_Viable_Function: |
| 11913 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11914 | << R.getLookupName(); |
| 11915 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11916 | return ExprError(); |
| 11917 | |
| 11918 | case OR_Ambiguous: |
| 11919 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11920 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11921 | return ExprError(); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11922 | } |
| 11923 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11924 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | 134af91 | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11925 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11926 | HadMultipleCandidates, |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11927 | SuffixInfo.getLoc(), |
| 11928 | SuffixInfo.getInfo()); |
| 11929 | if (Fn.isInvalid()) |
| 11930 | return true; |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11931 | |
| 11932 | // Check the argument types. This should almost always be a no-op, except |
| 11933 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11934 | Expr *ConvArgs[2]; |
Richard Smith | e54c307 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11935 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11936 | ExprResult InputInit = PerformCopyInitialization( |
| 11937 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11938 | SourceLocation(), Args[ArgIdx]); |
| 11939 | if (InputInit.isInvalid()) |
| 11940 | return true; |
| 11941 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11942 | } |
| 11943 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11944 | QualType ResultTy = FD->getReturnType(); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11945 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11946 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11947 | |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11948 | UserDefinedLiteral *UDL = |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11949 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11950 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11951 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11952 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 11953 | if (CheckCallReturnType(FD->getReturnType(), UDSuffixLoc, UDL, FD)) |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11954 | return ExprError(); |
| 11955 | |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11956 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11957 | return ExprError(); |
| 11958 | |
| 11959 | return MaybeBindToTemporary(UDL); |
| 11960 | } |
| 11961 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11962 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11963 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11964 | /// will be invoked. Otherwise, the function will be found via argument |
| 11965 | /// dependent lookup. |
| 11966 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11967 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11968 | /// is returned. |
| 11969 | Sema::ForRangeStatus |
| 11970 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11971 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11972 | BeginEndFunction BEF, |
| 11973 | const DeclarationNameInfo &NameInfo, |
| 11974 | LookupResult &MemberLookup, |
| 11975 | OverloadCandidateSet *CandidateSet, |
| 11976 | Expr *Range, ExprResult *CallExpr) { |
| 11977 | CandidateSet->clear(); |
| 11978 | if (!MemberLookup.empty()) { |
| 11979 | ExprResult MemberRef = |
| 11980 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11981 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11982 | /*TemplateKWLoc=*/SourceLocation(), |
| 11983 | /*FirstQualifierInScope=*/0, |
| 11984 | MemberLookup, |
| 11985 | /*TemplateArgs=*/0); |
| 11986 | if (MemberRef.isInvalid()) { |
| 11987 | *CallExpr = ExprError(); |
| 11988 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11989 | << RangeLoc << BEF << Range->getType(); |
| 11990 | return FRS_DiagnosticIssued; |
| 11991 | } |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11992 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11993 | if (CallExpr->isInvalid()) { |
| 11994 | *CallExpr = ExprError(); |
| 11995 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11996 | << RangeLoc << BEF << Range->getType(); |
| 11997 | return FRS_DiagnosticIssued; |
| 11998 | } |
| 11999 | } else { |
| 12000 | UnresolvedSet<0> FoundNames; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12001 | UnresolvedLookupExpr *Fn = |
| 12002 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 12003 | NestedNameSpecifierLoc(), NameInfo, |
| 12004 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b662674 | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 12005 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12006 | |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12007 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12008 | CandidateSet, CallExpr); |
| 12009 | if (CandidateSet->empty() || CandidateSetError) { |
| 12010 | *CallExpr = ExprError(); |
| 12011 | return FRS_NoViableFunction; |
| 12012 | } |
| 12013 | OverloadCandidateSet::iterator Best; |
| 12014 | OverloadingResult OverloadResult = |
| 12015 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 12016 | |
| 12017 | if (OverloadResult == OR_No_Viable_Function) { |
| 12018 | *CallExpr = ExprError(); |
| 12019 | return FRS_NoViableFunction; |
| 12020 | } |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 12021 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 12022 | Loc, 0, CandidateSet, &Best, |
| 12023 | OverloadResult, |
| 12024 | /*AllowTypoCorrection=*/false); |
| 12025 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 12026 | *CallExpr = ExprError(); |
| 12027 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 12028 | << RangeLoc << BEF << Range->getType(); |
| 12029 | return FRS_DiagnosticIssued; |
| 12030 | } |
| 12031 | } |
| 12032 | return FRS_Success; |
| 12033 | } |
| 12034 | |
| 12035 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12036 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 12037 | /// a C++ overloaded function (possibly with some parentheses and |
| 12038 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 12039 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 12040 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 12041 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12042 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12043 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12044 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 12045 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12046 | if (SubExpr == PE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12047 | return PE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12048 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12049 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12050 | } |
| 12051 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12052 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12053 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 12054 | Found, Fn); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12055 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12056 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 12057 | "Implicit cast type cannot be determined from overload"); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12058 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12059 | if (SubExpr == ICE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12060 | return ICE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12061 | |
| 12062 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 12063 | ICE->getCastKind(), |
| 12064 | SubExpr, 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 12065 | ICE->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12066 | } |
| 12067 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12068 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12069 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12070 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 12071 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 12072 | if (Method->isStatic()) { |
| 12073 | // Do nothing: static member functions aren't any different |
| 12074 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12075 | } else { |
Alp Toker | 028ed91 | 2013-12-06 17:56:43 +0000 | [diff] [blame] | 12076 | // Fix the subexpression, which really has to be an |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12077 | // UnresolvedLookupExpr holding an overloaded member function |
| 12078 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12079 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 12080 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12081 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12082 | return UnOp; |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12083 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12084 | assert(isa<DeclRefExpr>(SubExpr) |
| 12085 | && "fixed to something other than a decl ref"); |
| 12086 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 12087 | && "fixed to a member ref with no nested name qualifier"); |
| 12088 | |
| 12089 | // We have taken the address of a pointer to member |
| 12090 | // function. Perform the computation here so that we get the |
| 12091 | // appropriate pointer to member type. |
| 12092 | QualType ClassType |
| 12093 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 12094 | QualType MemPtrType |
| 12095 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 12096 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12097 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 12098 | VK_RValue, OK_Ordinary, |
| 12099 | UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 12100 | } |
| 12101 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12102 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 12103 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12104 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12105 | return UnOp; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12106 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12107 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12108 | Context.getPointerType(SubExpr->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12109 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12110 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12111 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12112 | |
| 12113 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12114 | // FIXME: avoid copy. |
| 12115 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12116 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12117 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 12118 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 12119 | } |
| 12120 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12121 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 12122 | ULE->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 12123 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12124 | Fn, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 12125 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12126 | ULE->getNameLoc(), |
| 12127 | Fn->getType(), |
| 12128 | VK_LValue, |
| 12129 | Found.getDecl(), |
| 12130 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12131 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12132 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 12133 | return DRE; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 12134 | } |
| 12135 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 12136 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 12137 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12138 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 12139 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 12140 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 12141 | TemplateArgs = &TemplateArgsBuffer; |
| 12142 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 12143 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12144 | Expr *Base; |
| 12145 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 12146 | // If we're filling in a static method where we used to have an |
| 12147 | // implicit member access, rewrite to a simple decl ref. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12148 | if (MemExpr->isImplicitAccess()) { |
| 12149 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12150 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 12151 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 12152 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12153 | Fn, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 12154 | /*enclosing*/ false, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12155 | MemExpr->getMemberLoc(), |
| 12156 | Fn->getType(), |
| 12157 | VK_LValue, |
| 12158 | Found.getDecl(), |
| 12159 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12160 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12161 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 12162 | return DRE; |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 12163 | } else { |
| 12164 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 12165 | if (MemExpr->getQualifier()) |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 12166 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 12167 | CheckCXXThisCapture(Loc); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 12168 | Base = new (Context) CXXThisExpr(Loc, |
| 12169 | MemExpr->getBaseType(), |
| 12170 | /*isImplicit=*/true); |
| 12171 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12172 | } else |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12173 | Base = MemExpr->getBase(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 12174 | |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 12175 | ExprValueKind valueKind; |
| 12176 | QualType type; |
| 12177 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 12178 | valueKind = VK_LValue; |
| 12179 | type = Fn->getType(); |
| 12180 | } else { |
| 12181 | valueKind = VK_RValue; |
| 12182 | type = Context.BoundMemberTy; |
| 12183 | } |
| 12184 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12185 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 12186 | MemExpr->isArrow(), |
| 12187 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 12188 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12189 | Fn, |
| 12190 | Found, |
| 12191 | MemExpr->getMemberNameInfo(), |
| 12192 | TemplateArgs, |
| 12193 | type, valueKind, OK_Ordinary); |
| 12194 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4f6a2c4 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 12195 | MarkMemberReferenced(ME); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12196 | return ME; |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12197 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12198 | |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12199 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12200 | } |
| 12201 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12202 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12203 | DeclAccessPair Found, |
| 12204 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12205 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 12206 | } |
| 12207 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 12208 | } // end namespace clang |