Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading -------------------------------===// |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 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 | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | |
| 37 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 39 | |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 40 | /// A convenience routine for creating a decayed reference to a function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 41 | static ExprResult |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 42 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| 43 | bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 44 | SourceLocation Loc = SourceLocation(), |
| 45 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 46 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
Faisal Vali | d570a92 | 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 | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 55 | return ExprError(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 56 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 57 | VK_LValue, Loc, LocInfo); |
| 58 | if (HadMultipleCandidates) |
| 59 | DRE->setHadMultipleCandidates(true); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 60 | |
| 61 | S.MarkDeclRefReferenced(DRE); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 62 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 63 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 64 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 65 | if (E.isInvalid()) |
| 66 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 67 | return E; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 70 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 71 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 72 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 73 | bool CStyle, |
| 74 | bool AllowObjCWritebackConversion); |
Sam Panzer | d012586 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 75 | |
Fariborz Jahanian | d97f558 | 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 | 120d63c | 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, |
| 85 | bool AllowExplicit); |
| 86 | |
| 87 | |
| 88 | static ImplicitConversionSequence::CompareKind |
| 89 | CompareStandardConversionSequences(Sema &S, |
| 90 | const StandardConversionSequence& SCS1, |
| 91 | const StandardConversionSequence& SCS2); |
| 92 | |
| 93 | static ImplicitConversionSequence::CompareKind |
| 94 | CompareQualificationConversions(Sema &S, |
| 95 | const StandardConversionSequence& SCS1, |
| 96 | const StandardConversionSequence& SCS2); |
| 97 | |
| 98 | static ImplicitConversionSequence::CompareKind |
| 99 | CompareDerivedToBaseConversions(Sema &S, |
| 100 | const StandardConversionSequence& SCS1, |
| 101 | const StandardConversionSequence& SCS2); |
| 102 | |
| 103 | |
| 104 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | /// GetConversionCategory - Retrieve the implicit conversion |
| 106 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 109 | static const ImplicitConversionCategory |
| 110 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 111 | ICC_Identity, |
| 112 | ICC_Lvalue_Transformation, |
| 113 | ICC_Lvalue_Transformation, |
| 114 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 115 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 116 | ICC_Qualification_Adjustment, |
| 117 | ICC_Promotion, |
| 118 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 119 | ICC_Promotion, |
| 120 | ICC_Conversion, |
| 121 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 122 | ICC_Conversion, |
| 123 | ICC_Conversion, |
| 124 | ICC_Conversion, |
| 125 | ICC_Conversion, |
| 126 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 127 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 128 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 129 | ICC_Conversion, |
| 130 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 131 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 132 | ICC_Conversion |
| 133 | }; |
| 134 | return Category[(int)Kind]; |
| 135 | } |
| 136 | |
| 137 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 138 | /// corresponding to the given implicit conversion kind. |
| 139 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 140 | static const ImplicitConversionRank |
| 141 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 142 | ICR_Exact_Match, |
| 143 | ICR_Exact_Match, |
| 144 | ICR_Exact_Match, |
| 145 | ICR_Exact_Match, |
| 146 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 147 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | ICR_Promotion, |
| 149 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 150 | ICR_Promotion, |
| 151 | ICR_Conversion, |
| 152 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | ICR_Conversion, |
| 154 | ICR_Conversion, |
| 155 | ICR_Conversion, |
| 156 | ICR_Conversion, |
| 157 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 158 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 159 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 160 | ICR_Conversion, |
| 161 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 162 | ICR_Complex_Real_Conversion, |
| 163 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 164 | ICR_Conversion, |
| 165 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 166 | }; |
| 167 | return Rank[(int)Kind]; |
| 168 | } |
| 169 | |
| 170 | /// GetImplicitConversionName - Return the name of this kind of |
| 171 | /// implicit conversion. |
| 172 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 173 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 174 | "No conversion", |
| 175 | "Lvalue-to-rvalue", |
| 176 | "Array-to-pointer", |
| 177 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 178 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 179 | "Qualification", |
| 180 | "Integral promotion", |
| 181 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 182 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 183 | "Integral conversion", |
| 184 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 185 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | "Floating-integral conversion", |
| 187 | "Pointer conversion", |
| 188 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 189 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 190 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 191 | "Derived-to-base conversion", |
| 192 | "Vector conversion", |
| 193 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 194 | "Complex-real conversion", |
| 195 | "Block Pointer conversion", |
| 196 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 198 | }; |
| 199 | return Name[Kind]; |
| 200 | } |
| 201 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 202 | /// StandardConversionSequence - Set the standard conversion |
| 203 | /// sequence to the identity conversion. |
| 204 | void StandardConversionSequence::setAsIdentityConversion() { |
| 205 | First = ICK_Identity; |
| 206 | Second = ICK_Identity; |
| 207 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 208 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 209 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 210 | ReferenceBinding = false; |
| 211 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 212 | IsLvalueReference = true; |
| 213 | BindsToFunctionLvalue = false; |
| 214 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 215 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 216 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 217 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 220 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 221 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 222 | /// implicit conversions. |
| 223 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 224 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 225 | if (GetConversionRank(First) > Rank) |
| 226 | Rank = GetConversionRank(First); |
| 227 | if (GetConversionRank(Second) > Rank) |
| 228 | Rank = GetConversionRank(Second); |
| 229 | if (GetConversionRank(Third) > Rank) |
| 230 | Rank = GetConversionRank(Third); |
| 231 | return Rank; |
| 232 | } |
| 233 | |
| 234 | /// isPointerConversionToBool - Determines whether this conversion is |
| 235 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 237 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 239 | // Note that FromType has not necessarily been transformed by the |
| 240 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 241 | // check for their presence as well as checking whether FromType is |
| 242 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 243 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 244 | (getFromType()->isPointerType() || |
| 245 | getFromType()->isObjCObjectPointerType() || |
| 246 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 247 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 248 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 249 | return true; |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 254 | /// isPointerConversionToVoidPointer - Determines whether this |
| 255 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 256 | /// used as part of the ranking of standard conversion sequences (C++ |
| 257 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 259 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 261 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 262 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 263 | |
| 264 | // Note that FromType has not necessarily been transformed by the |
| 265 | // array-to-pointer implicit conversion, so check for its presence |
| 266 | // and redo the conversion to get a pointer. |
| 267 | if (First == ICK_Array_To_Pointer) |
| 268 | FromType = Context.getArrayDecayedType(FromType); |
| 269 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 270 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 271 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 272 | return ToPtrType->getPointeeType()->isVoidType(); |
| 273 | |
| 274 | return false; |
| 275 | } |
| 276 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 277 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 278 | /// or after one in an implicit conversion. |
| 279 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 280 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 281 | switch (ICE->getCastKind()) { |
| 282 | case CK_NoOp: |
| 283 | case CK_IntegralCast: |
| 284 | case CK_IntegralToBoolean: |
| 285 | case CK_IntegralToFloating: |
| 286 | case CK_FloatingToIntegral: |
| 287 | case CK_FloatingToBoolean: |
| 288 | case CK_FloatingCast: |
| 289 | Converted = ICE->getSubExpr(); |
| 290 | continue; |
| 291 | |
| 292 | default: |
| 293 | return Converted; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return Converted; |
| 298 | } |
| 299 | |
| 300 | /// Check if this standard conversion sequence represents a narrowing |
| 301 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 302 | /// |
| 303 | /// \param Ctx The AST context. |
| 304 | /// \param Converted The result of applying this standard conversion sequence. |
| 305 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 306 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 307 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 308 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 309 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 310 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 311 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 312 | APValue &ConstantValue, |
| 313 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 314 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 315 | |
| 316 | // C++11 [dcl.init.list]p7: |
| 317 | // A narrowing conversion is an implicit conversion ... |
| 318 | QualType FromType = getToType(0); |
| 319 | QualType ToType = getToType(1); |
| 320 | switch (Second) { |
| 321 | // -- from a floating-point type to an integer type, or |
| 322 | // |
| 323 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 324 | // type, except where the source is a constant expression and the actual |
| 325 | // value after conversion will fit into the target type and will produce |
| 326 | // the original value when converted back to the original type, or |
| 327 | case ICK_Floating_Integral: |
| 328 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 329 | return NK_Type_Narrowing; |
| 330 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 331 | llvm::APSInt IntConstantValue; |
| 332 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 333 | if (Initializer && |
| 334 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 335 | // Convert the integer to the floating type. |
| 336 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 337 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 338 | llvm::APFloat::rmNearestTiesToEven); |
| 339 | // And back. |
| 340 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 341 | bool ignored; |
| 342 | Result.convertToInteger(ConvertedValue, |
| 343 | llvm::APFloat::rmTowardZero, &ignored); |
| 344 | // If the resulting value is different, this was a narrowing conversion. |
| 345 | if (IntConstantValue != ConvertedValue) { |
| 346 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 347 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 348 | return NK_Constant_Narrowing; |
| 349 | } |
| 350 | } else { |
| 351 | // Variables are always narrowings. |
| 352 | return NK_Variable_Narrowing; |
| 353 | } |
| 354 | } |
| 355 | return NK_Not_Narrowing; |
| 356 | |
| 357 | // -- from long double to double or float, or from double to float, except |
| 358 | // where the source is a constant expression and the actual value after |
| 359 | // conversion is within the range of values that can be represented (even |
| 360 | // if it cannot be represented exactly), or |
| 361 | case ICK_Floating_Conversion: |
| 362 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 363 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 364 | // FromType is larger than ToType. |
| 365 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 366 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 367 | // Constant! |
| 368 | assert(ConstantValue.isFloat()); |
| 369 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 370 | // Convert the source value into the target type. |
| 371 | bool ignored; |
| 372 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 373 | Ctx.getFloatTypeSemantics(ToType), |
| 374 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 375 | // If there was no overflow, the source value is within the range of |
| 376 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 377 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 378 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 379 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 380 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 381 | } else { |
| 382 | return NK_Variable_Narrowing; |
| 383 | } |
| 384 | } |
| 385 | return NK_Not_Narrowing; |
| 386 | |
| 387 | // -- from an integer type or unscoped enumeration type to an integer type |
| 388 | // that cannot represent all the values of the original type, except where |
| 389 | // the source is a constant expression and the actual value after |
| 390 | // conversion will fit into the target type and will produce the original |
| 391 | // value when converted back to the original type. |
| 392 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 393 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 394 | // Boolean conversions can be from pointers and pointers to members |
| 395 | // [conv.bool], and those aren't considered narrowing conversions. |
| 396 | return NK_Not_Narrowing; |
| 397 | } // Otherwise, fall through to the integral case. |
| 398 | case ICK_Integral_Conversion: { |
| 399 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 400 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 401 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 402 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 403 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 404 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 405 | |
| 406 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 407 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 408 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 409 | // Not all values of FromType can be represented in ToType. |
| 410 | llvm::APSInt InitializerValue; |
| 411 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 412 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 413 | // Such conversions on variables are always narrowing. |
| 414 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 415 | } |
| 416 | bool Narrowing = false; |
| 417 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 418 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 419 | // narrowing. |
| 420 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 421 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 422 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 423 | // Add a bit to the InitializerValue so we don't have to worry about |
| 424 | // signed vs. unsigned comparisons. |
| 425 | InitializerValue = InitializerValue.extend( |
| 426 | InitializerValue.getBitWidth() + 1); |
| 427 | // Convert the initializer to and from the target width and signed-ness. |
| 428 | llvm::APSInt ConvertedValue = InitializerValue; |
| 429 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 430 | ConvertedValue.setIsSigned(ToSigned); |
| 431 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 432 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 433 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 434 | if (ConvertedValue != InitializerValue) |
| 435 | Narrowing = true; |
| 436 | } |
| 437 | if (Narrowing) { |
| 438 | ConstantType = Initializer->getType(); |
| 439 | ConstantValue = APValue(InitializerValue); |
| 440 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | return NK_Not_Narrowing; |
| 444 | } |
| 445 | |
| 446 | default: |
| 447 | // Other kinds of conversions are not narrowings. |
| 448 | return NK_Not_Narrowing; |
| 449 | } |
| 450 | } |
| 451 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 452 | /// DebugPrint - Print this standard conversion sequence to standard |
| 453 | /// error. Useful for debugging overloading issues. |
| 454 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 455 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 456 | bool PrintedSomething = false; |
| 457 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 458 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 459 | PrintedSomething = true; |
| 460 | } |
| 461 | |
| 462 | if (Second != ICK_Identity) { |
| 463 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 464 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 465 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 466 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 467 | |
| 468 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 469 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 470 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 471 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 472 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 473 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 474 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 475 | PrintedSomething = true; |
| 476 | } |
| 477 | |
| 478 | if (Third != ICK_Identity) { |
| 479 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 480 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 481 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 482 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | PrintedSomething = true; |
| 484 | } |
| 485 | |
| 486 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 487 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 492 | /// error. Useful for debugging overloading issues. |
| 493 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 494 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | if (Before.First || Before.Second || Before.Third) { |
| 496 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 497 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 498 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 499 | if (ConversionFunction) |
| 500 | OS << '\'' << *ConversionFunction << '\''; |
| 501 | else |
| 502 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 503 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 504 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 505 | After.DebugPrint(); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 510 | /// error. Useful for debugging overloading issues. |
| 511 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 512 | raw_ostream &OS = llvm::errs(); |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 513 | if (isStdInitializerListElement()) |
| 514 | OS << "Worst std::initializer_list element conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 515 | switch (ConversionKind) { |
| 516 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 517 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 518 | Standard.DebugPrint(); |
| 519 | break; |
| 520 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 521 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 522 | UserDefined.DebugPrint(); |
| 523 | break; |
| 524 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 525 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 526 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 527 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 528 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 529 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 530 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 531 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 535 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 536 | } |
| 537 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 538 | void AmbiguousConversionSequence::construct() { |
| 539 | new (&conversions()) ConversionSet(); |
| 540 | } |
| 541 | |
| 542 | void AmbiguousConversionSequence::destruct() { |
| 543 | conversions().~ConversionSet(); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 548 | FromTypePtr = O.FromTypePtr; |
| 549 | ToTypePtr = O.ToTypePtr; |
| 550 | new (&conversions()) ConversionSet(O.conversions()); |
| 551 | } |
| 552 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 553 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 554 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 555 | // template argument information. |
| 556 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 557 | TemplateArgument FirstArg; |
| 558 | TemplateArgument SecondArg; |
| 559 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 560 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 561 | // template parameter and template argument information. |
| 562 | struct DFIParamWithArguments : DFIArguments { |
| 563 | TemplateParameter Param; |
| 564 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 565 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 567 | /// \brief Convert from Sema's representation of template deduction information |
| 568 | /// to the form used in overload-candidate information. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 569 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 570 | Sema::TemplateDeductionResult TDK, |
| 571 | TemplateDeductionInfo &Info) { |
| 572 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 573 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 574 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 575 | Result.Data = 0; |
| 576 | switch (TDK) { |
| 577 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 578 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 579 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 580 | case Sema::TDK_TooManyArguments: |
| 581 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 582 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 584 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 585 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 586 | Result.Data = Info.Param.getOpaqueValue(); |
| 587 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 588 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 589 | case Sema::TDK_NonDeducedMismatch: { |
| 590 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 591 | DFIArguments *Saved = new (Context) DFIArguments; |
| 592 | Saved->FirstArg = Info.FirstArg; |
| 593 | Saved->SecondArg = Info.SecondArg; |
| 594 | Result.Data = Saved; |
| 595 | break; |
| 596 | } |
| 597 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 598 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 599 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 600 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 601 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 602 | Saved->Param = Info.Param; |
| 603 | Saved->FirstArg = Info.FirstArg; |
| 604 | Saved->SecondArg = Info.SecondArg; |
| 605 | Result.Data = Saved; |
| 606 | break; |
| 607 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 609 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 610 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 611 | if (Info.hasSFINAEDiagnostic()) { |
| 612 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 613 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 614 | Info.takeSFINAEDiagnostic(*Diag); |
| 615 | Result.HasDiagnostic = true; |
| 616 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 617 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 619 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 620 | Result.Data = Info.Expression; |
| 621 | break; |
| 622 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 623 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 624 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 625 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 627 | return Result; |
| 628 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 629 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 630 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 631 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 632 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 633 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 634 | case Sema::TDK_InstantiationDepth: |
| 635 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 636 | case Sema::TDK_TooManyArguments: |
| 637 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 638 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 639 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 640 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 642 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 643 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 644 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 645 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 646 | Data = 0; |
| 647 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 648 | |
| 649 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 650 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 651 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 652 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 653 | Diag->~PartialDiagnosticAt(); |
| 654 | HasDiagnostic = false; |
| 655 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 656 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 658 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 659 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 663 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 664 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 665 | if (HasDiagnostic) |
| 666 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 667 | return 0; |
| 668 | } |
| 669 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 670 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 671 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 672 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 673 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 674 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 675 | case Sema::TDK_TooManyArguments: |
| 676 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 677 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 678 | case Sema::TDK_NonDeducedMismatch: |
| 679 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 680 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 682 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 683 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 685 | |
| 686 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 687 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 688 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 690 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 691 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 695 | return TemplateParameter(); |
| 696 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 698 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 699 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 700 | case Sema::TDK_Success: |
| 701 | case Sema::TDK_Invalid: |
| 702 | case Sema::TDK_InstantiationDepth: |
| 703 | case Sema::TDK_TooManyArguments: |
| 704 | case Sema::TDK_TooFewArguments: |
| 705 | case Sema::TDK_Incomplete: |
| 706 | case Sema::TDK_InvalidExplicitArguments: |
| 707 | case Sema::TDK_Inconsistent: |
| 708 | case Sema::TDK_Underqualified: |
| 709 | case Sema::TDK_NonDeducedMismatch: |
| 710 | case Sema::TDK_FailedOverloadResolution: |
| 711 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 712 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 713 | case Sema::TDK_SubstitutionFailure: |
| 714 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 715 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 716 | // Unhandled |
| 717 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 718 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 724 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 725 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 726 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 727 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 728 | case Sema::TDK_InstantiationDepth: |
| 729 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 730 | case Sema::TDK_TooManyArguments: |
| 731 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 732 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 733 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 734 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 735 | return 0; |
| 736 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 737 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 738 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 739 | case Sema::TDK_NonDeducedMismatch: |
| 740 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 742 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 743 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 744 | break; |
| 745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 747 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 748 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 749 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 750 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 751 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 752 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 753 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 754 | case Sema::TDK_InstantiationDepth: |
| 755 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 756 | case Sema::TDK_TooManyArguments: |
| 757 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 758 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 759 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 760 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 761 | return 0; |
| 762 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 763 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 764 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 765 | case Sema::TDK_NonDeducedMismatch: |
| 766 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 768 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 769 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 770 | break; |
| 771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 776 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 777 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 778 | Sema::TDK_FailedOverloadResolution) |
| 779 | return static_cast<Expr*>(Data); |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 784 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 785 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 786 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 787 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 788 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 789 | i->DeductionFailure.Destroy(); |
| 790 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void OverloadCandidateSet::clear() { |
| 794 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 795 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 796 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 797 | Functions.clear(); |
| 798 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 800 | namespace { |
| 801 | class UnbridgedCastsSet { |
| 802 | struct Entry { |
| 803 | Expr **Addr; |
| 804 | Expr *Saved; |
| 805 | }; |
| 806 | SmallVector<Entry, 2> Entries; |
| 807 | |
| 808 | public: |
| 809 | void save(Sema &S, Expr *&E) { |
| 810 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 811 | Entry entry = { &E, E }; |
| 812 | Entries.push_back(entry); |
| 813 | E = S.stripARCUnbridgedCast(E); |
| 814 | } |
| 815 | |
| 816 | void restore() { |
| 817 | for (SmallVectorImpl<Entry>::iterator |
| 818 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 819 | *i->Addr = i->Saved; |
| 820 | } |
| 821 | }; |
| 822 | } |
| 823 | |
| 824 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 825 | /// preprocessing on the given expression. |
| 826 | /// |
| 827 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 828 | /// without this, they will be immediately diagnosed as errors |
| 829 | /// |
| 830 | /// Return true on unrecoverable error. |
| 831 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 832 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 833 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 834 | // We can't handle overloaded expressions here because overload |
| 835 | // resolution might reasonably tweak them. |
| 836 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 837 | |
| 838 | // If the context potentially accepts unbridged ARC casts, strip |
| 839 | // the unbridged cast and add it to the collection for later restoration. |
| 840 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 841 | unbridgedCasts) { |
| 842 | unbridgedCasts->save(S, E); |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | // Go ahead and check everything else. |
| 847 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 848 | if (result.isInvalid()) |
| 849 | return true; |
| 850 | |
| 851 | E = result.take(); |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | // Nothing to do. |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 860 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 861 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 862 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 863 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 864 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 865 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 866 | return true; |
| 867 | |
| 868 | return false; |
| 869 | } |
| 870 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 871 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 872 | // overload of the declarations in Old. This routine returns false if |
| 873 | // New and Old cannot be overloaded, e.g., if New has the same |
| 874 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 875 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 876 | // it does return false, MatchedDecl will point to the decl that New |
| 877 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 878 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 879 | // |
| 880 | // Example: Given the following input: |
| 881 | // |
| 882 | // void f(int, float); // #1 |
| 883 | // void f(int, int); // #2 |
| 884 | // int f(int, int); // #3 |
| 885 | // |
| 886 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 888 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 889 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 890 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 891 | // (since they have different signatures), so this routine returns |
| 892 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 893 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 894 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 895 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 896 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 897 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 898 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 899 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 900 | // |
| 901 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 902 | // into a class by a using declaration. The rules for whether to hide |
| 903 | // shadow declarations ignore some properties which otherwise figure |
| 904 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 905 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 906 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 907 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 908 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 909 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 910 | NamedDecl *OldD = *I; |
| 911 | |
| 912 | bool OldIsUsingDecl = false; |
| 913 | if (isa<UsingShadowDecl>(OldD)) { |
| 914 | OldIsUsingDecl = true; |
| 915 | |
| 916 | // We can always introduce two using declarations into the same |
| 917 | // context, even if they have identical signatures. |
| 918 | if (NewIsUsingDecl) continue; |
| 919 | |
| 920 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 921 | } |
| 922 | |
| 923 | // If either declaration was introduced by a using declaration, |
| 924 | // we'll need to use slightly different rules for matching. |
| 925 | // Essentially, these rules are the normal rules, except that |
| 926 | // function templates hide function templates with different |
| 927 | // return types or template parameter lists. |
| 928 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 929 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 930 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 931 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 932 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 933 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 934 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 935 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 936 | continue; |
| 937 | } |
| 938 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 939 | Match = *I; |
| 940 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 941 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 942 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 943 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 944 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 945 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 946 | continue; |
| 947 | } |
| 948 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 949 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 950 | continue; |
| 951 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 952 | Match = *I; |
| 953 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 954 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 955 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 956 | // We can overload with these, which can show up when doing |
| 957 | // redeclaration checks for UsingDecls. |
| 958 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 959 | } else if (isa<TagDecl>(OldD)) { |
| 960 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 961 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 962 | // Optimistically assume that an unresolved using decl will |
| 963 | // overload; if it doesn't, we'll have to diagnose during |
| 964 | // template instantiation. |
| 965 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 966 | // (C++ 13p1): |
| 967 | // Only function declarations can be overloaded; object and type |
| 968 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 969 | Match = *I; |
| 970 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 971 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 972 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 973 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 974 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 977 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 978 | bool UseUsingDeclRules) { |
| 979 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 980 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 981 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 982 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 983 | // MSVCRT user defined entry points cannot be overloaded. |
| 984 | if (New->isMSVCRTEntryPoint()) |
| 985 | return false; |
| 986 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 987 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 988 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 989 | |
| 990 | // C++ [temp.fct]p2: |
| 991 | // A function template can be overloaded with other function templates |
| 992 | // and with normal (non-template) functions. |
| 993 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 994 | return true; |
| 995 | |
| 996 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 997 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 998 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 999 | |
| 1000 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 1001 | // determine whether they are overloads. If we find any mismatch |
| 1002 | // in the signature, they are overloads. |
| 1003 | |
| 1004 | // If either of these functions is a K&R-style function (no |
| 1005 | // prototype), then we consider them to have matching signatures. |
| 1006 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1007 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1008 | return false; |
| 1009 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1010 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1011 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1012 | |
| 1013 | // The signature of a function includes the types of its |
| 1014 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1015 | // of the ellipsis; see C++ DR 357). |
| 1016 | if (OldQType != NewQType && |
| 1017 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1018 | OldType->isVariadic() != NewType->isVariadic() || |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1019 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1020 | return true; |
| 1021 | |
| 1022 | // C++ [temp.over.link]p4: |
| 1023 | // The signature of a function template consists of its function |
| 1024 | // signature, its return type and its template parameter list. The names |
| 1025 | // of the template parameters are significant only for establishing the |
| 1026 | // relationship between the template parameters and the rest of the |
| 1027 | // signature. |
| 1028 | // |
| 1029 | // We check the return type and template parameter lists for function |
| 1030 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1031 | // |
| 1032 | // However, we don't consider either of these when deciding whether |
| 1033 | // a member introduced by a shadow declaration is hidden. |
| 1034 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1035 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1036 | OldTemplate->getTemplateParameters(), |
| 1037 | false, TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1038 | OldType->getResultType() != NewType->getResultType())) |
| 1039 | return true; |
| 1040 | |
| 1041 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1042 | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1043 | // |
| 1044 | // As part of this, also check whether one of the member functions |
| 1045 | // is static, in which case they are not overloads (C++ |
| 1046 | // 13.1p2). While not part of the definition of the signature, |
| 1047 | // this check is important to determine whether these functions |
| 1048 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1049 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1050 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1051 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1052 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1053 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1054 | if (!UseUsingDeclRules && |
| 1055 | (OldMethod->getRefQualifier() == RQ_None || |
| 1056 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1057 | // C++0x [over.load]p2: |
| 1058 | // - Member function declarations with the same name and the same |
| 1059 | // parameter-type-list as well as member function template |
| 1060 | // declarations with the same name, the same parameter-type-list, and |
| 1061 | // the same template parameter lists cannot be overloaded if any of |
| 1062 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1063 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1064 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1065 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1066 | } |
| 1067 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1068 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1070 | // We may not have applied the implicit const for a constexpr member |
| 1071 | // function yet (because we haven't yet resolved whether this is a static |
| 1072 | // or non-static member function). Add it now, on the assumption that this |
| 1073 | // is a redeclaration of OldMethod. |
David Majnemer | 62e9370 | 2013-11-03 23:51:28 +0000 | [diff] [blame^] | 1074 | unsigned OldQuals = OldMethod->getTypeQualifiers(); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1075 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1076 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1077 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1078 | NewQuals |= Qualifiers::Const; |
David Majnemer | 62e9370 | 2013-11-03 23:51:28 +0000 | [diff] [blame^] | 1079 | |
| 1080 | // We do not allow overloading based off of '__restrict'. |
| 1081 | OldQuals &= ~Qualifiers::Restrict; |
| 1082 | NewQuals &= ~Qualifiers::Restrict; |
| 1083 | if (OldQuals != NewQuals) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1084 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1085 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1086 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1087 | // The signatures match; this is not an overload. |
| 1088 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1091 | /// \brief Checks availability of the function depending on the current |
| 1092 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1093 | /// |
| 1094 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1095 | /// an available function, false otherwise. |
| 1096 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1097 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1098 | } |
| 1099 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1100 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1101 | /// |
| 1102 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1103 | /// is not an option. See TryImplicitConversion for more information. |
| 1104 | static ImplicitConversionSequence |
| 1105 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1106 | bool SuppressUserConversions, |
| 1107 | bool AllowExplicit, |
| 1108 | bool InOverloadResolution, |
| 1109 | bool CStyle, |
| 1110 | bool AllowObjCWritebackConversion) { |
| 1111 | ImplicitConversionSequence ICS; |
| 1112 | |
| 1113 | if (SuppressUserConversions) { |
| 1114 | // We're not in the case above, so there is no conversion that |
| 1115 | // we can perform. |
| 1116 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1117 | return ICS; |
| 1118 | } |
| 1119 | |
| 1120 | // Attempt user-defined conversion. |
| 1121 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1122 | OverloadingResult UserDefResult |
| 1123 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1124 | AllowExplicit); |
| 1125 | |
| 1126 | if (UserDefResult == OR_Success) { |
| 1127 | ICS.setUserDefined(); |
| 1128 | // C++ [over.ics.user]p4: |
| 1129 | // A conversion of an expression of class type to the same class |
| 1130 | // type is given Exact Match rank, and a conversion of an |
| 1131 | // expression of class type to a base class of that type is |
| 1132 | // given Conversion rank, in spite of the fact that a copy |
| 1133 | // constructor (i.e., a user-defined conversion function) is |
| 1134 | // called for those cases. |
| 1135 | if (CXXConstructorDecl *Constructor |
| 1136 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1137 | QualType FromCanon |
| 1138 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1139 | QualType ToCanon |
| 1140 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1141 | if (Constructor->isCopyConstructor() && |
| 1142 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1143 | // Turn this into a "standard" conversion sequence, so that it |
| 1144 | // gets ranked with standard conversion sequences. |
| 1145 | ICS.setStandard(); |
| 1146 | ICS.Standard.setAsIdentityConversion(); |
| 1147 | ICS.Standard.setFromType(From->getType()); |
| 1148 | ICS.Standard.setAllToTypes(ToType); |
| 1149 | ICS.Standard.CopyConstructor = Constructor; |
| 1150 | if (ToCanon != FromCanon) |
| 1151 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | // C++ [over.best.ics]p4: |
| 1156 | // However, when considering the argument of a user-defined |
| 1157 | // conversion function that is a candidate by 13.3.1.3 when |
| 1158 | // invoked for the copying of the temporary in the second step |
| 1159 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1160 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1161 | // ellipsis conversion sequences are allowed. |
| 1162 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1163 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1164 | } |
| 1165 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1166 | ICS.setAmbiguous(); |
| 1167 | ICS.Ambiguous.setFromType(From->getType()); |
| 1168 | ICS.Ambiguous.setToType(ToType); |
| 1169 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1170 | Cand != Conversions.end(); ++Cand) |
| 1171 | if (Cand->Viable) |
| 1172 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1173 | } else { |
| 1174 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1175 | } |
| 1176 | |
| 1177 | return ICS; |
| 1178 | } |
| 1179 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1180 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1181 | /// from the given expression (Expr) to the given type (ToType). This |
| 1182 | /// function returns an implicit conversion sequence that can be used |
| 1183 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1184 | /// |
| 1185 | /// void f(float f); |
| 1186 | /// void g(int i) { f(i); } |
| 1187 | /// |
| 1188 | /// this routine would produce an implicit conversion sequence to |
| 1189 | /// describe the initialization of f from i, which will be a standard |
| 1190 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1191 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1192 | // |
| 1193 | /// Note that this routine only determines how the conversion can be |
| 1194 | /// performed; it does not actually perform the conversion. As such, |
| 1195 | /// it will not produce any diagnostics if no conversion is available, |
| 1196 | /// but will instead return an implicit conversion sequence of kind |
| 1197 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1198 | /// |
| 1199 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1200 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1201 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1202 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1203 | /// |
| 1204 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1205 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1206 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1207 | static ImplicitConversionSequence |
| 1208 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1209 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1210 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1211 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1212 | bool CStyle, |
| 1213 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1214 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1215 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1216 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1217 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1218 | return ICS; |
| 1219 | } |
| 1220 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1221 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1222 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1223 | return ICS; |
| 1224 | } |
| 1225 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1226 | // C++ [over.ics.user]p4: |
| 1227 | // A conversion of an expression of class type to the same class |
| 1228 | // type is given Exact Match rank, and a conversion of an |
| 1229 | // expression of class type to a base class of that type is |
| 1230 | // given Conversion rank, in spite of the fact that a copy/move |
| 1231 | // constructor (i.e., a user-defined conversion function) is |
| 1232 | // called for those cases. |
| 1233 | QualType FromType = From->getType(); |
| 1234 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1235 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1236 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1237 | ICS.setStandard(); |
| 1238 | ICS.Standard.setAsIdentityConversion(); |
| 1239 | ICS.Standard.setFromType(FromType); |
| 1240 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1241 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1242 | // We don't actually check at this point whether there is a valid |
| 1243 | // copy/move constructor, since overloading just assumes that it |
| 1244 | // exists. When we actually perform initialization, we'll find the |
| 1245 | // appropriate constructor to copy the returned object, if needed. |
| 1246 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1247 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1248 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1249 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1250 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1252 | return ICS; |
| 1253 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1254 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1255 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1256 | AllowExplicit, InOverloadResolution, CStyle, |
| 1257 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1260 | ImplicitConversionSequence |
| 1261 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1262 | bool SuppressUserConversions, |
| 1263 | bool AllowExplicit, |
| 1264 | bool InOverloadResolution, |
| 1265 | bool CStyle, |
| 1266 | bool AllowObjCWritebackConversion) { |
| 1267 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1268 | SuppressUserConversions, AllowExplicit, |
| 1269 | InOverloadResolution, CStyle, |
| 1270 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1273 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1274 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1275 | /// converted expression. Flavor is the kind of conversion we're |
| 1276 | /// performing, used in the error message. If @p AllowExplicit, |
| 1277 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1278 | ExprResult |
| 1279 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1280 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1281 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1282 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1285 | ExprResult |
| 1286 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1287 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1288 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1289 | if (checkPlaceholderForOverload(*this, From)) |
| 1290 | return ExprError(); |
| 1291 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1292 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1293 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1294 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1295 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1296 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1297 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1298 | /*SuppressUserConversions=*/false, |
| 1299 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1300 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1301 | /*CStyle=*/false, |
| 1302 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1303 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1304 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1305 | |
| 1306 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1307 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1308 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1309 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1310 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1311 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1312 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1313 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1314 | // where F adds one of the following at most once: |
| 1315 | // - a pointer |
| 1316 | // - a member pointer |
| 1317 | // - a block pointer |
| 1318 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1319 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1320 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1321 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1322 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1323 | if (TyClass == Type::Pointer) { |
| 1324 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1325 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1326 | } else if (TyClass == Type::BlockPointer) { |
| 1327 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1328 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1329 | } else if (TyClass == Type::MemberPointer) { |
| 1330 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1331 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1332 | } else { |
| 1333 | return false; |
| 1334 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1335 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1336 | TyClass = CanTo->getTypeClass(); |
| 1337 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1338 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
| 1342 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1343 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1344 | if (!EInfo.getNoReturn()) return false; |
| 1345 | |
| 1346 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1347 | assert(QualType(FromFn, 0).isCanonical()); |
| 1348 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1349 | |
| 1350 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1351 | return true; |
| 1352 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1354 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1355 | /// vector conversion. |
| 1356 | /// |
| 1357 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1358 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1359 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1360 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1361 | // We need at least one of these types to be a vector type to have a vector |
| 1362 | // conversion. |
| 1363 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1364 | return false; |
| 1365 | |
| 1366 | // Identical types require no conversions. |
| 1367 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1368 | return false; |
| 1369 | |
| 1370 | // There are no conversions between extended vector types, only identity. |
| 1371 | if (ToType->isExtVectorType()) { |
| 1372 | // There are no conversions between extended vector types other than the |
| 1373 | // identity conversion. |
| 1374 | if (FromType->isExtVectorType()) |
| 1375 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1377 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1378 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1379 | ICK = ICK_Vector_Splat; |
| 1380 | return true; |
| 1381 | } |
| 1382 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1383 | |
| 1384 | // We can perform the conversion between vector types in the following cases: |
| 1385 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1386 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1387 | // same size |
| 1388 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1389 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1390 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1391 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1392 | ICK = ICK_Vector_Conversion; |
| 1393 | return true; |
| 1394 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1395 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1396 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1397 | return false; |
| 1398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1400 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1401 | bool InOverloadResolution, |
| 1402 | StandardConversionSequence &SCS, |
| 1403 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1405 | /// IsStandardConversion - Determines whether there is a standard |
| 1406 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1407 | /// expression From to the type ToType. Standard conversion sequences |
| 1408 | /// only consider non-class types; for conversions that involve class |
| 1409 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1410 | /// contain the standard conversion sequence required to perform this |
| 1411 | /// conversion and this routine will return true. Otherwise, this |
| 1412 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1413 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1414 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1415 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1416 | bool CStyle, |
| 1417 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1418 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1420 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1421 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1422 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1423 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1424 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1425 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1426 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1427 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1429 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1430 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1431 | return false; |
| 1432 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1436 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1437 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1438 | // (C++ 4p1). |
| 1439 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1440 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1441 | DeclAccessPair AccessPair; |
| 1442 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1443 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1444 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1445 | // We were able to resolve the address of the overloaded function, |
| 1446 | // so we can convert to the type of that function. |
| 1447 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1448 | |
| 1449 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1450 | // if the type matches (identity) or we are converting to bool |
| 1451 | if (!S.Context.hasSameUnqualifiedType( |
| 1452 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1453 | QualType resultTy; |
| 1454 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1455 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1456 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1457 | // otherwise, only a boolean conversion is standard |
| 1458 | if (!ToType->isBooleanType()) |
| 1459 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1460 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1461 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1462 | // Check if the "from" expression is taking the address of an overloaded |
| 1463 | // function and recompute the FromType accordingly. Take advantage of the |
| 1464 | // fact that non-static member functions *must* have such an address-of |
| 1465 | // expression. |
| 1466 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1467 | if (Method && !Method->isStatic()) { |
| 1468 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1469 | "Non-unary operator on non-static member address"); |
| 1470 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1471 | == UO_AddrOf && |
| 1472 | "Non-address-of operator on non-static member address"); |
| 1473 | const Type *ClassType |
| 1474 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1475 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1476 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1477 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1478 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1479 | "Non-address-of operator for overloaded function expression"); |
| 1480 | FromType = S.Context.getPointerType(FromType); |
| 1481 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1482 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1483 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1484 | assert(S.Context.hasSameType( |
| 1485 | FromType, |
| 1486 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1487 | } else { |
| 1488 | return false; |
| 1489 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1490 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1491 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1492 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1493 | // be converted to a prvalue. |
| 1494 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1495 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1496 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1497 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1498 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1499 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1500 | // C11 6.3.2.1p2: |
| 1501 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1502 | // of the type of the lvalue ... |
| 1503 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1504 | FromType = Atomic->getValueType(); |
| 1505 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1506 | // If T is a non-class type, the type of the rvalue is the |
| 1507 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1508 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1509 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1510 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1511 | } else if (FromType->isArrayType()) { |
| 1512 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1513 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1514 | |
| 1515 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1516 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1517 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1518 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1519 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1520 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1521 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1522 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1523 | |
| 1524 | // For the purpose of ranking in overload resolution |
| 1525 | // (13.3.3.1.1), this conversion is considered an |
| 1526 | // array-to-pointer conversion followed by a qualification |
| 1527 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1528 | SCS.Second = ICK_Identity; |
| 1529 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1530 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1531 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1532 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1533 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1534 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1535 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1536 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1537 | |
| 1538 | // An lvalue of function type T can be converted to an rvalue of |
| 1539 | // type "pointer to T." The result is a pointer to the |
| 1540 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1541 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1544 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1545 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1546 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1547 | |
| 1548 | // The second conversion can be an integral promotion, floating |
| 1549 | // point promotion, integral conversion, floating point conversion, |
| 1550 | // floating-integral conversion, pointer conversion, |
| 1551 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1552 | // For overloading in C, this can also be a "compatible-type" |
| 1553 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1554 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1555 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1556 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1557 | // The unqualified versions of the types are the same: there's no |
| 1558 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1559 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1560 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1562 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1563 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1564 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1565 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1566 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1567 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1568 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1569 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1570 | SCS.Second = ICK_Complex_Promotion; |
| 1571 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1572 | } else if (ToType->isBooleanType() && |
| 1573 | (FromType->isArithmeticType() || |
| 1574 | FromType->isAnyPointerType() || |
| 1575 | FromType->isBlockPointerType() || |
| 1576 | FromType->isMemberPointerType() || |
| 1577 | FromType->isNullPtrType())) { |
| 1578 | // Boolean conversions (C++ 4.12). |
| 1579 | SCS.Second = ICK_Boolean_Conversion; |
| 1580 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1581 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1582 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1583 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1584 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1585 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1586 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1587 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1588 | SCS.Second = ICK_Complex_Conversion; |
| 1589 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1590 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1591 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1592 | // Complex-real conversions (C99 6.3.1.7) |
| 1593 | SCS.Second = ICK_Complex_Real; |
| 1594 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1595 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1596 | // Floating point conversions (C++ 4.8). |
| 1597 | SCS.Second = ICK_Floating_Conversion; |
| 1598 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1599 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1600 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1601 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1602 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1603 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1604 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1605 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1606 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1607 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1608 | } else if (AllowObjCWritebackConversion && |
| 1609 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1610 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1611 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1612 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1613 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1614 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1615 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1616 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1617 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1618 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1619 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1620 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1621 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1622 | SCS.Second = SecondICK; |
| 1623 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1624 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1625 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1626 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1627 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1628 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1629 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1630 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1631 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1632 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1633 | InOverloadResolution, |
| 1634 | SCS, CStyle)) { |
| 1635 | SCS.Second = ICK_TransparentUnionConversion; |
| 1636 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1637 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1638 | CStyle)) { |
| 1639 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1640 | // appropriately. |
| 1641 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1642 | } else if (ToType->isEventT() && |
| 1643 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1644 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1645 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1646 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1647 | } else { |
| 1648 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1649 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1650 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1651 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1652 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1653 | QualType CanonFrom; |
| 1654 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1655 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1656 | bool ObjCLifetimeConversion; |
| 1657 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1658 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1659 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1660 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1661 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1662 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1663 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1664 | } else { |
| 1665 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1666 | SCS.Third = ICK_Identity; |
| 1667 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1669 | // [...] Any difference in top-level cv-qualification is |
| 1670 | // subsumed by the initialization itself and does not constitute |
| 1671 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1672 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1673 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1675 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1676 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1677 | FromType = ToType; |
| 1678 | CanonFrom = CanonTo; |
| 1679 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1680 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1681 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1682 | |
| 1683 | // If we have not converted the argument type to the parameter type, |
| 1684 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1685 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1686 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1688 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1689 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1690 | |
| 1691 | static bool |
| 1692 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1693 | QualType &ToType, |
| 1694 | bool InOverloadResolution, |
| 1695 | StandardConversionSequence &SCS, |
| 1696 | bool CStyle) { |
| 1697 | |
| 1698 | const RecordType *UT = ToType->getAsUnionType(); |
| 1699 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1700 | return false; |
| 1701 | // The field to initialize within the transparent union. |
| 1702 | RecordDecl *UD = UT->getDecl(); |
| 1703 | // It's compatible if the expression matches any of the fields. |
| 1704 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1705 | itend = UD->field_end(); |
| 1706 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1707 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1708 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1709 | ToType = it->getType(); |
| 1710 | return true; |
| 1711 | } |
| 1712 | } |
| 1713 | return false; |
| 1714 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1715 | |
| 1716 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1717 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1718 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1719 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1721 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1722 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1723 | if (!To) { |
| 1724 | return false; |
| 1725 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1726 | |
| 1727 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1728 | // unsigned short int can be converted to an rvalue of type int if |
| 1729 | // int can represent all the values of the source type; otherwise, |
| 1730 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1731 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1732 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1733 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1734 | if (// We can promote any signed, promotable integer type to an int |
| 1735 | (FromType->isSignedIntegerType() || |
| 1736 | // We can promote any unsigned integer type whose size is |
| 1737 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1739 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1740 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1743 | return To->getKind() == BuiltinType::UInt; |
| 1744 | } |
| 1745 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1746 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1747 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1748 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1749 | // following types that can represent all the values of the enumeration |
| 1750 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1751 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1752 | // long long int. If none of the types in that list can represent all the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1753 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1754 | // type can be converted to an rvalue a prvalue of the extended integer type |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1755 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1756 | // long in which all the values of the enumeration can be represented. If |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1757 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1758 | // C++11 [conv.prom]p4: |
| 1759 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1760 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1761 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1762 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1763 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1764 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1765 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1766 | // provided for a scoped enumeration. |
| 1767 | if (FromEnumType->getDecl()->isScoped()) |
| 1768 | return false; |
| 1769 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1770 | // We can perform an integral promotion to the underlying type of the enum, |
| 1771 | // even if that's not the promoted type. |
| 1772 | if (FromEnumType->getDecl()->isFixed()) { |
| 1773 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1774 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1775 | IsIntegralPromotion(From, Underlying, ToType); |
| 1776 | } |
| 1777 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1778 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1779 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1780 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1781 | return Context.hasSameUnqualifiedType(ToType, |
| 1782 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1783 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1785 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1786 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1787 | // to an rvalue a prvalue of the first of the following types that can |
| 1788 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1789 | // long int, unsigned long int, long long int, or unsigned long long int. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1790 | // If none of the types in that list can represent all the values of its |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1791 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1792 | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1793 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1794 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1795 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1796 | // Determine whether the type we're converting from is signed or |
| 1797 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1798 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1799 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1801 | // The types we'll try to promote to, in the appropriate |
| 1802 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | QualType PromoteTypes[6] = { |
| 1804 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1805 | Context.LongTy, Context.UnsignedLongTy , |
| 1806 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1807 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1808 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1809 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1810 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1812 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1813 | // We found the type that we can promote to. If this is the |
| 1814 | // type we wanted, we have a promotion. Otherwise, no |
| 1815 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1816 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1822 | // rvalue of type int if int can represent all the values of the |
| 1823 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1824 | // unsigned int can represent all the values of the bit-field. If |
| 1825 | // the bit-field is larger yet, no integral promotion applies to |
| 1826 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1827 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1828 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1829 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1830 | using llvm::APSInt; |
| 1831 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1832 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1833 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1834 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1835 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1836 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1837 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1839 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1840 | if (BitWidth < ToSize || |
| 1841 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1842 | return To->getKind() == BuiltinType::Int; |
| 1843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1845 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1846 | // that fits into an unsigned int? |
| 1847 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1848 | return To->getKind() == BuiltinType::UInt; |
| 1849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1851 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1853 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1855 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1856 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1857 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1858 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1859 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1860 | |
| 1861 | return false; |
| 1862 | } |
| 1863 | |
| 1864 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1865 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1866 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1868 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1869 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1870 | /// An rvalue of type float can be converted to an rvalue of type |
| 1871 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1872 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1873 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1874 | return true; |
| 1875 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1876 | // C99 6.3.1.5p1: |
| 1877 | // When a float is promoted to double or long double, or a |
| 1878 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1879 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1880 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1881 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1882 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1883 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1884 | |
| 1885 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1886 | if (!getLangOpts().NativeHalfType && |
| 1887 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1888 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1889 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1892 | return false; |
| 1893 | } |
| 1894 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1895 | /// \brief Determine if a conversion is a complex promotion. |
| 1896 | /// |
| 1897 | /// A complex promotion is defined as a complex -> complex conversion |
| 1898 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1899 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1900 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1901 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1902 | if (!FromComplex) |
| 1903 | return false; |
| 1904 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1905 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1906 | if (!ToComplex) |
| 1907 | return false; |
| 1908 | |
| 1909 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1910 | ToComplex->getElementType()) || |
| 1911 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1912 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1915 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1916 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1917 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1918 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1919 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1920 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1922 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1923 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1924 | ASTContext &Context, |
| 1925 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1926 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1927 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1928 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1929 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1930 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1931 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1932 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1933 | |
| 1934 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1935 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1936 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1937 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1939 | if (StripObjCLifetime) |
| 1940 | Quals.removeObjCLifetime(); |
| 1941 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1943 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1944 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1945 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1946 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1947 | |
| 1948 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1949 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1950 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1951 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1952 | return Context.getPointerType(ToPointee); |
| 1953 | } |
| 1954 | |
| 1955 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1956 | QualType QualifiedCanonToPointee |
| 1957 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1959 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1960 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1961 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1962 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1963 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1965 | bool InOverloadResolution, |
| 1966 | ASTContext &Context) { |
| 1967 | // Handle value-dependent integral null pointer constants correctly. |
| 1968 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1969 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1970 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1971 | return !InOverloadResolution; |
| 1972 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1973 | return Expr->isNullPointerConstant(Context, |
| 1974 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1975 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1976 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1978 | /// IsPointerConversion - Determines whether the conversion of the |
| 1979 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1980 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1981 | /// 4.10). If so, returns true and places the converted type (that |
| 1982 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1983 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1984 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1985 | /// This routine also supports conversions to and from block pointers |
| 1986 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1987 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1988 | /// appropriate overloading rules for Objective-C, we may want to |
| 1989 | /// split the Objective-C checks into a different routine; however, |
| 1990 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1991 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1992 | /// set if the conversion is an allowed Objective-C conversion that |
| 1993 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1994 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1995 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1996 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1998 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1999 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 2000 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2001 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2002 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 2004 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2005 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 2006 | ConvertedType = ToType; |
| 2007 | return true; |
| 2008 | } |
| 2009 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2010 | // Blocks: Block pointers can be converted to void*. |
| 2011 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2012 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2013 | ConvertedType = ToType; |
| 2014 | return true; |
| 2015 | } |
| 2016 | // Blocks: A null pointer constant can be converted to a block |
| 2017 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2019 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2020 | ConvertedType = ToType; |
| 2021 | return true; |
| 2022 | } |
| 2023 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2024 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2025 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2027 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2028 | ConvertedType = ToType; |
| 2029 | return true; |
| 2030 | } |
| 2031 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2032 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2033 | if (!ToTypePtr) |
| 2034 | return false; |
| 2035 | |
| 2036 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2037 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2038 | ConvertedType = ToType; |
| 2039 | return true; |
| 2040 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2041 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2043 | // , including objective-c pointers. |
| 2044 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2045 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2046 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2047 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2048 | FromType->getAs<ObjCObjectPointerType>(), |
| 2049 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2050 | ToType, Context); |
| 2051 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2052 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2053 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2054 | if (!FromTypePtr) |
| 2055 | return false; |
| 2056 | |
| 2057 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2058 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2059 | // If the unqualified pointee types are the same, this can't be a |
Douglas Gregor | 4e938f57b | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 2060 | // pointer conversion, so don't do all of the work below. |
| 2061 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2062 | return false; |
| 2063 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2064 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2065 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2066 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2067 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2068 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2070 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2071 | ToType, Context, |
| 2072 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2073 | return true; |
| 2074 | } |
| 2075 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2076 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2077 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2078 | ToPointeeType->isVoidType()) { |
| 2079 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2080 | ToPointeeType, |
| 2081 | ToType, Context); |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2085 | // When we're overloading in C, we allow a special kind of pointer |
| 2086 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2087 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2088 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2090 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2092 | return true; |
| 2093 | } |
| 2094 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2095 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2097 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2098 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2099 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2100 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2101 | // necessitates this conversion is ill-formed. The result of the |
| 2102 | // conversion is a pointer to the base class sub-object of the |
| 2103 | // derived class object. The null pointer value is converted to |
| 2104 | // the null pointer value of the destination type. |
| 2105 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2106 | // Note that we do not check for ambiguity or inaccessibility |
| 2107 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2108 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2109 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2110 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2111 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2112 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2114 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2115 | ToType, Context); |
| 2116 | return true; |
| 2117 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2118 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2119 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2120 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2121 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2122 | ToPointeeType, |
| 2123 | ToType, Context); |
| 2124 | return true; |
| 2125 | } |
| 2126 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2127 | return false; |
| 2128 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2129 | |
| 2130 | /// \brief Adopt the given qualifiers for the given type. |
| 2131 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2132 | Qualifiers TQs = T.getQualifiers(); |
| 2133 | |
| 2134 | // Check whether qualifiers already match. |
| 2135 | if (TQs == Qs) |
| 2136 | return T; |
| 2137 | |
| 2138 | if (Qs.compatiblyIncludes(TQs)) |
| 2139 | return Context.getQualifiedType(T, Qs); |
| 2140 | |
| 2141 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2142 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2143 | |
| 2144 | /// isObjCPointerConversion - Determines whether this is an |
| 2145 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2146 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2148 | QualType& ConvertedType, |
| 2149 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2150 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2151 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2153 | // The set of qualifiers on the type we're converting from. |
| 2154 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2155 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2156 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2157 | const ObjCObjectPointerType* ToObjCPtr = |
| 2158 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2160 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2161 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2162 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2163 | // If the pointee types are the same (ignoring qualifications), |
| 2164 | // then this is not a pointer conversion. |
| 2165 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2166 | FromObjCPtr->getPointeeType())) |
| 2167 | return false; |
| 2168 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2169 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2170 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2171 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2172 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2173 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2174 | return true; |
| 2175 | } |
| 2176 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2178 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2180 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2181 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2182 | return true; |
| 2183 | } |
| 2184 | // Objective C++: We're able to convert from a pointer to an |
| 2185 | // interface to a pointer to a different interface. |
| 2186 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2187 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2188 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2189 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2190 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2191 | FromObjCPtr->getPointeeType())) |
| 2192 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2193 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2194 | ToObjCPtr->getPointeeType(), |
| 2195 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2196 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2197 | return true; |
| 2198 | } |
| 2199 | |
| 2200 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2201 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2202 | // interfaces, which is permitted. However, we're going to |
| 2203 | // complain about it. |
| 2204 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2205 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2206 | ToObjCPtr->getPointeeType(), |
| 2207 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2208 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2209 | return true; |
| 2210 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2211 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2212 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2213 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2214 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2215 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2216 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2217 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2218 | // Objective C++: We're able to convert from a pointer to any object |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2219 | // to a block pointer type. |
| 2220 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2221 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2222 | return true; |
| 2223 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2224 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2225 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2226 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2227 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2228 | // Objective C++: We're able to convert from a block pointer type to a |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2229 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2230 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2231 | return true; |
| 2232 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2233 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2234 | return false; |
| 2235 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2236 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2237 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2238 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2239 | else if (const BlockPointerType *FromBlockPtr = |
| 2240 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2241 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2242 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2243 | return false; |
| 2244 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2245 | // If we have pointers to pointers, recursively check whether this |
| 2246 | // is an Objective-C conversion. |
| 2247 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2248 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2249 | IncompatibleObjC)) { |
| 2250 | // We always complain about this conversion. |
| 2251 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2252 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2253 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2254 | return true; |
| 2255 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2256 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2257 | // as in I* to id. |
| 2258 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2259 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2260 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2261 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2263 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2264 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2265 | return true; |
| 2266 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2268 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2269 | // differences in the argument and result types are in Objective-C |
| 2270 | // pointer conversions. If so, we permit the conversion (but |
| 2271 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2273 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2274 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2275 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2276 | if (FromFunctionType && ToFunctionType) { |
| 2277 | // If the function types are exactly the same, this isn't an |
| 2278 | // Objective-C pointer conversion. |
| 2279 | if (Context.getCanonicalType(FromPointeeType) |
| 2280 | == Context.getCanonicalType(ToPointeeType)) |
| 2281 | return false; |
| 2282 | |
| 2283 | // Perform the quick checks that will tell us whether these |
| 2284 | // function types are obviously different. |
| 2285 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2286 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2287 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2288 | return false; |
| 2289 | |
| 2290 | bool HasObjCConversion = false; |
| 2291 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2292 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2293 | // Okay, the types match exactly. Nothing to do. |
| 2294 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2295 | ToFunctionType->getResultType(), |
| 2296 | ConvertedType, IncompatibleObjC)) { |
| 2297 | // Okay, we have an Objective-C pointer conversion. |
| 2298 | HasObjCConversion = true; |
| 2299 | } else { |
| 2300 | // Function types are too different. Abort. |
| 2301 | return false; |
| 2302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2304 | // Check argument types. |
| 2305 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2306 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2307 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2308 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2309 | if (Context.getCanonicalType(FromArgType) |
| 2310 | == Context.getCanonicalType(ToArgType)) { |
| 2311 | // Okay, the types match exactly. Nothing to do. |
| 2312 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2313 | ConvertedType, IncompatibleObjC)) { |
| 2314 | // Okay, we have an Objective-C pointer conversion. |
| 2315 | HasObjCConversion = true; |
| 2316 | } else { |
| 2317 | // Argument types are too different. Abort. |
| 2318 | return false; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | if (HasObjCConversion) { |
| 2323 | // We had an Objective-C conversion. Allow this pointer |
| 2324 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2325 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2326 | IncompatibleObjC = true; |
| 2327 | return true; |
| 2328 | } |
| 2329 | } |
| 2330 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2331 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2332 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2333 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2334 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2335 | /// used for parameter passing when performing automatic reference counting. |
| 2336 | /// |
| 2337 | /// \param FromType The type we're converting form. |
| 2338 | /// |
| 2339 | /// \param ToType The type we're converting to. |
| 2340 | /// |
| 2341 | /// \param ConvertedType The type that will be produced after applying |
| 2342 | /// this conversion. |
| 2343 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2344 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2345 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2346 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2347 | return false; |
| 2348 | |
| 2349 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2350 | QualType ToPointee; |
| 2351 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2352 | ToPointee = ToPointer->getPointeeType(); |
| 2353 | else |
| 2354 | return false; |
| 2355 | |
| 2356 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2357 | if (!ToPointee->isObjCLifetimeType() || |
| 2358 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2359 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2360 | return false; |
| 2361 | |
| 2362 | // Argument must be a pointer to __strong to __weak. |
| 2363 | QualType FromPointee; |
| 2364 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2365 | FromPointee = FromPointer->getPointeeType(); |
| 2366 | else |
| 2367 | return false; |
| 2368 | |
| 2369 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2370 | if (!FromPointee->isObjCLifetimeType() || |
| 2371 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2372 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2373 | return false; |
| 2374 | |
| 2375 | // Make sure that we have compatible qualifiers. |
| 2376 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2377 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2378 | return false; |
| 2379 | |
| 2380 | // Remove qualifiers from the pointee type we're converting from; they |
| 2381 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2382 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2383 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2384 | |
| 2385 | // The unqualified form of the pointee types must be compatible. |
| 2386 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2387 | bool IncompatibleObjC; |
| 2388 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2389 | FromPointee = ToPointee; |
| 2390 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2391 | IncompatibleObjC)) |
| 2392 | return false; |
| 2393 | |
| 2394 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2395 | /// __autoreleasing pointee. |
| 2396 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2397 | ConvertedType = Context.getPointerType(FromPointee); |
| 2398 | return true; |
| 2399 | } |
| 2400 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2401 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2402 | QualType& ConvertedType) { |
| 2403 | QualType ToPointeeType; |
| 2404 | if (const BlockPointerType *ToBlockPtr = |
| 2405 | ToType->getAs<BlockPointerType>()) |
| 2406 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2407 | else |
| 2408 | return false; |
| 2409 | |
| 2410 | QualType FromPointeeType; |
| 2411 | if (const BlockPointerType *FromBlockPtr = |
| 2412 | FromType->getAs<BlockPointerType>()) |
| 2413 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2414 | else |
| 2415 | return false; |
| 2416 | // We have pointer to blocks, check whether the only |
| 2417 | // differences in the argument and result types are in Objective-C |
| 2418 | // pointer conversions. If so, we permit the conversion. |
| 2419 | |
| 2420 | const FunctionProtoType *FromFunctionType |
| 2421 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2422 | const FunctionProtoType *ToFunctionType |
| 2423 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2424 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2425 | if (!FromFunctionType || !ToFunctionType) |
| 2426 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2427 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2428 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2429 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2430 | |
| 2431 | // Perform the quick checks that will tell us whether these |
| 2432 | // function types are obviously different. |
| 2433 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2434 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2435 | return false; |
| 2436 | |
| 2437 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2438 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2439 | if (FromEInfo != ToEInfo) |
| 2440 | return false; |
| 2441 | |
| 2442 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2443 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2444 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2445 | // Okay, the types match exactly. Nothing to do. |
| 2446 | } else { |
| 2447 | QualType RHS = FromFunctionType->getResultType(); |
| 2448 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2449 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2450 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2451 | LHS = LHS.getUnqualifiedType(); |
| 2452 | |
| 2453 | if (Context.hasSameType(RHS,LHS)) { |
| 2454 | // OK exact match. |
| 2455 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2456 | ConvertedType, IncompatibleObjC)) { |
| 2457 | if (IncompatibleObjC) |
| 2458 | return false; |
| 2459 | // Okay, we have an Objective-C pointer conversion. |
| 2460 | } |
| 2461 | else |
| 2462 | return false; |
| 2463 | } |
| 2464 | |
| 2465 | // Check argument types. |
| 2466 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2467 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2468 | IncompatibleObjC = false; |
| 2469 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2470 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2471 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2472 | // Okay, the types match exactly. Nothing to do. |
| 2473 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2474 | ConvertedType, IncompatibleObjC)) { |
| 2475 | if (IncompatibleObjC) |
| 2476 | return false; |
| 2477 | // Okay, we have an Objective-C pointer conversion. |
| 2478 | } else |
| 2479 | // Argument types are too different. Abort. |
| 2480 | return false; |
| 2481 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2482 | if (LangOpts.ObjCAutoRefCount && |
| 2483 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2484 | ToFunctionType)) |
| 2485 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2486 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2487 | ConvertedType = ToType; |
| 2488 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2491 | enum { |
| 2492 | ft_default, |
| 2493 | ft_different_class, |
| 2494 | ft_parameter_arity, |
| 2495 | ft_parameter_mismatch, |
| 2496 | ft_return_type, |
| 2497 | ft_qualifer_mismatch |
| 2498 | }; |
| 2499 | |
| 2500 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2501 | /// function types. Catches different number of parameter, mismatch in |
| 2502 | /// parameter types, and different return types. |
| 2503 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2504 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2505 | // If either type is not valid, include no extra info. |
| 2506 | if (FromType.isNull() || ToType.isNull()) { |
| 2507 | PDiag << ft_default; |
| 2508 | return; |
| 2509 | } |
| 2510 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2511 | // Get the function type from the pointers. |
| 2512 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2513 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2514 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2515 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2516 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2517 | << QualType(FromMember->getClass(), 0); |
| 2518 | return; |
| 2519 | } |
| 2520 | FromType = FromMember->getPointeeType(); |
| 2521 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2524 | if (FromType->isPointerType()) |
| 2525 | FromType = FromType->getPointeeType(); |
| 2526 | if (ToType->isPointerType()) |
| 2527 | ToType = ToType->getPointeeType(); |
| 2528 | |
| 2529 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2530 | FromType = FromType.getNonReferenceType(); |
| 2531 | ToType = ToType.getNonReferenceType(); |
| 2532 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2533 | // Don't print extra info for non-specialized template functions. |
| 2534 | if (FromType->isInstantiationDependentType() && |
| 2535 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2536 | PDiag << ft_default; |
| 2537 | return; |
| 2538 | } |
| 2539 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2540 | // No extra info for same types. |
| 2541 | if (Context.hasSameType(FromType, ToType)) { |
| 2542 | PDiag << ft_default; |
| 2543 | return; |
| 2544 | } |
| 2545 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2546 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2547 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2548 | |
| 2549 | // Both types need to be function types. |
| 2550 | if (!FromFunction || !ToFunction) { |
| 2551 | PDiag << ft_default; |
| 2552 | return; |
| 2553 | } |
| 2554 | |
| 2555 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2556 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2557 | << FromFunction->getNumArgs(); |
| 2558 | return; |
| 2559 | } |
| 2560 | |
| 2561 | // Handle different parameter types. |
| 2562 | unsigned ArgPos; |
| 2563 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2564 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2565 | << ToFunction->getArgType(ArgPos) |
| 2566 | << FromFunction->getArgType(ArgPos); |
| 2567 | return; |
| 2568 | } |
| 2569 | |
| 2570 | // Handle different return type. |
| 2571 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2572 | ToFunction->getResultType())) { |
| 2573 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2574 | << FromFunction->getResultType(); |
| 2575 | return; |
| 2576 | } |
| 2577 | |
| 2578 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2579 | ToQuals = ToFunction->getTypeQuals(); |
| 2580 | if (FromQuals != ToQuals) { |
| 2581 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2582 | return; |
| 2583 | } |
| 2584 | |
| 2585 | // Unable to find a difference, so add no extra info. |
| 2586 | PDiag << ft_default; |
| 2587 | } |
| 2588 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2589 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2590 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2591 | /// they have same number of arguments. If the parameters are different, |
| 2592 | /// ArgPos will have the parameter index of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2593 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2594 | const FunctionProtoType *NewType, |
| 2595 | unsigned *ArgPos) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2596 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2597 | N = NewType->arg_type_begin(), |
| 2598 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2599 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2600 | N->getUnqualifiedType())) { |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2601 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2602 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | return true; |
| 2606 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2607 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2608 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2609 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2610 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2611 | /// conversions for which IsPointerConversion has already returned |
| 2612 | /// true. It returns true and produces a diagnostic if there was an |
| 2613 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2614 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2615 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2616 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2617 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2618 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2619 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2620 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2621 | Kind = CK_BitCast; |
| 2622 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2623 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2624 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2625 | Expr::NPCK_ZeroExpression) { |
| 2626 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2627 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2628 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2629 | << ToType << From->getSourceRange()); |
| 2630 | else if (!isUnevaluatedContext()) |
| 2631 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2632 | << ToType << From->getSourceRange(); |
| 2633 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2634 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2635 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2636 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2637 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2638 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2639 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2640 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2641 | // We must have a derived-to-base conversion. Check an |
| 2642 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2643 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2644 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2645 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2646 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2647 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2648 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2649 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2650 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2651 | } |
| 2652 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2653 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2654 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2655 | if (const ObjCObjectPointerType *FromPtrType = |
| 2656 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2657 | // Objective-C++ conversions are always okay. |
| 2658 | // FIXME: We should have a different class of conversions for the |
| 2659 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2660 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2661 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2662 | } else if (FromType->isBlockPointerType()) { |
| 2663 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2664 | } else { |
| 2665 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2666 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2667 | } else if (ToType->isBlockPointerType()) { |
| 2668 | if (!FromType->isBlockPointerType()) |
| 2669 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2670 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2671 | |
| 2672 | // We shouldn't fall into this case unless it's valid for other |
| 2673 | // reasons. |
| 2674 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2675 | Kind = CK_NullToPointer; |
| 2676 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2677 | return false; |
| 2678 | } |
| 2679 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2680 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2681 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2682 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2683 | /// If so, returns true and places the converted type (that might differ from |
| 2684 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2685 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2686 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2687 | bool InOverloadResolution, |
| 2688 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2689 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2690 | if (!ToTypePtr) |
| 2691 | return false; |
| 2692 | |
| 2693 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2694 | if (From->isNullPointerConstant(Context, |
| 2695 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2696 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2697 | ConvertedType = ToType; |
| 2698 | return true; |
| 2699 | } |
| 2700 | |
| 2701 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2702 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2703 | if (!FromTypePtr) |
| 2704 | return false; |
| 2705 | |
| 2706 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2707 | // where D is derived from B (C++ 4.11p2). |
| 2708 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2709 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2710 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2711 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2712 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2713 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2714 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2715 | ToClass.getTypePtr()); |
| 2716 | return true; |
| 2717 | } |
| 2718 | |
| 2719 | return false; |
| 2720 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2721 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2722 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2723 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2724 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2725 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2726 | /// true and produces a diagnostic if there was an error, or returns false |
| 2727 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2729 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2730 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2731 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2732 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2733 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2734 | if (!FromPtrType) { |
| 2735 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2736 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2737 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2738 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2739 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2740 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2741 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2742 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2743 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2744 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2745 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2746 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2747 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2748 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2749 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2750 | // FIXME: What about dependent types? |
| 2751 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2752 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2753 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2754 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2755 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2756 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2757 | assert(DerivationOkay && |
| 2758 | "Should not have been called if derivation isn't OK."); |
| 2759 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2760 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2761 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2762 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2763 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2764 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2765 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2766 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2767 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2769 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2770 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2771 | << FromClass << ToClass << QualType(VBase, 0) |
| 2772 | << From->getSourceRange(); |
| 2773 | return true; |
| 2774 | } |
| 2775 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2776 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2777 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2778 | Paths.front(), |
| 2779 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2780 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2781 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2782 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2783 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2784 | return false; |
| 2785 | } |
| 2786 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2787 | /// IsQualificationConversion - Determines whether the conversion from |
| 2788 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2789 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2790 | /// |
| 2791 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2792 | /// when the qualification conversion involves a change in the Objective-C |
| 2793 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2795 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2796 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2797 | FromType = Context.getCanonicalType(FromType); |
| 2798 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2799 | ObjCLifetimeConversion = false; |
| 2800 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2801 | // If FromType and ToType are the same type, this is not a |
| 2802 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2803 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2804 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2806 | // (C++ 4.4p4): |
| 2807 | // A conversion can add cv-qualifiers at levels other than the first |
| 2808 | // in multi-level pointers, subject to the following rules: [...] |
| 2809 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2810 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2811 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2812 | // Within each iteration of the loop, we check the qualifiers to |
| 2813 | // determine if this still looks like a qualification |
| 2814 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2815 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2816 | // until there are no more pointers or pointers-to-members left to |
| 2817 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2818 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2820 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2821 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2822 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2823 | // Objective-C ARC: |
| 2824 | // Check Objective-C lifetime conversions. |
| 2825 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2826 | UnwrappedAnyPointer) { |
| 2827 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2828 | ObjCLifetimeConversion = true; |
| 2829 | FromQuals.removeObjCLifetime(); |
| 2830 | ToQuals.removeObjCLifetime(); |
| 2831 | } else { |
| 2832 | // Qualification conversions cannot cast between different |
| 2833 | // Objective-C lifetime qualifiers. |
| 2834 | return false; |
| 2835 | } |
| 2836 | } |
| 2837 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2838 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2839 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2840 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2841 | FromQuals.removeObjCGCAttr(); |
| 2842 | ToQuals.removeObjCGCAttr(); |
| 2843 | } |
| 2844 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2845 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2846 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2847 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2848 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2850 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2851 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2852 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2853 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2854 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2856 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2857 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2859 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2860 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2861 | |
| 2862 | // We are left with FromType and ToType being the pointee types |
| 2863 | // after unwrapping the original FromType and ToType the same number |
| 2864 | // of types. If we unwrapped any pointers, and if FromType and |
| 2865 | // ToType have the same unqualified type (since we checked |
| 2866 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2867 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2870 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2871 | /// atomic type. |
| 2872 | /// |
| 2873 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2874 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2875 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2876 | bool InOverloadResolution, |
| 2877 | StandardConversionSequence &SCS, |
| 2878 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2879 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2880 | if (!ToAtomic) |
| 2881 | return false; |
| 2882 | |
| 2883 | StandardConversionSequence InnerSCS; |
| 2884 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2885 | InOverloadResolution, InnerSCS, |
| 2886 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2887 | return false; |
| 2888 | |
| 2889 | SCS.Second = InnerSCS.Second; |
| 2890 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2891 | SCS.Third = InnerSCS.Third; |
| 2892 | SCS.QualificationIncludesObjCLifetime |
| 2893 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2894 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2895 | return true; |
| 2896 | } |
| 2897 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2898 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2899 | CXXConstructorDecl *Constructor, |
| 2900 | QualType Type) { |
| 2901 | const FunctionProtoType *CtorType = |
| 2902 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2903 | if (CtorType->getNumArgs() > 0) { |
| 2904 | QualType FirstArg = CtorType->getArgType(0); |
| 2905 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2906 | return true; |
| 2907 | } |
| 2908 | return false; |
| 2909 | } |
| 2910 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2911 | static OverloadingResult |
| 2912 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2913 | CXXRecordDecl *To, |
| 2914 | UserDefinedConversionSequence &User, |
| 2915 | OverloadCandidateSet &CandidateSet, |
| 2916 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2917 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2918 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2919 | Con != ConEnd; ++Con) { |
| 2920 | NamedDecl *D = *Con; |
| 2921 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2922 | |
| 2923 | // Find the constructor (which may be a template). |
| 2924 | CXXConstructorDecl *Constructor = 0; |
| 2925 | FunctionTemplateDecl *ConstructorTmpl |
| 2926 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2927 | if (ConstructorTmpl) |
| 2928 | Constructor |
| 2929 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2930 | else |
| 2931 | Constructor = cast<CXXConstructorDecl>(D); |
| 2932 | |
| 2933 | bool Usable = !Constructor->isInvalidDecl() && |
| 2934 | S.isInitListConstructor(Constructor) && |
| 2935 | (AllowExplicit || !Constructor->isExplicit()); |
| 2936 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2937 | // If the first argument is (a reference to) the target type, |
| 2938 | // suppress conversions. |
| 2939 | bool SuppressUserConversions = |
| 2940 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2941 | if (ConstructorTmpl) |
| 2942 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2943 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2944 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2945 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2946 | else |
| 2947 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2948 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2949 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2954 | |
| 2955 | OverloadCandidateSet::iterator Best; |
| 2956 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2957 | case OR_Success: { |
| 2958 | // Record the standard conversion we used and the conversion function. |
| 2959 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2960 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2961 | // Initializer lists don't have conversions as such. |
| 2962 | User.Before.setAsIdentityConversion(); |
| 2963 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2964 | User.ConversionFunction = Constructor; |
| 2965 | User.FoundConversionFunction = Best->FoundDecl; |
| 2966 | User.After.setAsIdentityConversion(); |
| 2967 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2968 | User.After.setAllToTypes(ToType); |
| 2969 | return OR_Success; |
| 2970 | } |
| 2971 | |
| 2972 | case OR_No_Viable_Function: |
| 2973 | return OR_No_Viable_Function; |
| 2974 | case OR_Deleted: |
| 2975 | return OR_Deleted; |
| 2976 | case OR_Ambiguous: |
| 2977 | return OR_Ambiguous; |
| 2978 | } |
| 2979 | |
| 2980 | llvm_unreachable("Invalid OverloadResult!"); |
| 2981 | } |
| 2982 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2983 | /// Determines whether there is a user-defined conversion sequence |
| 2984 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2985 | /// ToType. If such a conversion exists, User will contain the |
| 2986 | /// user-defined conversion sequence that performs such a conversion |
| 2987 | /// and this routine will return true. Otherwise, this routine returns |
| 2988 | /// false and User is unspecified. |
| 2989 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2990 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2991 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2992 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2993 | static OverloadingResult |
| 2994 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2995 | UserDefinedConversionSequence &User, |
| 2996 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2997 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2998 | // Whether we will only visit constructors. |
| 2999 | bool ConstructorsOnly = false; |
| 3000 | |
| 3001 | // If the type we are conversion to is a class type, enumerate its |
| 3002 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3003 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3004 | // C++ [over.match.ctor]p1: |
| 3005 | // When objects of class type are direct-initialized (8.5), or |
| 3006 | // copy-initialized from an expression of the same or a |
| 3007 | // derived class type (8.5), overload resolution selects the |
| 3008 | // constructor. [...] For copy-initialization, the candidate |
| 3009 | // functions are all the converting constructors (12.3.1) of |
| 3010 | // that class. The argument list is the expression-list within |
| 3011 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3012 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3013 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3014 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3015 | ConstructorsOnly = true; |
| 3016 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3017 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3018 | // RequireCompleteType may have returned true due to some invalid decl |
| 3019 | // during template instantiation, but ToType may be complete enough now |
| 3020 | // to try to recover. |
| 3021 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3022 | // We're not going to find any constructors. |
| 3023 | } else if (CXXRecordDecl *ToRecordDecl |
| 3024 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3025 | |
| 3026 | Expr **Args = &From; |
| 3027 | unsigned NumArgs = 1; |
| 3028 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3029 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3030 | // But first, see if there is an init-list-constructor that will work. |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3031 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3032 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3033 | if (Result != OR_No_Viable_Function) |
| 3034 | return Result; |
| 3035 | // Never mind. |
| 3036 | CandidateSet.clear(); |
| 3037 | |
| 3038 | // If we're list-initializing, we pass the individual elements as |
| 3039 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3040 | Args = InitList->getInits(); |
| 3041 | NumArgs = InitList->getNumInits(); |
| 3042 | ListInitializing = true; |
| 3043 | } |
| 3044 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3045 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3046 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3047 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3048 | NamedDecl *D = *Con; |
| 3049 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3050 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3051 | // Find the constructor (which may be a template). |
| 3052 | CXXConstructorDecl *Constructor = 0; |
| 3053 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3054 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3055 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3057 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3058 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3059 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3060 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3061 | bool Usable = !Constructor->isInvalidDecl(); |
| 3062 | if (ListInitializing) |
| 3063 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3064 | else |
| 3065 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3066 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3067 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3068 | if (SuppressUserConversions && ListInitializing) { |
| 3069 | SuppressUserConversions = false; |
| 3070 | if (NumArgs == 1) { |
| 3071 | // If the first argument is (a reference to) the target type, |
| 3072 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3073 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3074 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3077 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3078 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3079 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3080 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3081 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3082 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3083 | // Allow one user-defined conversion when user specifies a |
| 3084 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3085 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3086 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3087 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3088 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3089 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3093 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3094 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3095 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3096 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3098 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3100 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3101 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3102 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3103 | CXXRecordDecl::conversion_iterator> |
| 3104 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3105 | for (CXXRecordDecl::conversion_iterator |
| 3106 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3107 | DeclAccessPair FoundDecl = I.getPair(); |
| 3108 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3109 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3110 | if (isa<UsingShadowDecl>(D)) |
| 3111 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3112 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3113 | CXXConversionDecl *Conv; |
| 3114 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3115 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3116 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3117 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3118 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3119 | |
| 3120 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3121 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3122 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3123 | ActingContext, From, ToType, |
| 3124 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3125 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3126 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3127 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3128 | } |
| 3129 | } |
| 3130 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3131 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3132 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3133 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3134 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3135 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3136 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3137 | case OR_Success: |
| 3138 | // Record the standard conversion we used and the conversion function. |
| 3139 | if (CXXConstructorDecl *Constructor |
| 3140 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3141 | // C++ [over.ics.user]p1: |
| 3142 | // If the user-defined conversion is specified by a |
| 3143 | // constructor (12.3.1), the initial standard conversion |
| 3144 | // sequence converts the source type to the type required by |
| 3145 | // the argument of the constructor. |
| 3146 | // |
| 3147 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3148 | if (isa<InitListExpr>(From)) { |
| 3149 | // Initializer lists don't have conversions as such. |
| 3150 | User.Before.setAsIdentityConversion(); |
| 3151 | } else { |
| 3152 | if (Best->Conversions[0].isEllipsis()) |
| 3153 | User.EllipsisConversion = true; |
| 3154 | else { |
| 3155 | User.Before = Best->Conversions[0].Standard; |
| 3156 | User.EllipsisConversion = false; |
| 3157 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3158 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3159 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3160 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3161 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3162 | User.After.setAsIdentityConversion(); |
| 3163 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3164 | User.After.setAllToTypes(ToType); |
| 3165 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3166 | } |
| 3167 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3168 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3169 | // C++ [over.ics.user]p1: |
| 3170 | // |
| 3171 | // [...] If the user-defined conversion is specified by a |
| 3172 | // conversion function (12.3.2), the initial standard |
| 3173 | // conversion sequence converts the source type to the |
| 3174 | // implicit object parameter of the conversion function. |
| 3175 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3176 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3177 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3178 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3179 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3181 | // C++ [over.ics.user]p2: |
| 3182 | // The second standard conversion sequence converts the |
| 3183 | // result of the user-defined conversion to the target type |
| 3184 | // for the sequence. Since an implicit conversion sequence |
| 3185 | // is an initialization, the special rules for |
| 3186 | // initialization by user-defined conversion apply when |
| 3187 | // selecting the best user-defined conversion for a |
| 3188 | // user-defined conversion sequence (see 13.3.3 and |
| 3189 | // 13.3.3.1). |
| 3190 | User.After = Best->FinalConversion; |
| 3191 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3192 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3193 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3194 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3195 | case OR_No_Viable_Function: |
| 3196 | return OR_No_Viable_Function; |
| 3197 | case OR_Deleted: |
| 3198 | // No conversion here! We're done. |
| 3199 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3200 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3201 | case OR_Ambiguous: |
| 3202 | return OR_Ambiguous; |
| 3203 | } |
| 3204 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3205 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3206 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3208 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3209 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3210 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3211 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3212 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3213 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3214 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3215 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3216 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3217 | diag::err_typecheck_ambiguous_condition) |
| 3218 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3219 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3220 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3221 | diag::err_typecheck_nonviable_condition_incomplete, |
| 3222 | From->getType(), From->getSourceRange())) |
| 3223 | Diag(From->getLocStart(), |
| 3224 | diag::err_typecheck_nonviable_condition) |
| 3225 | << From->getType() << From->getSourceRange() << ToType; |
| 3226 | } |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3227 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3228 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3229 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3230 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3231 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3232 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3233 | /// \brief Compare the user-defined conversion functions or constructors |
| 3234 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3235 | /// is possible. |
| 3236 | static ImplicitConversionSequence::CompareKind |
| 3237 | compareConversionFunctions(Sema &S, |
| 3238 | FunctionDecl *Function1, |
| 3239 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3240 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3241 | return ImplicitConversionSequence::Indistinguishable; |
| 3242 | |
| 3243 | // Objective-C++: |
| 3244 | // If both conversion functions are implicitly-declared conversions from |
| 3245 | // a lambda closure type to a function pointer and a block pointer, |
| 3246 | // respectively, always prefer the conversion to a function pointer, |
| 3247 | // because the function pointer is more lightweight and is more likely |
| 3248 | // to keep code working. |
| 3249 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3250 | if (!Conv1) |
| 3251 | return ImplicitConversionSequence::Indistinguishable; |
| 3252 | |
| 3253 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3254 | if (!Conv2) |
| 3255 | return ImplicitConversionSequence::Indistinguishable; |
| 3256 | |
| 3257 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3258 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3259 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3260 | if (Block1 != Block2) |
| 3261 | return Block1? ImplicitConversionSequence::Worse |
| 3262 | : ImplicitConversionSequence::Better; |
| 3263 | } |
| 3264 | |
| 3265 | return ImplicitConversionSequence::Indistinguishable; |
| 3266 | } |
| 3267 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3268 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3269 | /// conversion sequences to determine whether one is better than the |
| 3270 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3271 | static ImplicitConversionSequence::CompareKind |
| 3272 | CompareImplicitConversionSequences(Sema &S, |
| 3273 | const ImplicitConversionSequence& ICS1, |
| 3274 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3275 | { |
| 3276 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3277 | // conversion sequences (as defined in 13.3.3.1) |
| 3278 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3279 | // conversion sequence than a user-defined conversion sequence or |
| 3280 | // an ellipsis conversion sequence, and |
| 3281 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3282 | // conversion sequence than an ellipsis conversion sequence |
| 3283 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3285 | // C++0x [over.best.ics]p10: |
| 3286 | // For the purpose of ranking implicit conversion sequences as |
| 3287 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3288 | // treated as a user-defined sequence that is indistinguishable |
| 3289 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3290 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3291 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3292 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3293 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3294 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3295 | // The following checks require both conversion sequences to be of |
| 3296 | // the same kind. |
| 3297 | if (ICS1.getKind() != ICS2.getKind()) |
| 3298 | return ImplicitConversionSequence::Indistinguishable; |
| 3299 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3300 | ImplicitConversionSequence::CompareKind Result = |
| 3301 | ImplicitConversionSequence::Indistinguishable; |
| 3302 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3303 | // Two implicit conversion sequences of the same form are |
| 3304 | // indistinguishable conversion sequences unless one of the |
| 3305 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3306 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3307 | Result = CompareStandardConversionSequences(S, |
| 3308 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3309 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3310 | // User-defined conversion sequence U1 is a better conversion |
| 3311 | // sequence than another user-defined conversion sequence U2 if |
| 3312 | // they contain the same user-defined conversion function or |
| 3313 | // constructor and if the second standard conversion sequence of |
| 3314 | // U1 is better than the second standard conversion sequence of |
| 3315 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3317 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3318 | Result = CompareStandardConversionSequences(S, |
| 3319 | ICS1.UserDefined.After, |
| 3320 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3321 | else |
| 3322 | Result = compareConversionFunctions(S, |
| 3323 | ICS1.UserDefined.ConversionFunction, |
| 3324 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3327 | // List-initialization sequence L1 is a better conversion sequence than |
| 3328 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3329 | // for some X and L2 does not. |
| 3330 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 3331 | !ICS1.isBad()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3332 | if (ICS1.isStdInitializerListElement() && |
| 3333 | !ICS2.isStdInitializerListElement()) |
| 3334 | return ImplicitConversionSequence::Better; |
| 3335 | if (!ICS1.isStdInitializerListElement() && |
| 3336 | ICS2.isStdInitializerListElement()) |
| 3337 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3343 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3344 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3345 | Qualifiers Quals; |
| 3346 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3347 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3348 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3350 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3351 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3353 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3354 | // determine if one is a proper subset of the other. |
| 3355 | static ImplicitConversionSequence::CompareKind |
| 3356 | compareStandardConversionSubsets(ASTContext &Context, |
| 3357 | const StandardConversionSequence& SCS1, |
| 3358 | const StandardConversionSequence& SCS2) { |
| 3359 | ImplicitConversionSequence::CompareKind Result |
| 3360 | = ImplicitConversionSequence::Indistinguishable; |
| 3361 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3363 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3364 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3365 | return ImplicitConversionSequence::Better; |
| 3366 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3367 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3368 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3369 | if (SCS1.Second != SCS2.Second) { |
| 3370 | if (SCS1.Second == ICK_Identity) |
| 3371 | Result = ImplicitConversionSequence::Better; |
| 3372 | else if (SCS2.Second == ICK_Identity) |
| 3373 | Result = ImplicitConversionSequence::Worse; |
| 3374 | else |
| 3375 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3376 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3377 | return ImplicitConversionSequence::Indistinguishable; |
| 3378 | |
| 3379 | if (SCS1.Third == SCS2.Third) { |
| 3380 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3381 | : ImplicitConversionSequence::Indistinguishable; |
| 3382 | } |
| 3383 | |
| 3384 | if (SCS1.Third == ICK_Identity) |
| 3385 | return Result == ImplicitConversionSequence::Worse |
| 3386 | ? ImplicitConversionSequence::Indistinguishable |
| 3387 | : ImplicitConversionSequence::Better; |
| 3388 | |
| 3389 | if (SCS2.Third == ICK_Identity) |
| 3390 | return Result == ImplicitConversionSequence::Better |
| 3391 | ? ImplicitConversionSequence::Indistinguishable |
| 3392 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3394 | return ImplicitConversionSequence::Indistinguishable; |
| 3395 | } |
| 3396 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3397 | /// \brief Determine whether one of the given reference bindings is better |
| 3398 | /// than the other based on what kind of bindings they are. |
| 3399 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3400 | const StandardConversionSequence &SCS2) { |
| 3401 | // C++0x [over.ics.rank]p3b4: |
| 3402 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3403 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3404 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3405 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3407 | // reference*. |
| 3408 | // |
| 3409 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3410 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3411 | // time of this writing) break the standard definition of std::forward |
| 3412 | // and std::reference_wrapper when dealing with references to functions. |
| 3413 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3414 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3415 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3416 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3418 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3419 | SCS2.IsLvalueReference) || |
| 3420 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3421 | !SCS2.IsLvalueReference); |
| 3422 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3424 | /// CompareStandardConversionSequences - Compare two standard |
| 3425 | /// conversion sequences to determine whether one is better than the |
| 3426 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3427 | static ImplicitConversionSequence::CompareKind |
| 3428 | CompareStandardConversionSequences(Sema &S, |
| 3429 | const StandardConversionSequence& SCS1, |
| 3430 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3431 | { |
| 3432 | // Standard conversion sequence S1 is a better conversion sequence |
| 3433 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3434 | |
| 3435 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3436 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3437 | // excluding any Lvalue Transformation; the identity conversion |
| 3438 | // sequence is considered to be a subsequence of any |
| 3439 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3440 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3441 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3442 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3443 | |
| 3444 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3445 | // defined below), or, if not that, |
| 3446 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3447 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3448 | if (Rank1 < Rank2) |
| 3449 | return ImplicitConversionSequence::Better; |
| 3450 | else if (Rank2 < Rank1) |
| 3451 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3452 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3453 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3454 | // are indistinguishable unless one of the following rules |
| 3455 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3457 | // A conversion that is not a conversion of a pointer, or |
| 3458 | // pointer to member, to bool is better than another conversion |
| 3459 | // that is such a conversion. |
| 3460 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3461 | return SCS2.isPointerConversionToBool() |
| 3462 | ? ImplicitConversionSequence::Better |
| 3463 | : ImplicitConversionSequence::Worse; |
| 3464 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3465 | // C++ [over.ics.rank]p4b2: |
| 3466 | // |
| 3467 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3468 | // conversion of B* to A* is better than conversion of B* to |
| 3469 | // void*, and conversion of A* to void* is better than conversion |
| 3470 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3472 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3474 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3475 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3476 | // Exactly one of the conversion sequences is a conversion to |
| 3477 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3478 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3479 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3480 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3481 | // Neither conversion sequence converts to a void pointer; compare |
| 3482 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3483 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3484 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3485 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3486 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3487 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3488 | // Both conversion sequences are conversions to void |
| 3489 | // pointers. Compare the source types to determine if there's an |
| 3490 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3491 | QualType FromType1 = SCS1.getFromType(); |
| 3492 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3493 | |
| 3494 | // Adjust the types we're converting from via the array-to-pointer |
| 3495 | // conversion, if we need to. |
| 3496 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3497 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3498 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3499 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3500 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3501 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3502 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3503 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3504 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3505 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3506 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3507 | return ImplicitConversionSequence::Worse; |
| 3508 | |
| 3509 | // Objective-C++: If one interface is more specific than the |
| 3510 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3511 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3512 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3513 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3514 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3515 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3516 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3517 | FromObjCPtr2); |
| 3518 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3519 | FromObjCPtr1); |
| 3520 | if (AssignLeft != AssignRight) { |
| 3521 | return AssignLeft? ImplicitConversionSequence::Better |
| 3522 | : ImplicitConversionSequence::Worse; |
| 3523 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3524 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3525 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3526 | |
| 3527 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3528 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3530 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3531 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3532 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3533 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3534 | // Check for a better reference binding based on the kind of bindings. |
| 3535 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3536 | return ImplicitConversionSequence::Better; |
| 3537 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3538 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3539 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3540 | // C++ [over.ics.rank]p3b4: |
| 3541 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3542 | // which the references refer are the same type except for |
| 3543 | // top-level cv-qualifiers, and the type to which the reference |
| 3544 | // initialized by S2 refers is more cv-qualified than the type |
| 3545 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3546 | QualType T1 = SCS1.getToType(2); |
| 3547 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3548 | T1 = S.Context.getCanonicalType(T1); |
| 3549 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3550 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3551 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3552 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3553 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3554 | // Objective-C++ ARC: If the references refer to objects with different |
| 3555 | // lifetimes, prefer bindings that don't change lifetime. |
| 3556 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3557 | SCS2.ObjCLifetimeConversionBinding) { |
| 3558 | return SCS1.ObjCLifetimeConversionBinding |
| 3559 | ? ImplicitConversionSequence::Worse |
| 3560 | : ImplicitConversionSequence::Better; |
| 3561 | } |
| 3562 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3563 | // If the type is an array type, promote the element qualifiers to the |
| 3564 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3565 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3566 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3567 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3568 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3569 | if (T2.isMoreQualifiedThan(T1)) |
| 3570 | return ImplicitConversionSequence::Better; |
| 3571 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3572 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3573 | } |
| 3574 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3575 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3576 | // In Microsoft mode, prefer an integral conversion to a |
| 3577 | // floating-to-integral conversion if the integral conversion |
| 3578 | // is between types of the same size. |
| 3579 | // For example: |
| 3580 | // void f(float); |
| 3581 | // void f(int); |
| 3582 | // int main { |
| 3583 | // long a; |
| 3584 | // f(a); |
| 3585 | // } |
| 3586 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3587 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3588 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3589 | SCS1.Second == ICK_Integral_Conversion && |
| 3590 | SCS2.Second == ICK_Floating_Integral && |
| 3591 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3592 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3593 | return ImplicitConversionSequence::Better; |
| 3594 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3595 | return ImplicitConversionSequence::Indistinguishable; |
| 3596 | } |
| 3597 | |
| 3598 | /// CompareQualificationConversions - Compares two standard conversion |
| 3599 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3601 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3602 | CompareQualificationConversions(Sema &S, |
| 3603 | const StandardConversionSequence& SCS1, |
| 3604 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3605 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3606 | // -- S1 and S2 differ only in their qualification conversion and |
| 3607 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3608 | // cv-qualification signature of type T1 is a proper subset of |
| 3609 | // the cv-qualification signature of type T2, and S1 is not the |
| 3610 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3611 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3612 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3613 | return ImplicitConversionSequence::Indistinguishable; |
| 3614 | |
| 3615 | // FIXME: the example in the standard doesn't use a qualification |
| 3616 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3617 | QualType T1 = SCS1.getToType(2); |
| 3618 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3619 | T1 = S.Context.getCanonicalType(T1); |
| 3620 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3621 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3622 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3623 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3624 | |
| 3625 | // If the types are the same, we won't learn anything by unwrapped |
| 3626 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3627 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3628 | return ImplicitConversionSequence::Indistinguishable; |
| 3629 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3630 | // If the type is an array type, promote the element qualifiers to the type |
| 3631 | // for comparison. |
| 3632 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3633 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3634 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3635 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3636 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3637 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3638 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3639 | |
| 3640 | // Objective-C++ ARC: |
| 3641 | // Prefer qualification conversions not involving a change in lifetime |
| 3642 | // to qualification conversions that do not change lifetime. |
| 3643 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3644 | SCS2.QualificationIncludesObjCLifetime) { |
| 3645 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3646 | ? ImplicitConversionSequence::Worse |
| 3647 | : ImplicitConversionSequence::Better; |
| 3648 | } |
| 3649 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3650 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3651 | // Within each iteration of the loop, we check the qualifiers to |
| 3652 | // determine if this still looks like a qualification |
| 3653 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3654 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3655 | // until there are no more pointers or pointers-to-members left |
| 3656 | // to unwrap. This essentially mimics what |
| 3657 | // IsQualificationConversion does, but here we're checking for a |
| 3658 | // strict subset of qualifiers. |
| 3659 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3660 | // The qualifiers are the same, so this doesn't tell us anything |
| 3661 | // about how the sequences rank. |
| 3662 | ; |
| 3663 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3664 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3665 | if (Result == ImplicitConversionSequence::Worse) |
| 3666 | // Neither has qualifiers that are a subset of the other's |
| 3667 | // qualifiers. |
| 3668 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3669 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3670 | Result = ImplicitConversionSequence::Better; |
| 3671 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3672 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3673 | if (Result == ImplicitConversionSequence::Better) |
| 3674 | // Neither has qualifiers that are a subset of the other's |
| 3675 | // qualifiers. |
| 3676 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3678 | Result = ImplicitConversionSequence::Worse; |
| 3679 | } else { |
| 3680 | // Qualifiers are disjoint. |
| 3681 | return ImplicitConversionSequence::Indistinguishable; |
| 3682 | } |
| 3683 | |
| 3684 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3685 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3686 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3689 | // Check that the winning standard conversion sequence isn't using |
| 3690 | // the deprecated string literal array to pointer conversion. |
| 3691 | switch (Result) { |
| 3692 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3693 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3694 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3695 | break; |
| 3696 | |
| 3697 | case ImplicitConversionSequence::Indistinguishable: |
| 3698 | break; |
| 3699 | |
| 3700 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3701 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3702 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3703 | break; |
| 3704 | } |
| 3705 | |
| 3706 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3709 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3710 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3711 | /// various kinds of derived-to-base conversions (C++ |
| 3712 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3713 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3714 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3715 | CompareDerivedToBaseConversions(Sema &S, |
| 3716 | const StandardConversionSequence& SCS1, |
| 3717 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3718 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3719 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3720 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3721 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3722 | |
| 3723 | // Adjust the types we're converting from via the array-to-pointer |
| 3724 | // conversion, if we need to. |
| 3725 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3726 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3727 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3728 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3729 | |
| 3730 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3731 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3732 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3733 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3734 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3735 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3736 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3737 | // |
| 3738 | // If class B is derived directly or indirectly from class A and |
| 3739 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3740 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3741 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3743 | SCS2.Second == ICK_Pointer_Conversion && |
| 3744 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3745 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3746 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3748 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3749 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3750 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3751 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3752 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3753 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3754 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3755 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3756 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3757 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3758 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3759 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3760 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3761 | return ImplicitConversionSequence::Worse; |
| 3762 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3763 | |
| 3764 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3765 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3766 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3767 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3768 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3769 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3770 | } |
| 3771 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3772 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3773 | const ObjCObjectPointerType *FromPtr1 |
| 3774 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3775 | const ObjCObjectPointerType *FromPtr2 |
| 3776 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3777 | const ObjCObjectPointerType *ToPtr1 |
| 3778 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3779 | const ObjCObjectPointerType *ToPtr2 |
| 3780 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3781 | |
| 3782 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3783 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3784 | // that we do for C++ pointers to class types. However, we employ the |
| 3785 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3786 | // Objective-C pointer types. |
| 3787 | bool FromAssignLeft |
| 3788 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3789 | bool FromAssignRight |
| 3790 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3791 | bool ToAssignLeft |
| 3792 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3793 | bool ToAssignRight |
| 3794 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3795 | |
| 3796 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3797 | // type is better than a conversion to 'id'. |
| 3798 | if (ToPtr1->isObjCIdType() && |
| 3799 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3800 | return ImplicitConversionSequence::Worse; |
| 3801 | if (ToPtr2->isObjCIdType() && |
| 3802 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3803 | return ImplicitConversionSequence::Better; |
| 3804 | |
| 3805 | // A conversion to a non-id object pointer type is better than a |
| 3806 | // conversion to a qualified 'id' type |
| 3807 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3808 | return ImplicitConversionSequence::Worse; |
| 3809 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3810 | return ImplicitConversionSequence::Better; |
| 3811 | |
| 3812 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3813 | // type is better than a conversion to 'Class'. |
| 3814 | if (ToPtr1->isObjCClassType() && |
| 3815 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3816 | return ImplicitConversionSequence::Worse; |
| 3817 | if (ToPtr2->isObjCClassType() && |
| 3818 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3819 | return ImplicitConversionSequence::Better; |
| 3820 | |
| 3821 | // A conversion to a non-Class object pointer type is better than a |
| 3822 | // conversion to a qualified 'Class' type. |
| 3823 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3824 | return ImplicitConversionSequence::Worse; |
| 3825 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3826 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3828 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3829 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3830 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3831 | (ToAssignLeft != ToAssignRight)) |
| 3832 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3833 | : ImplicitConversionSequence::Better; |
| 3834 | |
| 3835 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3836 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3837 | (FromAssignLeft != FromAssignRight)) |
| 3838 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3839 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3840 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3841 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3842 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3843 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3844 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3845 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3846 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3847 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3848 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3849 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3850 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3851 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3852 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3853 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3854 | ToType2->getAs<MemberPointerType>(); |
| 3855 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3856 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3857 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3858 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3859 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3860 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3861 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3862 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3863 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3864 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3865 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3866 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3867 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3868 | return ImplicitConversionSequence::Better; |
| 3869 | } |
| 3870 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3871 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3872 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3873 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3874 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3875 | return ImplicitConversionSequence::Worse; |
| 3876 | } |
| 3877 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3879 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3880 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3881 | // -- binding of an expression of type C to a reference of type |
| 3882 | // B& is better than binding an expression of type C to a |
| 3883 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3884 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3885 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3886 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3887 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3888 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3889 | return ImplicitConversionSequence::Worse; |
| 3890 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3891 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3892 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3893 | // -- binding of an expression of type B to a reference of type |
| 3894 | // A& is better than binding an expression of type C to a |
| 3895 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3896 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3897 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3898 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3899 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3900 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3901 | return ImplicitConversionSequence::Worse; |
| 3902 | } |
| 3903 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3905 | return ImplicitConversionSequence::Indistinguishable; |
| 3906 | } |
| 3907 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3908 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3909 | /// C++ class. |
| 3910 | static bool isTypeValid(QualType T) { |
| 3911 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3912 | return !Record->isInvalidDecl(); |
| 3913 | |
| 3914 | return true; |
| 3915 | } |
| 3916 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3917 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3918 | /// determine whether they are reference-related, |
| 3919 | /// reference-compatible, reference-compatible with added |
| 3920 | /// qualification, or incompatible, for use in C++ initialization by |
| 3921 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3922 | /// type, and the first type (T1) is the pointee type of the reference |
| 3923 | /// type being initialized. |
| 3924 | Sema::ReferenceCompareResult |
| 3925 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3926 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3927 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3928 | bool &ObjCConversion, |
| 3929 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3930 | assert(!OrigT1->isReferenceType() && |
| 3931 | "T1 must be the pointee type of the reference type"); |
| 3932 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3933 | |
| 3934 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3935 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3936 | Qualifiers T1Quals, T2Quals; |
| 3937 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3938 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3939 | |
| 3940 | // C++ [dcl.init.ref]p4: |
| 3941 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3942 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3943 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3944 | DerivedToBase = false; |
| 3945 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3946 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3947 | if (UnqualT1 == UnqualT2) { |
| 3948 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3949 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3950 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3951 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3952 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3953 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3954 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3955 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3956 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3957 | else |
| 3958 | return Ref_Incompatible; |
| 3959 | |
| 3960 | // At this point, we know that T1 and T2 are reference-related (at |
| 3961 | // least). |
| 3962 | |
| 3963 | // If the type is an array type, promote the element qualifiers to the type |
| 3964 | // for comparison. |
| 3965 | if (isa<ArrayType>(T1) && T1Quals) |
| 3966 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3967 | if (isa<ArrayType>(T2) && T2Quals) |
| 3968 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3969 | |
| 3970 | // C++ [dcl.init.ref]p4: |
| 3971 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3972 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3973 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3974 | // overload resolution, cases for which cv1 is greater |
| 3975 | // cv-qualification than cv2 are identified as |
| 3976 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3977 | // |
| 3978 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3979 | // qualifiers when performing these computations, so that e.g., an int in |
| 3980 | // address space 1 is not reference-compatible with an int in address |
| 3981 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3982 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3983 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3984 | T1Quals.removeObjCLifetime(); |
| 3985 | T2Quals.removeObjCLifetime(); |
| 3986 | ObjCLifetimeConversion = true; |
| 3987 | } |
| 3988 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3989 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3990 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3991 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3992 | return Ref_Compatible_With_Added_Qualification; |
| 3993 | else |
| 3994 | return Ref_Related; |
| 3995 | } |
| 3996 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3997 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3998 | /// with DeclType. Return true if something definite is found. |
| 3999 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4000 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 4001 | QualType DeclType, SourceLocation DeclLoc, |
| 4002 | Expr *Init, QualType T2, bool AllowRvalues, |
| 4003 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4004 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4005 | CXXRecordDecl *T2RecordDecl |
| 4006 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4007 | |
| 4008 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4009 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4010 | CXXRecordDecl::conversion_iterator> |
| 4011 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4012 | for (CXXRecordDecl::conversion_iterator |
| 4013 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4014 | NamedDecl *D = *I; |
| 4015 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4016 | if (isa<UsingShadowDecl>(D)) |
| 4017 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4018 | |
| 4019 | FunctionTemplateDecl *ConvTemplate |
| 4020 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4021 | CXXConversionDecl *Conv; |
| 4022 | if (ConvTemplate) |
| 4023 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4024 | else |
| 4025 | Conv = cast<CXXConversionDecl>(D); |
| 4026 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4027 | // If this is an explicit conversion, and we're not allowed to consider |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4028 | // explicit conversions, skip it. |
| 4029 | if (!AllowExplicit && Conv->isExplicit()) |
| 4030 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4031 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4032 | if (AllowRvalues) { |
| 4033 | bool DerivedToBase = false; |
| 4034 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4035 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4036 | |
| 4037 | // If we are initializing an rvalue reference, don't permit conversion |
| 4038 | // functions that return lvalues. |
| 4039 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4040 | const ReferenceType *RefType |
| 4041 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4042 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4043 | continue; |
| 4044 | } |
| 4045 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4046 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4047 | S.CompareReferenceRelationship( |
| 4048 | DeclLoc, |
| 4049 | Conv->getConversionType().getNonReferenceType() |
| 4050 | .getUnqualifiedType(), |
| 4051 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4052 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4053 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4054 | continue; |
| 4055 | } else { |
| 4056 | // If the conversion function doesn't return a reference type, |
| 4057 | // it can't be considered for this conversion. An rvalue reference |
| 4058 | // is only acceptable if its referencee is a function type. |
| 4059 | |
| 4060 | const ReferenceType *RefType = |
| 4061 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4062 | if (!RefType || |
| 4063 | (!RefType->isLValueReferenceType() && |
| 4064 | !RefType->getPointeeType()->isFunctionType())) |
| 4065 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4066 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4068 | if (ConvTemplate) |
| 4069 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4070 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4071 | else |
| 4072 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4073 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4076 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4077 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4078 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4079 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4080 | case OR_Success: |
| 4081 | // C++ [over.ics.ref]p1: |
| 4082 | // |
| 4083 | // [...] If the parameter binds directly to the result of |
| 4084 | // applying a conversion function to the argument |
| 4085 | // expression, the implicit conversion sequence is a |
| 4086 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4087 | // second standard conversion sequence either an identity |
| 4088 | // conversion or, if the conversion function returns an |
| 4089 | // entity of a type that is a derived class of the parameter |
| 4090 | // type, a derived-to-base Conversion. |
| 4091 | if (!Best->FinalConversion.DirectBinding) |
| 4092 | return false; |
| 4093 | |
| 4094 | ICS.setUserDefined(); |
| 4095 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4096 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4097 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4098 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4099 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4100 | ICS.UserDefined.EllipsisConversion = false; |
| 4101 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4102 | ICS.UserDefined.After.DirectBinding && |
| 4103 | "Expected a direct reference binding!"); |
| 4104 | return true; |
| 4105 | |
| 4106 | case OR_Ambiguous: |
| 4107 | ICS.setAmbiguous(); |
| 4108 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4109 | Cand != CandidateSet.end(); ++Cand) |
| 4110 | if (Cand->Viable) |
| 4111 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4112 | return true; |
| 4113 | |
| 4114 | case OR_No_Viable_Function: |
| 4115 | case OR_Deleted: |
| 4116 | // There was no suitable conversion, or we found a deleted |
| 4117 | // conversion; continue with other checks. |
| 4118 | return false; |
| 4119 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4120 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4121 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4124 | /// \brief Compute an implicit conversion sequence for reference |
| 4125 | /// initialization. |
| 4126 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4127 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4128 | SourceLocation DeclLoc, |
| 4129 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4130 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4131 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4132 | |
| 4133 | // Most paths end in a failed conversion. |
| 4134 | ImplicitConversionSequence ICS; |
| 4135 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4136 | |
| 4137 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4138 | QualType T2 = Init->getType(); |
| 4139 | |
| 4140 | // If the initializer is the address of an overloaded function, try |
| 4141 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4142 | // type of the resulting function. |
| 4143 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4144 | DeclAccessPair Found; |
| 4145 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4146 | false, Found)) |
| 4147 | T2 = Fn->getType(); |
| 4148 | } |
| 4149 | |
| 4150 | // Compute some basic properties of the types and the initializer. |
| 4151 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4152 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4153 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4154 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4155 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4156 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4157 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4158 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4159 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4160 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4161 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4162 | // A reference to type "cv1 T1" is initialized by an expression |
| 4163 | // of type "cv2 T2" as follows: |
| 4164 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4165 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4166 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4167 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4168 | // reference-compatible with "cv2 T2," or |
| 4169 | // |
| 4170 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4171 | if (InitCategory.isLValue() && |
| 4172 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4173 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4174 | // When a parameter of reference type binds directly (8.5.3) |
| 4175 | // to an argument expression, the implicit conversion sequence |
| 4176 | // is the identity conversion, unless the argument expression |
| 4177 | // has a type that is a derived class of the parameter type, |
| 4178 | // in which case the implicit conversion sequence is a |
| 4179 | // derived-to-base Conversion (13.3.3.1). |
| 4180 | ICS.setStandard(); |
| 4181 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4182 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4183 | : ObjCConversion? ICK_Compatible_Conversion |
| 4184 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4185 | ICS.Standard.Third = ICK_Identity; |
| 4186 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4187 | ICS.Standard.setToType(0, T2); |
| 4188 | ICS.Standard.setToType(1, T1); |
| 4189 | ICS.Standard.setToType(2, T1); |
| 4190 | ICS.Standard.ReferenceBinding = true; |
| 4191 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4192 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4193 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4194 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4195 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4196 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4197 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4198 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4199 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4200 | // derived-to-base conversions is suppressed when we're |
| 4201 | // computing the implicit conversion sequence (C++ |
| 4202 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4203 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4204 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4205 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4206 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4207 | // not reference-related to T2, and can be implicitly |
| 4208 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4209 | // is reference-compatible with "cv3 T3" 92) (this |
| 4210 | // conversion is selected by enumerating the applicable |
| 4211 | // conversion functions (13.3.1.6) and choosing the best |
| 4212 | // one through overload resolution (13.3)), |
| 4213 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4214 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4215 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4216 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4217 | Init, T2, /*AllowRvalues=*/false, |
| 4218 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4219 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4223 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4224 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4225 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4227 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4228 | // point, which is that, due to p2 (which short-circuits reference |
| 4229 | // binding by only attempting a simple conversion for non-direct |
| 4230 | // bindings) and p3's strange wording, we allow a const volatile |
| 4231 | // reference to bind to an rvalue. Hence the check for the presence |
| 4232 | // of "const" rather than checking for "const" being the only |
| 4233 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4234 | // This is also the point where rvalue references and lvalue inits no longer |
| 4235 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4236 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4237 | return ICS; |
| 4238 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4239 | // -- If the initializer expression |
| 4240 | // |
| 4241 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4242 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4243 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4244 | (InitCategory.isXValue() || |
| 4245 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4246 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4247 | ICS.setStandard(); |
| 4248 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4250 | : ObjCConversion? ICK_Compatible_Conversion |
| 4251 | : ICK_Identity; |
| 4252 | ICS.Standard.Third = ICK_Identity; |
| 4253 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4254 | ICS.Standard.setToType(0, T2); |
| 4255 | ICS.Standard.setToType(1, T1); |
| 4256 | ICS.Standard.setToType(2, T1); |
| 4257 | ICS.Standard.ReferenceBinding = true; |
| 4258 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4259 | // binding unless we're binding to a class prvalue. |
| 4260 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4261 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4262 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4264 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4265 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4266 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4267 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4268 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4269 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4270 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4271 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4273 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4275 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4276 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | // an xvalue, class prvalue, or function lvalue of type |
| 4278 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4279 | // "cv3 T3", |
| 4280 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4282 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4284 | // class subobject). |
| 4285 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4286 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4287 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4288 | Init, T2, /*AllowRvalues=*/true, |
| 4289 | AllowExplicit)) { |
| 4290 | // In the second case, if the reference is an rvalue reference |
| 4291 | // and the second standard conversion sequence of the |
| 4292 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4293 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4294 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4295 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4296 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4297 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4298 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4299 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4301 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4302 | // initialized from the initializer expression using the |
| 4303 | // rules for a non-reference copy initialization (8.5). The |
| 4304 | // reference is then bound to the temporary. If T1 is |
| 4305 | // reference-related to T2, cv1 must be the same |
| 4306 | // cv-qualification as, or greater cv-qualification than, |
| 4307 | // cv2; otherwise, the program is ill-formed. |
| 4308 | if (RefRelationship == Sema::Ref_Related) { |
| 4309 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4310 | // we would be reference-compatible or reference-compatible with |
| 4311 | // added qualification. But that wasn't the case, so the reference |
| 4312 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4313 | // |
| 4314 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4315 | // ObjC GC and lifetime qualifiers aren't important. |
| 4316 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4317 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4318 | T1Quals.removeObjCGCAttr(); |
| 4319 | T1Quals.removeObjCLifetime(); |
| 4320 | T2Quals.removeObjCGCAttr(); |
| 4321 | T2Quals.removeObjCLifetime(); |
| 4322 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4323 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | // If at least one of the types is a class type, the types are not |
| 4327 | // related, and we aren't allowed any user conversions, the |
| 4328 | // reference binding fails. This case is important for breaking |
| 4329 | // recursion, since TryImplicitConversion below will attempt to |
| 4330 | // create a temporary through the use of a copy constructor. |
| 4331 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4332 | (T1->isRecordType() || T2->isRecordType())) |
| 4333 | return ICS; |
| 4334 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4335 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4336 | // reference, the initializer expression shall not be an lvalue. |
| 4337 | if (RefRelationship >= Sema::Ref_Related && |
| 4338 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4339 | return ICS; |
| 4340 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4341 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4342 | // When a parameter of reference type is not bound directly to |
| 4343 | // an argument expression, the conversion sequence is the one |
| 4344 | // required to convert the argument expression to the |
| 4345 | // underlying type of the reference according to |
| 4346 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4347 | // to copy-initializing a temporary of the underlying type with |
| 4348 | // the argument expression. Any difference in top-level |
| 4349 | // cv-qualification is subsumed by the initialization itself |
| 4350 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4351 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4352 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4353 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4354 | /*CStyle=*/false, |
| 4355 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4356 | |
| 4357 | // Of course, that's still a reference binding. |
| 4358 | if (ICS.isStandard()) { |
| 4359 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4360 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4361 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4362 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4363 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4364 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4365 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4366 | // Don't allow rvalue references to bind to lvalues. |
| 4367 | if (DeclType->isRValueReferenceType()) { |
| 4368 | if (const ReferenceType *RefType |
| 4369 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4370 | ->getAs<LValueReferenceType>()) { |
| 4371 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4372 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4373 | DeclType); |
| 4374 | return ICS; |
| 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4379 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4380 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4381 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4382 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4383 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4384 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4385 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4387 | return ICS; |
| 4388 | } |
| 4389 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4390 | static ImplicitConversionSequence |
| 4391 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4392 | bool SuppressUserConversions, |
| 4393 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4394 | bool AllowObjCWritebackConversion, |
| 4395 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4396 | |
| 4397 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4398 | /// initializer list From. |
| 4399 | static ImplicitConversionSequence |
| 4400 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4401 | bool SuppressUserConversions, |
| 4402 | bool InOverloadResolution, |
| 4403 | bool AllowObjCWritebackConversion) { |
| 4404 | // C++11 [over.ics.list]p1: |
| 4405 | // When an argument is an initializer list, it is not an expression and |
| 4406 | // special rules apply for converting it to a parameter type. |
| 4407 | |
| 4408 | ImplicitConversionSequence Result; |
| 4409 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4410 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4411 | // We need a complete type for what follows. Incomplete types can never be |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4412 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4413 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4414 | return Result; |
| 4415 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4416 | // C++11 [over.ics.list]p2: |
| 4417 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4418 | // all the elements can be implicitly converted to X, the implicit |
| 4419 | // conversion sequence is the worst conversion necessary to convert an |
| 4420 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4421 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4422 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4423 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4424 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4425 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4426 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4427 | if (!X.isNull()) { |
| 4428 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4429 | Expr *Init = From->getInit(i); |
| 4430 | ImplicitConversionSequence ICS = |
| 4431 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4432 | InOverloadResolution, |
| 4433 | AllowObjCWritebackConversion); |
| 4434 | // If a single element isn't convertible, fail. |
| 4435 | if (ICS.isBad()) { |
| 4436 | Result = ICS; |
| 4437 | break; |
| 4438 | } |
| 4439 | // Otherwise, look for the worst conversion. |
| 4440 | if (Result.isBad() || |
| 4441 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4442 | ImplicitConversionSequence::Worse) |
| 4443 | Result = ICS; |
| 4444 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4445 | |
| 4446 | // For an empty list, we won't have computed any conversion sequence. |
| 4447 | // Introduce the identity conversion sequence. |
| 4448 | if (From->getNumInits() == 0) { |
| 4449 | Result.setStandard(); |
| 4450 | Result.Standard.setAsIdentityConversion(); |
| 4451 | Result.Standard.setFromType(ToType); |
| 4452 | Result.Standard.setAllToTypes(ToType); |
| 4453 | } |
| 4454 | |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4455 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4456 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4457 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4458 | |
| 4459 | // C++11 [over.ics.list]p3: |
| 4460 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4461 | // resolution chooses a single best constructor [...] the implicit |
| 4462 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4463 | // constructors are viable but none is better than the others, the |
| 4464 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4465 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4466 | // This function can deal with initializer lists. |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4467 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4468 | /*AllowExplicit=*/false, |
| 4469 | InOverloadResolution, /*CStyle=*/false, |
| 4470 | AllowObjCWritebackConversion); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4471 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4472 | |
| 4473 | // C++11 [over.ics.list]p4: |
| 4474 | // Otherwise, if the parameter has an aggregate type which can be |
| 4475 | // initialized from the initializer list [...] the implicit conversion |
| 4476 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4477 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4478 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4479 | // down to checking whether the initialization works. |
| 4480 | // FIXME: Find out whether this parameter is consumed or not. |
| 4481 | InitializedEntity Entity = |
| 4482 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4483 | /*Consumed=*/false); |
| 4484 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4485 | Result.setUserDefined(); |
| 4486 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4487 | // Initializer lists don't have a type. |
| 4488 | Result.UserDefined.Before.setFromType(QualType()); |
| 4489 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4490 | |
| 4491 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4492 | Result.UserDefined.After.setFromType(ToType); |
| 4493 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4494 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4495 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4496 | return Result; |
| 4497 | } |
| 4498 | |
| 4499 | // C++11 [over.ics.list]p5: |
| 4500 | // Otherwise, if the parameter is a reference, see 13.3.3.1.4. |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4501 | if (ToType->isReferenceType()) { |
| 4502 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4503 | // mention initializer lists in any way. So we go by what list- |
| 4504 | // initialization would do and try to extrapolate from that. |
| 4505 | |
| 4506 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4507 | |
| 4508 | // If the initializer list has a single element that is reference-related |
| 4509 | // to the parameter type, we initialize the reference from that. |
| 4510 | if (From->getNumInits() == 1) { |
| 4511 | Expr *Init = From->getInit(0); |
| 4512 | |
| 4513 | QualType T2 = Init->getType(); |
| 4514 | |
| 4515 | // If the initializer is the address of an overloaded function, try |
| 4516 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4517 | // type of the resulting function. |
| 4518 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4519 | DeclAccessPair Found; |
| 4520 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4521 | Init, ToType, false, Found)) |
| 4522 | T2 = Fn->getType(); |
| 4523 | } |
| 4524 | |
| 4525 | // Compute some basic properties of the types and the initializer. |
| 4526 | bool dummy1 = false; |
| 4527 | bool dummy2 = false; |
| 4528 | bool dummy3 = false; |
| 4529 | Sema::ReferenceCompareResult RefRelationship |
| 4530 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4531 | dummy2, dummy3); |
| 4532 | |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4533 | if (RefRelationship >= Sema::Ref_Related) { |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4534 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4535 | SuppressUserConversions, |
| 4536 | /*AllowExplicit=*/false); |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4537 | } |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | // Otherwise, we bind the reference to a temporary created from the |
| 4541 | // initializer list. |
| 4542 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4543 | InOverloadResolution, |
| 4544 | AllowObjCWritebackConversion); |
| 4545 | if (Result.isFailure()) |
| 4546 | return Result; |
| 4547 | assert(!Result.isEllipsis() && |
| 4548 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4549 | |
| 4550 | // Can we even bind to a temporary? |
| 4551 | if (ToType->isRValueReferenceType() || |
| 4552 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4553 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4554 | Result.UserDefined.After; |
| 4555 | SCS.ReferenceBinding = true; |
| 4556 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4557 | SCS.BindsToRvalue = true; |
| 4558 | SCS.BindsToFunctionLvalue = false; |
| 4559 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4560 | SCS.ObjCLifetimeConversionBinding = false; |
| 4561 | } else |
| 4562 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4563 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4564 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4565 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4566 | |
| 4567 | // C++11 [over.ics.list]p6: |
| 4568 | // Otherwise, if the parameter type is not a class: |
| 4569 | if (!ToType->isRecordType()) { |
| 4570 | // - if the initializer list has one element, the implicit conversion |
| 4571 | // sequence is the one required to convert the element to the |
| 4572 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4573 | unsigned NumInits = From->getNumInits(); |
| 4574 | if (NumInits == 1) |
| 4575 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4576 | SuppressUserConversions, |
| 4577 | InOverloadResolution, |
| 4578 | AllowObjCWritebackConversion); |
| 4579 | // - if the initializer list has no elements, the implicit conversion |
| 4580 | // sequence is the identity conversion. |
| 4581 | else if (NumInits == 0) { |
| 4582 | Result.setStandard(); |
| 4583 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4584 | Result.Standard.setFromType(ToType); |
| 4585 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4586 | } |
| 4587 | return Result; |
| 4588 | } |
| 4589 | |
| 4590 | // C++11 [over.ics.list]p7: |
| 4591 | // In all cases other than those enumerated above, no conversion is possible |
| 4592 | return Result; |
| 4593 | } |
| 4594 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4595 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4596 | /// ToType from the expression From. Return the implicit conversion |
| 4597 | /// sequence required to pass this argument, which may be a bad |
| 4598 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4599 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4600 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4601 | static ImplicitConversionSequence |
| 4602 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4604 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4605 | bool AllowObjCWritebackConversion, |
| 4606 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4607 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4608 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4609 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4610 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4611 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4612 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4613 | /*FIXME:*/From->getLocStart(), |
| 4614 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4615 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4616 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4617 | return TryImplicitConversion(S, From, ToType, |
| 4618 | SuppressUserConversions, |
| 4619 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4620 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4621 | /*CStyle=*/false, |
| 4622 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4625 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4626 | const CanQualType ToQTy, |
| 4627 | Sema &S, |
| 4628 | SourceLocation Loc, |
| 4629 | ExprValueKind FromVK) { |
| 4630 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4631 | ImplicitConversionSequence ICS = |
| 4632 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4633 | |
| 4634 | return !ICS.isBad(); |
| 4635 | } |
| 4636 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4637 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4638 | /// parameter of the given member function (@c Method) from the |
| 4639 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4640 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4641 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4642 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4643 | CXXMethodDecl *Method, |
| 4644 | CXXRecordDecl *ActingContext) { |
| 4645 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4646 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4647 | // const volatile object. |
| 4648 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4649 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4650 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4651 | |
| 4652 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4653 | // to exit early. |
| 4654 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4655 | |
| 4656 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4657 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4658 | FromType = PT->getPointeeType(); |
| 4659 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4660 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4661 | // better have an lvalue. |
| 4662 | assert(FromClassification.isLValue()); |
| 4663 | } |
| 4664 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4665 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4667 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4668 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4669 | // parameter is |
| 4670 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4671 | // - "lvalue reference to cv X" for functions declared without a |
| 4672 | // ref-qualifier or with the & ref-qualifier |
| 4673 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4674 | // ref-qualifier |
| 4675 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | // where X is the class of which the function is a member and cv is the |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4677 | // cv-qualification on the member function declaration. |
| 4678 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4679 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4680 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4681 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4682 | // reference binding here, that allows class rvalues to bind to |
| 4683 | // non-constant references. |
| 4684 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4685 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4686 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4687 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4688 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4689 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4690 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4691 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4692 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4693 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4694 | |
| 4695 | // Check that we have either the same type or a derived type. It |
| 4696 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4697 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4698 | ImplicitConversionKind SecondKind; |
| 4699 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4700 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4701 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4702 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4703 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4704 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4705 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4706 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4707 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4709 | // Check the ref-qualifier. |
| 4710 | switch (Method->getRefQualifier()) { |
| 4711 | case RQ_None: |
| 4712 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4713 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4715 | case RQ_LValue: |
| 4716 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4717 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4719 | ImplicitParamType); |
| 4720 | return ICS; |
| 4721 | } |
| 4722 | break; |
| 4723 | |
| 4724 | case RQ_RValue: |
| 4725 | if (!FromClassification.isRValue()) { |
| 4726 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4728 | ImplicitParamType); |
| 4729 | return ICS; |
| 4730 | } |
| 4731 | break; |
| 4732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4734 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4735 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4736 | ICS.Standard.setAsIdentityConversion(); |
| 4737 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4738 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4739 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4740 | ICS.Standard.ReferenceBinding = true; |
| 4741 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4742 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4743 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4744 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4745 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4746 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4747 | return ICS; |
| 4748 | } |
| 4749 | |
| 4750 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4751 | /// the implicit object parameter for the given Method with the given |
| 4752 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4753 | ExprResult |
| 4754 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4755 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4756 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4757 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4758 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4760 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4762 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4763 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4764 | FromRecordType = PT->getPointeeType(); |
| 4765 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4766 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4767 | } else { |
| 4768 | FromRecordType = From->getType(); |
| 4769 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4770 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4773 | // Note that we always use the true parent context when performing |
| 4774 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4776 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4777 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4778 | if (ICS.isBad()) { |
| 4779 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4780 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4781 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4782 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4783 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4784 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4785 | diag::err_member_function_call_bad_cvr) |
| 4786 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4787 | << From->getSourceRange(); |
| 4788 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4789 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4790 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4791 | } |
| 4792 | } |
| 4793 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4794 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4795 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4796 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4797 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4798 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4799 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4800 | ExprResult FromRes = |
| 4801 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4802 | if (FromRes.isInvalid()) |
| 4803 | return ExprError(); |
| 4804 | From = FromRes.take(); |
| 4805 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4807 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4808 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4809 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4810 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4813 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4814 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4815 | static ImplicitConversionSequence |
| 4816 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4817 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4818 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4819 | // FIXME: Are these flags correct? |
| 4820 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4822 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4823 | /*CStyle=*/false, |
| 4824 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4828 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4829 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4830 | if (checkPlaceholderForOverload(*this, From)) |
| 4831 | return ExprError(); |
| 4832 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4833 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4834 | if (!ICS.isBad()) |
| 4835 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4837 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4838 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4839 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4840 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4841 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4843 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4844 | /// Check that the specified conversion is permitted in a converted constant |
| 4845 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4846 | /// is acceptable. |
| 4847 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4848 | StandardConversionSequence &SCS) { |
| 4849 | // Since we know that the target type is an integral or unscoped enumeration |
| 4850 | // type, most conversion kinds are impossible. All possible First and Third |
| 4851 | // conversions are fine. |
| 4852 | switch (SCS.Second) { |
| 4853 | case ICK_Identity: |
| 4854 | case ICK_Integral_Promotion: |
| 4855 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4856 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4857 | return true; |
| 4858 | |
| 4859 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4860 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4861 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4862 | // conversion, so it's permitted in a converted constant expression. |
| 4863 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4864 | SCS.getToType(2)->isBooleanType(); |
| 4865 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4866 | case ICK_Floating_Integral: |
| 4867 | case ICK_Complex_Real: |
| 4868 | return false; |
| 4869 | |
| 4870 | case ICK_Lvalue_To_Rvalue: |
| 4871 | case ICK_Array_To_Pointer: |
| 4872 | case ICK_Function_To_Pointer: |
| 4873 | case ICK_NoReturn_Adjustment: |
| 4874 | case ICK_Qualification: |
| 4875 | case ICK_Compatible_Conversion: |
| 4876 | case ICK_Vector_Conversion: |
| 4877 | case ICK_Vector_Splat: |
| 4878 | case ICK_Derived_To_Base: |
| 4879 | case ICK_Pointer_Conversion: |
| 4880 | case ICK_Pointer_Member: |
| 4881 | case ICK_Block_Pointer_Conversion: |
| 4882 | case ICK_Writeback_Conversion: |
| 4883 | case ICK_Floating_Promotion: |
| 4884 | case ICK_Complex_Promotion: |
| 4885 | case ICK_Complex_Conversion: |
| 4886 | case ICK_Floating_Conversion: |
| 4887 | case ICK_TransparentUnionConversion: |
| 4888 | llvm_unreachable("unexpected second conversion kind"); |
| 4889 | |
| 4890 | case ICK_Num_Conversion_Kinds: |
| 4891 | break; |
| 4892 | } |
| 4893 | |
| 4894 | llvm_unreachable("unknown conversion kind"); |
| 4895 | } |
| 4896 | |
| 4897 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4898 | /// converted constant expression of type T, perform the conversion and produce |
| 4899 | /// the converted expression, per C++11 [expr.const]p3. |
| 4900 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4901 | llvm::APSInt &Value, |
| 4902 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4903 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4904 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4905 | |
| 4906 | if (checkPlaceholderForOverload(*this, From)) |
| 4907 | return ExprError(); |
| 4908 | |
| 4909 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4910 | // A converted constant expression of type T is a core constant expression, |
| 4911 | // implicitly converted to a prvalue of type T, where the converted |
| 4912 | // expression is a literal constant expression and the implicit conversion |
| 4913 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4914 | // conversions, integral promotions, and integral conversions other than |
| 4915 | // narrowing conversions. |
| 4916 | ImplicitConversionSequence ICS = |
| 4917 | TryImplicitConversion(From, T, |
| 4918 | /*SuppressUserConversions=*/false, |
| 4919 | /*AllowExplicit=*/false, |
| 4920 | /*InOverloadResolution=*/false, |
| 4921 | /*CStyle=*/false, |
| 4922 | /*AllowObjcWritebackConversion=*/false); |
| 4923 | StandardConversionSequence *SCS = 0; |
| 4924 | switch (ICS.getKind()) { |
| 4925 | case ImplicitConversionSequence::StandardConversion: |
| 4926 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4927 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4928 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4929 | << From->getType() << From->getSourceRange() << T; |
| 4930 | SCS = &ICS.Standard; |
| 4931 | break; |
| 4932 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4933 | // We are converting from class type to an integral or enumeration type, so |
| 4934 | // the Before sequence must be trivial. |
| 4935 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4936 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4937 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4938 | << From->getType() << From->getSourceRange() << T; |
| 4939 | SCS = &ICS.UserDefined.After; |
| 4940 | break; |
| 4941 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4942 | case ImplicitConversionSequence::BadConversion: |
| 4943 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4944 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4945 | diag::err_typecheck_converted_constant_expression) |
| 4946 | << From->getType() << From->getSourceRange() << T; |
| 4947 | return ExprError(); |
| 4948 | |
| 4949 | case ImplicitConversionSequence::EllipsisConversion: |
| 4950 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4951 | } |
| 4952 | |
| 4953 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4954 | if (Result.isInvalid()) |
| 4955 | return Result; |
| 4956 | |
| 4957 | // Check for a narrowing implicit conversion. |
| 4958 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4959 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4960 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4961 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4962 | case NK_Variable_Narrowing: |
| 4963 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4964 | // expression. We'll diagnose this in a moment. |
| 4965 | case NK_Not_Narrowing: |
| 4966 | break; |
| 4967 | |
| 4968 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4969 | Diag(From->getLocStart(), |
| 4970 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4971 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4972 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4973 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4974 | break; |
| 4975 | |
| 4976 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4977 | Diag(From->getLocStart(), |
| 4978 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4979 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4980 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4981 | break; |
| 4982 | } |
| 4983 | |
| 4984 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4985 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4986 | Expr::EvalResult Eval; |
| 4987 | Eval.Diag = &Notes; |
| 4988 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 4989 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4990 | // The expression can't be folded, so we can't keep it at this position in |
| 4991 | // the AST. |
| 4992 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4993 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4994 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4995 | |
| 4996 | if (Notes.empty()) { |
| 4997 | // It's a constant expression. |
| 4998 | return Result; |
| 4999 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5003 | if (Notes.size() == 1 && |
| 5004 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5005 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5006 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5007 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5008 | << CCE << From->getSourceRange(); |
| 5009 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5010 | Diag(Notes[I].first, Notes[I].second); |
| 5011 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5012 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5015 | /// dropPointerConversions - If the given standard conversion sequence |
| 5016 | /// involves any pointer conversions, remove them. This may change |
| 5017 | /// the result type of the conversion sequence. |
| 5018 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5019 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5020 | SCS.Second = ICK_Identity; |
| 5021 | SCS.Third = ICK_Identity; |
| 5022 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5023 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5024 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5025 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5026 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5027 | /// convert the expression From to an Objective-C pointer type. |
| 5028 | static ImplicitConversionSequence |
| 5029 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5030 | // Do an implicit conversion to 'id'. |
| 5031 | QualType Ty = S.Context.getObjCIdType(); |
| 5032 | ImplicitConversionSequence ICS |
| 5033 | = TryImplicitConversion(S, From, Ty, |
| 5034 | // FIXME: Are these flags correct? |
| 5035 | /*SuppressUserConversions=*/false, |
| 5036 | /*AllowExplicit=*/true, |
| 5037 | /*InOverloadResolution=*/false, |
| 5038 | /*CStyle=*/false, |
| 5039 | /*AllowObjCWritebackConversion=*/false); |
| 5040 | |
| 5041 | // Strip off any final conversions to 'id'. |
| 5042 | switch (ICS.getKind()) { |
| 5043 | case ImplicitConversionSequence::BadConversion: |
| 5044 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5045 | case ImplicitConversionSequence::EllipsisConversion: |
| 5046 | break; |
| 5047 | |
| 5048 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5049 | dropPointerConversion(ICS.UserDefined.After); |
| 5050 | break; |
| 5051 | |
| 5052 | case ImplicitConversionSequence::StandardConversion: |
| 5053 | dropPointerConversion(ICS.Standard); |
| 5054 | break; |
| 5055 | } |
| 5056 | |
| 5057 | return ICS; |
| 5058 | } |
| 5059 | |
| 5060 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5061 | /// conversion of the expression From to an Objective-C pointer type. |
| 5062 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5063 | if (checkPlaceholderForOverload(*this, From)) |
| 5064 | return ExprError(); |
| 5065 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5066 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5067 | ImplicitConversionSequence ICS = |
| 5068 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5069 | if (!ICS.isBad()) |
| 5070 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5071 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5072 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5073 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5074 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5075 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5076 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5077 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5078 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5081 | static ExprResult |
| 5082 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5083 | Sema::ContextualImplicitConverter &Converter, |
| 5084 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5085 | |
| 5086 | if (Converter.Suppress) |
| 5087 | return ExprError(); |
| 5088 | |
| 5089 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5090 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5091 | CXXConversionDecl *Conv = |
| 5092 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5093 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5094 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5095 | } |
| 5096 | return SemaRef.Owned(From); |
| 5097 | } |
| 5098 | |
| 5099 | static bool |
| 5100 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5101 | Sema::ContextualImplicitConverter &Converter, |
| 5102 | QualType T, bool HadMultipleCandidates, |
| 5103 | UnresolvedSetImpl &ExplicitConversions) { |
| 5104 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5105 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5106 | CXXConversionDecl *Conversion = |
| 5107 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5108 | |
| 5109 | // The user probably meant to invoke the given explicit |
| 5110 | // conversion; use it. |
| 5111 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5112 | std::string TypeStr; |
| 5113 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5114 | |
| 5115 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5116 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5117 | "static_cast<" + TypeStr + ">(") |
| 5118 | << FixItHint::CreateInsertion( |
| 5119 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5120 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5121 | |
| 5122 | // If we aren't in a SFINAE context, build a call to the |
| 5123 | // explicit conversion function. |
| 5124 | if (SemaRef.isSFINAEContext()) |
| 5125 | return true; |
| 5126 | |
| 5127 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5128 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5129 | HadMultipleCandidates); |
| 5130 | if (Result.isInvalid()) |
| 5131 | return true; |
| 5132 | // Record usage of conversion in an implicit cast. |
| 5133 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5134 | CK_UserDefinedConversion, Result.get(), 0, |
| 5135 | Result.get()->getValueKind()); |
| 5136 | } |
| 5137 | return false; |
| 5138 | } |
| 5139 | |
| 5140 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5141 | Sema::ContextualImplicitConverter &Converter, |
| 5142 | QualType T, bool HadMultipleCandidates, |
| 5143 | DeclAccessPair &Found) { |
| 5144 | CXXConversionDecl *Conversion = |
| 5145 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5146 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5147 | |
| 5148 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5149 | if (!Converter.SuppressConversion) { |
| 5150 | if (SemaRef.isSFINAEContext()) |
| 5151 | return true; |
| 5152 | |
| 5153 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5154 | << From->getSourceRange(); |
| 5155 | } |
| 5156 | |
| 5157 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5158 | HadMultipleCandidates); |
| 5159 | if (Result.isInvalid()) |
| 5160 | return true; |
| 5161 | // Record usage of conversion in an implicit cast. |
| 5162 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5163 | CK_UserDefinedConversion, Result.get(), 0, |
| 5164 | Result.get()->getValueKind()); |
| 5165 | return false; |
| 5166 | } |
| 5167 | |
| 5168 | static ExprResult finishContextualImplicitConversion( |
| 5169 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5170 | Sema::ContextualImplicitConverter &Converter) { |
| 5171 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5172 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5173 | << From->getSourceRange(); |
| 5174 | |
| 5175 | return SemaRef.DefaultLvalueConversion(From); |
| 5176 | } |
| 5177 | |
| 5178 | static void |
| 5179 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5180 | UnresolvedSetImpl &ViableConversions, |
| 5181 | OverloadCandidateSet &CandidateSet) { |
| 5182 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5183 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5184 | NamedDecl *D = FoundDecl.getDecl(); |
| 5185 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5186 | if (isa<UsingShadowDecl>(D)) |
| 5187 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5188 | |
| 5189 | CXXConversionDecl *Conv; |
| 5190 | FunctionTemplateDecl *ConvTemplate; |
| 5191 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5192 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5193 | else |
| 5194 | Conv = cast<CXXConversionDecl>(D); |
| 5195 | |
| 5196 | if (ConvTemplate) |
| 5197 | SemaRef.AddTemplateConversionCandidate( |
| 5198 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet); |
| 5199 | else |
| 5200 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| 5201 | ToType, CandidateSet); |
| 5202 | } |
| 5203 | } |
| 5204 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5205 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5206 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5207 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5208 | /// This routine will attempt to convert an expression of class type to a |
| 5209 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5210 | /// must have a single non-explicit conversion function converting to a matching |
| 5211 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5212 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5213 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5214 | /// \param Loc The source location of the construct that requires the |
| 5215 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5216 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5217 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5218 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5219 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5220 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5221 | /// \returns The expression, converted to an integral or enumeration type if |
| 5222 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5223 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5224 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5225 | // We can't perform any more checking for type-dependent expressions. |
| 5226 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5227 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5228 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5229 | // Process placeholders immediately. |
| 5230 | if (From->hasPlaceholderType()) { |
| 5231 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5232 | if (result.isInvalid()) |
| 5233 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5234 | From = result.take(); |
| 5235 | } |
| 5236 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5237 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5238 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5239 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5240 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5241 | |
| 5242 | // FIXME: Check for missing '()' if T is a function type? |
| 5243 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5244 | // We can only perform contextual implicit conversions on objects of class |
| 5245 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5246 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5247 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5248 | if (!Converter.Suppress) |
| 5249 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5250 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5251 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5252 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5253 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5254 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5255 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5256 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5257 | |
| 5258 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5259 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5260 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5261 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5262 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5263 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5264 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5265 | |
| 5266 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5267 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5268 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5269 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5270 | UnresolvedSet<4> |
| 5271 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5272 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5273 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5274 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5275 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5276 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5277 | bool HadMultipleCandidates = |
| 5278 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5279 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5280 | // To check that there is only one target type, in C++1y: |
| 5281 | QualType ToType; |
| 5282 | bool HasUniqueTargetType = true; |
| 5283 | |
| 5284 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5285 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5286 | E = Conversions.second; |
| 5287 | I != E; ++I) { |
| 5288 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5289 | CXXConversionDecl *Conversion; |
| 5290 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5291 | if (ConvTemplate) { |
| 5292 | if (getLangOpts().CPlusPlus1y) |
| 5293 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5294 | else |
| 5295 | continue; // C++11 does not consider conversion operator templates(?). |
| 5296 | } else |
| 5297 | Conversion = cast<CXXConversionDecl>(D); |
| 5298 | |
| 5299 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5300 | "Conversion operator templates are considered potentially " |
| 5301 | "viable in C++1y"); |
| 5302 | |
| 5303 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5304 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5305 | |
| 5306 | if (Conversion->isExplicit()) { |
| 5307 | // FIXME: For C++1y, do we need this restriction? |
| 5308 | // cf. diagnoseNoViableConversion() |
| 5309 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5310 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5311 | } else { |
| 5312 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5313 | if (ToType.isNull()) |
| 5314 | ToType = CurToType.getUnqualifiedType(); |
| 5315 | else if (HasUniqueTargetType && |
| 5316 | (CurToType.getUnqualifiedType() != ToType)) |
| 5317 | HasUniqueTargetType = false; |
| 5318 | } |
| 5319 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5320 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5321 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5324 | if (getLangOpts().CPlusPlus1y) { |
| 5325 | // C++1y [conv]p6: |
| 5326 | // ... An expression e of class type E appearing in such a context |
| 5327 | // is said to be contextually implicitly converted to a specified |
| 5328 | // type T and is well-formed if and only if e can be implicitly |
| 5329 | // converted to a type T that is determined as follows: E is searched |
Larisse Voufo | 7acc5a6 | 2013-06-10 08:25:58 +0000 | [diff] [blame] | 5330 | // for conversion functions whose return type is cv T or reference to |
| 5331 | // cv T such that T is allowed by the context. There shall be |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5332 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5333 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5334 | // If no unique T is found: |
| 5335 | if (ToType.isNull()) { |
| 5336 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5337 | HadMultipleCandidates, |
| 5338 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5339 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5340 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5341 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5342 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5343 | // If more than one unique Ts are found: |
| 5344 | if (!HasUniqueTargetType) |
| 5345 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5346 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5347 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5348 | // If one unique T is found: |
| 5349 | // First, build a candidate set from the previously recorded |
| 5350 | // potentially viable conversions. |
| 5351 | OverloadCandidateSet CandidateSet(Loc); |
| 5352 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5353 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5354 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5355 | // Then, perform overload resolution over the candidate set. |
| 5356 | OverloadCandidateSet::iterator Best; |
| 5357 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5358 | case OR_Success: { |
| 5359 | // Apply this conversion. |
| 5360 | DeclAccessPair Found = |
| 5361 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5362 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5363 | HadMultipleCandidates, Found)) |
| 5364 | return ExprError(); |
| 5365 | break; |
| 5366 | } |
| 5367 | case OR_Ambiguous: |
| 5368 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5369 | ViableConversions); |
| 5370 | case OR_No_Viable_Function: |
| 5371 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5372 | HadMultipleCandidates, |
| 5373 | ExplicitConversions)) |
| 5374 | return ExprError(); |
| 5375 | // fall through 'OR_Deleted' case. |
| 5376 | case OR_Deleted: |
| 5377 | // We'll complain below about a non-integral condition type. |
| 5378 | break; |
| 5379 | } |
| 5380 | } else { |
| 5381 | switch (ViableConversions.size()) { |
| 5382 | case 0: { |
| 5383 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5384 | HadMultipleCandidates, |
| 5385 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5386 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5387 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5388 | // We'll complain below about a non-integral condition type. |
| 5389 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5390 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5391 | case 1: { |
| 5392 | // Apply this conversion. |
| 5393 | DeclAccessPair Found = ViableConversions[0]; |
| 5394 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5395 | HadMultipleCandidates, Found)) |
| 5396 | return ExprError(); |
| 5397 | break; |
| 5398 | } |
| 5399 | default: |
| 5400 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5401 | ViableConversions); |
| 5402 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5403 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5405 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5408 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5409 | /// candidate functions, using the given function call arguments. If |
| 5410 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5411 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5412 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5413 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5414 | /// based on an incomplete set of function arguments. This feature is used by |
| 5415 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | void |
| 5417 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5418 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5419 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5420 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5421 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5422 | bool PartialOverloading, |
| 5423 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5425 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5426 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5427 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5428 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5430 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5431 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5432 | // If we get here, it's because we're calling a member function |
| 5433 | // that is named without a member access expression (e.g., |
| 5434 | // "this->f") that was either written explicitly or created |
| 5435 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5436 | // function, e.g., X::f(). We use an empty type for the implied |
| 5437 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5438 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5439 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5440 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5441 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5442 | return; |
| 5443 | } |
| 5444 | // We treat a constructor like a non-member function, since its object |
| 5445 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5448 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5449 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5450 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5451 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5452 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5453 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5454 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5455 | // C++ [class.copy]p3: |
| 5456 | // A member function template is never instantiated to perform the copy |
| 5457 | // of a class object to an object of its class type. |
| 5458 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5459 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5460 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5461 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5462 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5463 | return; |
| 5464 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5465 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5466 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5467 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5468 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5469 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5470 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5471 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5472 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5473 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5475 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5476 | |
| 5477 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5478 | // parameters is viable only if it has an ellipsis in its parameter |
| 5479 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5480 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5481 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5482 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5483 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5484 | return; |
| 5485 | } |
| 5486 | |
| 5487 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5488 | // is viable only if the (m+1)st parameter has a default argument |
| 5489 | // (8.3.6). For the purposes of overload resolution, the |
| 5490 | // parameter list is truncated on the right, so that there are |
| 5491 | // exactly m parameters. |
| 5492 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5493 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5494 | // Not enough arguments. |
| 5495 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5496 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5497 | return; |
| 5498 | } |
| 5499 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5500 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5501 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5502 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5503 | if (CheckCUDATarget(Caller, Function)) { |
| 5504 | Candidate.Viable = false; |
| 5505 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5506 | return; |
| 5507 | } |
| 5508 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5509 | // Determine the implicit conversion sequences for each of the |
| 5510 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5511 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5512 | if (ArgIdx < NumArgsInProto) { |
| 5513 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5514 | // exist for each argument an implicit conversion sequence |
| 5515 | // (13.3.3.1) that converts that argument to the corresponding |
| 5516 | // parameter of F. |
| 5517 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5518 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5519 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5520 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5521 | /*InOverloadResolution=*/true, |
| 5522 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5523 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5524 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5525 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5526 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5527 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5528 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5529 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5530 | } else { |
| 5531 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5532 | // argument for which there is no corresponding parameter is |
| 5533 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5534 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5535 | } |
| 5536 | } |
| 5537 | } |
| 5538 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5539 | /// \brief Add all of the function declarations in the given function set to |
Nick Lewycky | 199e370 | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 5540 | /// the overload candidate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5541 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5542 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5543 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5544 | bool SuppressUserConversions, |
| 5545 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5546 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5547 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5548 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5549 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5550 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5551 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5552 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5553 | Args.slice(1), CandidateSet, |
| 5554 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5555 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5556 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5557 | SuppressUserConversions); |
| 5558 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5559 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5560 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5561 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5562 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5563 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5564 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5565 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5566 | Args[0]->Classify(Context), Args.slice(1), |
| 5567 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5568 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5569 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5570 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5571 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5572 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5573 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5576 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5577 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5578 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5579 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5580 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5581 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5582 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5583 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5584 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5585 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5586 | |
| 5587 | if (isa<UsingShadowDecl>(Decl)) |
| 5588 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5589 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5590 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5591 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5592 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5593 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5594 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5595 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5596 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5597 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5598 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5599 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5600 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5601 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5602 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5603 | } |
| 5604 | } |
| 5605 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5606 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5607 | /// of candidate functions, using the given function call arguments |
| 5608 | /// and the object argument (@c Object). For example, in a call |
| 5609 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5610 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5611 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5612 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5614 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5615 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5616 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5617 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5618 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5619 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5621 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5622 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5623 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5624 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5625 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5626 | if (!CandidateSet.isNewCandidate(Method)) |
| 5627 | return; |
| 5628 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5629 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5630 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5632 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5633 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5634 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5635 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5636 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5637 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5638 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5639 | |
| 5640 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5641 | |
| 5642 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5643 | // parameters is viable only if it has an ellipsis in its parameter |
| 5644 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5645 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5646 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5647 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5648 | return; |
| 5649 | } |
| 5650 | |
| 5651 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5652 | // is viable only if the (m+1)st parameter has a default argument |
| 5653 | // (8.3.6). For the purposes of overload resolution, the |
| 5654 | // parameter list is truncated on the right, so that there are |
| 5655 | // exactly m parameters. |
| 5656 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5657 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5658 | // Not enough arguments. |
| 5659 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5660 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5661 | return; |
| 5662 | } |
| 5663 | |
| 5664 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5665 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5666 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5667 | // The implicit object argument is ignored. |
| 5668 | Candidate.IgnoreObjectArgument = true; |
| 5669 | else { |
| 5670 | // Determine the implicit conversion sequence for the object |
| 5671 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5672 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5673 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5674 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5675 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5676 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5677 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5678 | return; |
| 5679 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | // Determine the implicit conversion sequences for each of the |
| 5683 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5684 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5685 | if (ArgIdx < NumArgsInProto) { |
| 5686 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5687 | // exist for each argument an implicit conversion sequence |
| 5688 | // (13.3.3.1) that converts that argument to the corresponding |
| 5689 | // parameter of F. |
| 5690 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5691 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5692 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5693 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5694 | /*InOverloadResolution=*/true, |
| 5695 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5696 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5697 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5698 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5699 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5700 | break; |
| 5701 | } |
| 5702 | } else { |
| 5703 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5704 | // argument for which there is no corresponding parameter is |
| 5705 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5706 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5711 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5712 | /// set, using template argument deduction to produce an appropriate member |
| 5713 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5714 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5715 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5716 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5717 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5718 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5719 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5720 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5721 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5722 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5723 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5724 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5725 | return; |
| 5726 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5727 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5728 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5729 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5730 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5731 | // candidate functions in the usual way.113) A given name can refer to one |
| 5732 | // or more function templates and also to a set of overloaded non-template |
| 5733 | // functions. In such a case, the candidate functions generated from each |
| 5734 | // function template are combined with the set of non-template candidate |
| 5735 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5736 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5737 | FunctionDecl *Specialization = 0; |
| 5738 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5739 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5740 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5741 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5742 | Candidate.FoundDecl = FoundDecl; |
| 5743 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5744 | Candidate.Viable = false; |
| 5745 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5746 | Candidate.IsSurrogate = false; |
| 5747 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5748 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5749 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5750 | Info); |
| 5751 | return; |
| 5752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5753 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5754 | // Add the function template specialization produced by template argument |
| 5755 | // deduction as a candidate. |
| 5756 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5757 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5758 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5759 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5760 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5761 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5764 | /// \brief Add a C++ function template specialization as a candidate |
| 5765 | /// in the candidate set, using template argument deduction to produce |
| 5766 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5767 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5768 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5769 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5770 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5771 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5772 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5773 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5774 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5775 | return; |
| 5776 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5777 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5778 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5779 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5781 | // candidate functions in the usual way.113) A given name can refer to one |
| 5782 | // or more function templates and also to a set of overloaded non-template |
| 5783 | // functions. In such a case, the candidate functions generated from each |
| 5784 | // function template are combined with the set of non-template candidate |
| 5785 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5786 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5787 | FunctionDecl *Specialization = 0; |
| 5788 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5789 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5790 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5791 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5792 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5793 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5794 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5795 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5796 | Candidate.IsSurrogate = false; |
| 5797 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5798 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5800 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5801 | return; |
| 5802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5804 | // Add the function template specialization produced by template argument |
| 5805 | // deduction as a candidate. |
| 5806 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5807 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5808 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5810 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5811 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5813 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5814 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5815 | /// (which may or may not be the same type as the type that the |
| 5816 | /// conversion function produces). |
| 5817 | void |
| 5818 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5819 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5820 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5821 | Expr *From, QualType ToType, |
| 5822 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5823 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5824 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5825 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5826 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5827 | return; |
| 5828 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5829 | // If the conversion function has an undeduced return type, trigger its |
| 5830 | // deduction now. |
| 5831 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5832 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5833 | return; |
| 5834 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5835 | } |
| 5836 | |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5837 | // Per C++ [over.match.conv]p1, [over.match.ref]p1, an explicit conversion |
| 5838 | // operator is only a candidate if its return type is the target type or |
| 5839 | // can be converted to the target type with a qualification conversion. |
| 5840 | bool ObjCLifetimeConversion; |
| 5841 | QualType ToNonRefType = ToType.getNonReferenceType(); |
| 5842 | if (Conversion->isExplicit() && |
| 5843 | !Context.hasSameUnqualifiedType(ConvType, ToNonRefType) && |
| 5844 | !IsQualificationConversion(ConvType, ToNonRefType, /*CStyle*/false, |
| 5845 | ObjCLifetimeConversion)) |
| 5846 | return; |
| 5847 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5848 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5849 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5850 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5851 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5852 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5853 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5854 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5855 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5856 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5857 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5858 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5859 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5860 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5861 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5862 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5863 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | // For conversion functions, the function is considered to be a member of |
| 5865 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5866 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5867 | // |
| 5868 | // Determine the implicit conversion sequence for the implicit |
| 5869 | // object parameter. |
| 5870 | QualType ImplicitParamType = From->getType(); |
| 5871 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5872 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5873 | CXXRecordDecl *ConversionContext |
| 5874 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5876 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5877 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5878 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5879 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5880 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5881 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5882 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5883 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5884 | return; |
| 5885 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5886 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | // We won't go through a user-define type conversion function to convert a |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5888 | // derived to base as such conversions are given Conversion Rank. They only |
| 5889 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5890 | QualType FromCanon |
| 5891 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5892 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5893 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5894 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5895 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5896 | return; |
| 5897 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5898 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5899 | // To determine what the conversion from the result of calling the |
| 5900 | // conversion function to the type we're eventually trying to |
| 5901 | // convert to (ToType), we need to synthesize a call to the |
| 5902 | // conversion function and attempt copy initialization from it. This |
| 5903 | // makes sure that we get the right semantics with respect to |
| 5904 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5905 | // call on the stack and we don't need its arguments to be |
| 5906 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5907 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5908 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5909 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5910 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5911 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5912 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5913 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5914 | QualType ConversionType = Conversion->getConversionType(); |
| 5915 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5916 | Candidate.Viable = false; |
| 5917 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5918 | return; |
| 5919 | } |
| 5920 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5921 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5922 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5923 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5924 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5925 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5926 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 5927 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5928 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5929 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5930 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5931 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5932 | /*InOverloadResolution=*/false, |
| 5933 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5934 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5935 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5936 | case ImplicitConversionSequence::StandardConversion: |
| 5937 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5939 | // C++ [over.ics.user]p3: |
| 5940 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5941 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5942 | // shall have exact match rank. |
| 5943 | if (Conversion->getPrimaryTemplate() && |
| 5944 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5945 | Candidate.Viable = false; |
| 5946 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5947 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5948 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5949 | // C++0x [dcl.init.ref]p5: |
| 5950 | // In the second case, if the reference is an rvalue reference and |
| 5951 | // the second standard conversion sequence of the user-defined |
| 5952 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5953 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5954 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5955 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5956 | Candidate.Viable = false; |
| 5957 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5958 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5959 | break; |
| 5960 | |
| 5961 | case ImplicitConversionSequence::BadConversion: |
| 5962 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5963 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | |
| 5966 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5967 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5968 | "Can only end up with a standard conversion sequence or failure"); |
| 5969 | } |
| 5970 | } |
| 5971 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5972 | /// \brief Adds a conversion function template specialization |
| 5973 | /// candidate to the overload set, using template argument deduction |
| 5974 | /// to deduce the template arguments of the conversion function |
| 5975 | /// template from the type that we are converting to (C++ |
| 5976 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5977 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5978 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5979 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5980 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5981 | Expr *From, QualType ToType, |
| 5982 | OverloadCandidateSet &CandidateSet) { |
| 5983 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5984 | "Only conversion function templates permitted here"); |
| 5985 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5986 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5987 | return; |
| 5988 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5989 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5990 | CXXConversionDecl *Specialization = 0; |
| 5991 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5992 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5993 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5994 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5995 | Candidate.FoundDecl = FoundDecl; |
| 5996 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5997 | Candidate.Viable = false; |
| 5998 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5999 | Candidate.IsSurrogate = false; |
| 6000 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6001 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6002 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6003 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6004 | return; |
| 6005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6007 | // Add the conversion function template specialization produced by |
| 6008 | // template argument deduction as a candidate. |
| 6009 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6010 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6011 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6014 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6015 | /// converts the given @c Object to a function pointer via the |
| 6016 | /// conversion function @c Conversion, and then attempts to call it |
| 6017 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6018 | /// the type of function that we'll eventually be calling. |
| 6019 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6020 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6021 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6022 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6023 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6024 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6025 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6026 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6027 | return; |
| 6028 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6029 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6030 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6031 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6032 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6033 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6034 | Candidate.Function = 0; |
| 6035 | Candidate.Surrogate = Conversion; |
| 6036 | Candidate.Viable = true; |
| 6037 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6038 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6039 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6040 | |
| 6041 | // Determine the implicit conversion sequence for the implicit |
| 6042 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6043 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6045 | Object->Classify(Context), |
| 6046 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6047 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6048 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6049 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6050 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6051 | return; |
| 6052 | } |
| 6053 | |
| 6054 | // The first conversion is actually a user-defined conversion whose |
| 6055 | // first conversion is ObjectInit's standard conversion (which is |
| 6056 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6057 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6058 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6059 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6060 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6061 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6062 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6063 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6064 | = Candidate.Conversions[0].UserDefined.Before; |
| 6065 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6066 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6067 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6068 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6069 | |
| 6070 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6071 | // parameters is viable only if it has an ellipsis in its parameter |
| 6072 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6073 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6074 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6075 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6076 | return; |
| 6077 | } |
| 6078 | |
| 6079 | // Function types don't have any default arguments, so just check if |
| 6080 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6081 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6082 | // Not enough arguments. |
| 6083 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6084 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6085 | return; |
| 6086 | } |
| 6087 | |
| 6088 | // Determine the implicit conversion sequences for each of the |
| 6089 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6090 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6091 | if (ArgIdx < NumArgsInProto) { |
| 6092 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6093 | // exist for each argument an implicit conversion sequence |
| 6094 | // (13.3.3.1) that converts that argument to the corresponding |
| 6095 | // parameter of F. |
| 6096 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6097 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6098 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6099 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6100 | /*InOverloadResolution=*/false, |
| 6101 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6102 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6103 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6104 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6105 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6106 | break; |
| 6107 | } |
| 6108 | } else { |
| 6109 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6110 | // argument for which there is no corresponding parameter is |
| 6111 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6112 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6113 | } |
| 6114 | } |
| 6115 | } |
| 6116 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6117 | /// \brief Add overload candidates for overloaded operators that are |
| 6118 | /// member functions. |
| 6119 | /// |
| 6120 | /// Add the overloaded operator candidates that are member functions |
| 6121 | /// for the operator Op that was used in an operator expression such |
| 6122 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6123 | /// CandidateSet will store the added overload candidates. (C++ |
| 6124 | /// [over.match.oper]). |
| 6125 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6126 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6127 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6128 | OverloadCandidateSet& CandidateSet, |
| 6129 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6130 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6131 | |
| 6132 | // C++ [over.match.oper]p3: |
| 6133 | // For a unary operator @ with an operand of a type whose |
| 6134 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6135 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6136 | // a right operand of a type whose cv-unqualified version is T2, |
| 6137 | // three sets of candidate functions, designated member |
| 6138 | // candidates, non-member candidates and built-in candidates, are |
| 6139 | // constructed as follows: |
| 6140 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6141 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6142 | // -- If T1 is a complete class type or a class currently being |
| 6143 | // defined, the set of member candidates is the result of the |
| 6144 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6145 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6146 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6147 | // Complete the type if it can be completed. |
| 6148 | RequireCompleteType(OpLoc, T1, 0); |
| 6149 | // If the type is neither complete nor being defined, bail out now. |
| 6150 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6151 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6152 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6153 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6154 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6155 | Operators.suppressDiagnostics(); |
| 6156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6157 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6158 | OperEnd = Operators.end(); |
| 6159 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6160 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6161 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6162 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6163 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6164 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6165 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6166 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6169 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6170 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6171 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6172 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6173 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6174 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6175 | /// (at the beginning of the argument list) that will be contextually |
| 6176 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6177 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6178 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6179 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6180 | bool IsAssignmentOperator, |
| 6181 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6182 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6183 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6185 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6186 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6187 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6188 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6189 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6190 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6191 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6192 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6193 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6194 | |
| 6195 | // Determine the implicit conversion sequences for each of the |
| 6196 | // arguments. |
| 6197 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6198 | Candidate.ExplicitCallArguments = Args.size(); |
| 6199 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6200 | // C++ [over.match.oper]p4: |
| 6201 | // For the built-in assignment operators, conversions of the |
| 6202 | // left operand are restricted as follows: |
| 6203 | // -- no temporaries are introduced to hold the left operand, and |
| 6204 | // -- no user-defined conversions are applied to the left |
| 6205 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6206 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6207 | // |
| 6208 | // We block these conversions by turning off user-defined |
| 6209 | // conversions, since that is the only way that initialization of |
| 6210 | // a reference to a non-class type can occur from something that |
| 6211 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6212 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6213 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6214 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6215 | Candidate.Conversions[ArgIdx] |
| 6216 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6217 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6218 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6219 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6220 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6221 | /*InOverloadResolution=*/false, |
| 6222 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6223 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6224 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6225 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6226 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6227 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6228 | break; |
| 6229 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6230 | } |
| 6231 | } |
| 6232 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6233 | namespace { |
| 6234 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6235 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6236 | /// candidate operator functions for built-in operators (C++ |
| 6237 | /// [over.built]). The types are separated into pointer types and |
| 6238 | /// enumeration types. |
| 6239 | class BuiltinCandidateTypeSet { |
| 6240 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6241 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6242 | |
| 6243 | /// PointerTypes - The set of pointer types that will be used in the |
| 6244 | /// built-in candidates. |
| 6245 | TypeSet PointerTypes; |
| 6246 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6247 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6248 | /// used in the built-in candidates. |
| 6249 | TypeSet MemberPointerTypes; |
| 6250 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6251 | /// EnumerationTypes - The set of enumeration types that will be |
| 6252 | /// used in the built-in candidates. |
| 6253 | TypeSet EnumerationTypes; |
| 6254 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6255 | /// \brief The set of vector types that will be used in the built-in |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6256 | /// candidates. |
| 6257 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6258 | |
| 6259 | /// \brief A flag indicating non-record types are viable candidates |
| 6260 | bool HasNonRecordTypes; |
| 6261 | |
| 6262 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6263 | /// were present in the candidate set. |
| 6264 | bool HasArithmeticOrEnumeralTypes; |
| 6265 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6266 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6267 | /// candidate set. |
| 6268 | bool HasNullPtrType; |
| 6269 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6270 | /// Sema - The semantic analysis instance where we are building the |
| 6271 | /// candidate type set. |
| 6272 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6274 | /// Context - The AST context in which we will build the type sets. |
| 6275 | ASTContext &Context; |
| 6276 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6277 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6278 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6279 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6280 | |
| 6281 | public: |
| 6282 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6283 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6284 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6285 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6286 | : HasNonRecordTypes(false), |
| 6287 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6288 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6289 | SemaRef(SemaRef), |
| 6290 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6291 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6292 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6293 | SourceLocation Loc, |
| 6294 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6295 | bool AllowExplicitConversions, |
| 6296 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6297 | |
| 6298 | /// pointer_begin - First pointer type found; |
| 6299 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6300 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6301 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6302 | iterator pointer_end() { return PointerTypes.end(); } |
| 6303 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6304 | /// member_pointer_begin - First member pointer type found; |
| 6305 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6306 | |
| 6307 | /// member_pointer_end - Past the last member pointer type found; |
| 6308 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6309 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6310 | /// enumeration_begin - First enumeration type found; |
| 6311 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6312 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6313 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6314 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6315 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6316 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6317 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6318 | |
| 6319 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6320 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6321 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6322 | }; |
| 6323 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6324 | } // end anonymous namespace |
| 6325 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6326 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6327 | /// the set of pointer types along with any more-qualified variants of |
| 6328 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6329 | /// will add "int const *", "int const volatile *", "int const |
| 6330 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6331 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6332 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6333 | /// |
| 6334 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6335 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6336 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6337 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6338 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6339 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6340 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6341 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6342 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6343 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6344 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6345 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6346 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6347 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6348 | PointeeTy = PTy->getPointeeType(); |
| 6349 | buildObjCPtr = true; |
| 6350 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6351 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6354 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6355 | // (the qualifier would sink to the element type), and for another, the |
| 6356 | // only overload situation where it matters is subscript or pointer +- int, |
| 6357 | // and those shouldn't have qualifier variants anyway. |
| 6358 | if (PointeeTy->isArrayType()) |
| 6359 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6360 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6361 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6362 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6363 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6364 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6365 | // Iterate through all strict supersets of BaseCVR. |
| 6366 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6367 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6368 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6369 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6370 | |
| 6371 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6372 | // the type cannot be restrict-qualified. |
| 6373 | if ((CVR & Qualifiers::Restrict) && |
| 6374 | (!hasRestrict || |
| 6375 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6376 | continue; |
| 6377 | |
| 6378 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6379 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6380 | |
| 6381 | // Build qualified pointer type. |
| 6382 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6383 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6384 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6385 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6386 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6387 | |
| 6388 | // Insert qualified pointer type. |
| 6389 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | return true; |
| 6393 | } |
| 6394 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6395 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6396 | /// to the set of pointer types along with any more-qualified variants of |
| 6397 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6398 | /// will add "int const *", "int const volatile *", "int const |
| 6399 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6400 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6401 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6402 | /// |
| 6403 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6404 | bool |
| 6405 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6406 | QualType Ty) { |
| 6407 | // Insert this type. |
| 6408 | if (!MemberPointerTypes.insert(Ty)) |
| 6409 | return false; |
| 6410 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6411 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6412 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6413 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6414 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6415 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6416 | // (the qualifier would sink to the element type), and for another, the |
| 6417 | // only overload situation where it matters is subscript or pointer +- int, |
| 6418 | // and those shouldn't have qualifier variants anyway. |
| 6419 | if (PointeeTy->isArrayType()) |
| 6420 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6421 | const Type *ClassTy = PointerTy->getClass(); |
| 6422 | |
| 6423 | // Iterate through all strict supersets of the pointee type's CVR |
| 6424 | // qualifiers. |
| 6425 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6426 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6427 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6429 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6430 | MemberPointerTypes.insert( |
| 6431 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | return true; |
| 6435 | } |
| 6436 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6437 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6438 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6439 | /// primarily interested in pointer types and enumeration types. We also |
| 6440 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6441 | /// AllowUserConversions is true if we should look at the conversion |
| 6442 | /// functions of a class type, and AllowExplicitConversions if we |
| 6443 | /// should also include the explicit conversion functions of a class |
| 6444 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6445 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6446 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6447 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6448 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6449 | bool AllowExplicitConversions, |
| 6450 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6451 | // Only deal with canonical types. |
| 6452 | Ty = Context.getCanonicalType(Ty); |
| 6453 | |
| 6454 | // Look through reference types; they aren't part of the type of an |
| 6455 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6456 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6457 | Ty = RefTy->getPointeeType(); |
| 6458 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6459 | // If we're dealing with an array type, decay to the pointer. |
| 6460 | if (Ty->isArrayType()) |
| 6461 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6462 | |
| 6463 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6464 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6465 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6466 | // Flag if we ever add a non-record type. |
| 6467 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6468 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6469 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6470 | // Flag if we encounter an arithmetic type. |
| 6471 | HasArithmeticOrEnumeralTypes = |
| 6472 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6473 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6474 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6475 | PointerTypes.insert(Ty); |
| 6476 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6477 | // Insert our type, and its more-qualified variants, into the set |
| 6478 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6479 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6480 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6481 | } else if (Ty->isMemberPointerType()) { |
| 6482 | // Member pointers are far easier, since the pointee can't be converted. |
| 6483 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6484 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6485 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6486 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6487 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6488 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6489 | // We treat vector types as arithmetic types in many contexts as an |
| 6490 | // extension. |
| 6491 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6492 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6493 | } else if (Ty->isNullPtrType()) { |
| 6494 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6495 | } else if (AllowUserConversions && TyRec) { |
| 6496 | // No conversion functions in incomplete types. |
| 6497 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6498 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6500 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6501 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6502 | CXXRecordDecl::conversion_iterator> |
| 6503 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6504 | for (CXXRecordDecl::conversion_iterator |
| 6505 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6506 | NamedDecl *D = I.getDecl(); |
| 6507 | if (isa<UsingShadowDecl>(D)) |
| 6508 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6509 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6510 | // Skip conversion function templates; they don't tell us anything |
| 6511 | // about which builtin types we can convert to. |
| 6512 | if (isa<FunctionTemplateDecl>(D)) |
| 6513 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6514 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6515 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6516 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6517 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6518 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6519 | } |
| 6520 | } |
| 6521 | } |
| 6522 | } |
| 6523 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6524 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6525 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6526 | /// given type to the candidate set. |
| 6527 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6528 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6529 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6530 | OverloadCandidateSet &CandidateSet) { |
| 6531 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6532 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6533 | // T& operator=(T&, T) |
| 6534 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6535 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6536 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6537 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6538 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6539 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6540 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6541 | ParamTypes[0] |
| 6542 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6543 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6544 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6545 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6546 | } |
| 6547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6548 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6549 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6550 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6551 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6552 | Qualifiers VRQuals; |
| 6553 | const RecordType *TyRec; |
| 6554 | if (const MemberPointerType *RHSMPType = |
| 6555 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6556 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6557 | else |
| 6558 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6559 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6560 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6561 | VRQuals.addVolatile(); |
| 6562 | VRQuals.addRestrict(); |
| 6563 | return VRQuals; |
| 6564 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6565 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6566 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6567 | if (!ClassDecl->hasDefinition()) |
| 6568 | return VRQuals; |
| 6569 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6570 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6571 | CXXRecordDecl::conversion_iterator> |
| 6572 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6573 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6574 | for (CXXRecordDecl::conversion_iterator |
| 6575 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6576 | NamedDecl *D = I.getDecl(); |
| 6577 | if (isa<UsingShadowDecl>(D)) |
| 6578 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6579 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6580 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6581 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6582 | CanTy = ResTypeRef->getPointeeType(); |
| 6583 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6584 | // as see them. |
| 6585 | bool done = false; |
| 6586 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6587 | if (CanTy.isRestrictQualified()) |
| 6588 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6589 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6590 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6591 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6592 | CanTy->getAs<MemberPointerType>()) |
| 6593 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6594 | else |
| 6595 | done = true; |
| 6596 | if (CanTy.isVolatileQualified()) |
| 6597 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6598 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6599 | return VRQuals; |
| 6600 | } |
| 6601 | } |
| 6602 | } |
| 6603 | return VRQuals; |
| 6604 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6605 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6606 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6607 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6608 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6609 | /// candidates. It provides shared state and utility methods used throughout |
| 6610 | /// the process, as well as a helper method to add each group of builtin |
| 6611 | /// operator overloads from the standard to a candidate set. |
| 6612 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6613 | // Common instance state available to all overload candidate addition methods. |
| 6614 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6615 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6616 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6617 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6618 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6619 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6620 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6621 | // Define some constants used to index and iterate over the arithemetic types |
| 6622 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6623 | // The "promoted arithmetic types" are the arithmetic |
| 6624 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6625 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6626 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6627 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6628 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6629 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6630 | LastPromotedArithmeticType = 11; |
| 6631 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6632 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6633 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6634 | CanQualType getArithmeticType(unsigned index) { |
| 6635 | assert(index < NumArithmeticTypes); |
| 6636 | static CanQualType ASTContext::* const |
| 6637 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6638 | // Start of promoted types. |
| 6639 | &ASTContext::FloatTy, |
| 6640 | &ASTContext::DoubleTy, |
| 6641 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6642 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6643 | // Start of integral types. |
| 6644 | &ASTContext::IntTy, |
| 6645 | &ASTContext::LongTy, |
| 6646 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6647 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6648 | &ASTContext::UnsignedIntTy, |
| 6649 | &ASTContext::UnsignedLongTy, |
| 6650 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6651 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6652 | // End of promoted types. |
| 6653 | |
| 6654 | &ASTContext::BoolTy, |
| 6655 | &ASTContext::CharTy, |
| 6656 | &ASTContext::WCharTy, |
| 6657 | &ASTContext::Char16Ty, |
| 6658 | &ASTContext::Char32Ty, |
| 6659 | &ASTContext::SignedCharTy, |
| 6660 | &ASTContext::ShortTy, |
| 6661 | &ASTContext::UnsignedCharTy, |
| 6662 | &ASTContext::UnsignedShortTy, |
| 6663 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6664 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6665 | }; |
| 6666 | return S.Context.*ArithmeticTypes[index]; |
| 6667 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6668 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6669 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6670 | /// converions for the given arithmetic types. |
| 6671 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6672 | // Accelerator table for performing the usual arithmetic conversions. |
| 6673 | // The rules are basically: |
| 6674 | // - if either is floating-point, use the wider floating-point |
| 6675 | // - if same signedness, use the higher rank |
| 6676 | // - if same size, use unsigned of the higher rank |
| 6677 | // - use the larger type |
| 6678 | // These rules, together with the axiom that higher ranks are |
| 6679 | // never smaller, are sufficient to precompute all of these results |
| 6680 | // *except* when dealing with signed types of higher rank. |
| 6681 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6682 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6683 | // We assume that int128 has a higher rank than long long on all platforms. |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6684 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6685 | Dep=-1, |
| 6686 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6687 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6688 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6689 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6690 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6691 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6692 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6693 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6694 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6695 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6696 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6697 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6698 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6699 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6700 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6701 | }; |
| 6702 | |
| 6703 | assert(L < LastPromotedArithmeticType); |
| 6704 | assert(R < LastPromotedArithmeticType); |
| 6705 | int Idx = ConversionsTable[L][R]; |
| 6706 | |
| 6707 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6708 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6709 | |
| 6710 | // Slow path: we need to compare widths. |
| 6711 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6712 | CanQualType LT = getArithmeticType(L), |
| 6713 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6714 | unsigned LW = S.Context.getIntWidth(LT), |
| 6715 | RW = S.Context.getIntWidth(RT); |
| 6716 | |
| 6717 | // If they're different widths, use the signed type. |
| 6718 | if (LW > RW) return LT; |
| 6719 | else if (LW < RW) return RT; |
| 6720 | |
| 6721 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6722 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6723 | assert(L == SLL || R == SLL); |
| 6724 | return S.Context.UnsignedLongLongTy; |
| 6725 | } |
| 6726 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6727 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6728 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6729 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6730 | bool HasVolatile, |
| 6731 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6732 | QualType ParamTypes[2] = { |
| 6733 | S.Context.getLValueReferenceType(CandidateTy), |
| 6734 | S.Context.IntTy |
| 6735 | }; |
| 6736 | |
| 6737 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6738 | if (Args.size() == 1) |
| 6739 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6740 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6741 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6742 | |
| 6743 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6744 | // add volatile version only if there are conversions to a volatile type. |
| 6745 | if (HasVolatile) { |
| 6746 | ParamTypes[0] = |
| 6747 | S.Context.getLValueReferenceType( |
| 6748 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6749 | if (Args.size() == 1) |
| 6750 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6751 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6752 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6753 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6754 | |
| 6755 | // Add restrict version only if there are conversions to a restrict type |
| 6756 | // and our candidate type is a non-restrict-qualified pointer. |
| 6757 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6758 | !CandidateTy.isRestrictQualified()) { |
| 6759 | ParamTypes[0] |
| 6760 | = S.Context.getLValueReferenceType( |
| 6761 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6762 | if (Args.size() == 1) |
| 6763 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6764 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6765 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6766 | |
| 6767 | if (HasVolatile) { |
| 6768 | ParamTypes[0] |
| 6769 | = S.Context.getLValueReferenceType( |
| 6770 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6771 | (Qualifiers::Volatile | |
| 6772 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6773 | if (Args.size() == 1) |
| 6774 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6775 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6776 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6777 | } |
| 6778 | } |
| 6779 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | public: |
| 6783 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6784 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6785 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6786 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6787 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6788 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6789 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6790 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6791 | HasArithmeticOrEnumeralCandidateType( |
| 6792 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6793 | CandidateTypes(CandidateTypes), |
| 6794 | CandidateSet(CandidateSet) { |
| 6795 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6796 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6797 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6798 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6799 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6800 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6801 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6802 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6803 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6804 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6805 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6806 | "Invalid last promoted arithmetic type"); |
| 6807 | } |
| 6808 | |
| 6809 | // C++ [over.built]p3: |
| 6810 | // |
| 6811 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6812 | // is either volatile or empty, there exist candidate operator |
| 6813 | // functions of the form |
| 6814 | // |
| 6815 | // VQ T& operator++(VQ T&); |
| 6816 | // T operator++(VQ T&, int); |
| 6817 | // |
| 6818 | // C++ [over.built]p4: |
| 6819 | // |
| 6820 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6821 | // than bool, and VQ is either volatile or empty, there exist |
| 6822 | // candidate operator functions of the form |
| 6823 | // |
| 6824 | // VQ T& operator--(VQ T&); |
| 6825 | // T operator--(VQ T&, int); |
| 6826 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6827 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6828 | return; |
| 6829 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6830 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6831 | Arith < NumArithmeticTypes; ++Arith) { |
| 6832 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6833 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6834 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6835 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6836 | } |
| 6837 | } |
| 6838 | |
| 6839 | // C++ [over.built]p5: |
| 6840 | // |
| 6841 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6842 | // cv-unqualified object type, and VQ is either volatile or |
| 6843 | // empty, there exist candidate operator functions of the form |
| 6844 | // |
| 6845 | // T*VQ& operator++(T*VQ&); |
| 6846 | // T*VQ& operator--(T*VQ&); |
| 6847 | // T* operator++(T*VQ&, int); |
| 6848 | // T* operator--(T*VQ&, int); |
| 6849 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6850 | for (BuiltinCandidateTypeSet::iterator |
| 6851 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6852 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6853 | Ptr != PtrEnd; ++Ptr) { |
| 6854 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6855 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6856 | continue; |
| 6857 | |
| 6858 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6859 | (!(*Ptr).isVolatileQualified() && |
| 6860 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6861 | (!(*Ptr).isRestrictQualified() && |
| 6862 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6863 | } |
| 6864 | } |
| 6865 | |
| 6866 | // C++ [over.built]p6: |
| 6867 | // For every cv-qualified or cv-unqualified object type T, there |
| 6868 | // exist candidate operator functions of the form |
| 6869 | // |
| 6870 | // T& operator*(T*); |
| 6871 | // |
| 6872 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6873 | // For every function type T that does not have cv-qualifiers or a |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6874 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6875 | // T& operator*(T*); |
| 6876 | void addUnaryStarPointerOverloads() { |
| 6877 | for (BuiltinCandidateTypeSet::iterator |
| 6878 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6879 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6880 | Ptr != PtrEnd; ++Ptr) { |
| 6881 | QualType ParamTy = *Ptr; |
| 6882 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6883 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6884 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6885 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6886 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6887 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6888 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6889 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6890 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6891 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6892 | } |
| 6893 | } |
| 6894 | |
| 6895 | // C++ [over.built]p9: |
| 6896 | // For every promoted arithmetic type T, there exist candidate |
| 6897 | // operator functions of the form |
| 6898 | // |
| 6899 | // T operator+(T); |
| 6900 | // T operator-(T); |
| 6901 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6902 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6903 | return; |
| 6904 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6905 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6906 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6907 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6908 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | // Extension: We also add these operators for vector types. |
| 6912 | for (BuiltinCandidateTypeSet::iterator |
| 6913 | Vec = CandidateTypes[0].vector_begin(), |
| 6914 | VecEnd = CandidateTypes[0].vector_end(); |
| 6915 | Vec != VecEnd; ++Vec) { |
| 6916 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6917 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | // C++ [over.built]p8: |
| 6922 | // For every type T, there exist candidate operator functions of |
| 6923 | // the form |
| 6924 | // |
| 6925 | // T* operator+(T*); |
| 6926 | void addUnaryPlusPointerOverloads() { |
| 6927 | for (BuiltinCandidateTypeSet::iterator |
| 6928 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6929 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6930 | Ptr != PtrEnd; ++Ptr) { |
| 6931 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6932 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6933 | } |
| 6934 | } |
| 6935 | |
| 6936 | // C++ [over.built]p10: |
| 6937 | // For every promoted integral type T, there exist candidate |
| 6938 | // operator functions of the form |
| 6939 | // |
| 6940 | // T operator~(T); |
| 6941 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6942 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6943 | return; |
| 6944 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6945 | for (unsigned Int = FirstPromotedIntegralType; |
| 6946 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6947 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6948 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | // Extension: We also add this operator for vector types. |
| 6952 | for (BuiltinCandidateTypeSet::iterator |
| 6953 | Vec = CandidateTypes[0].vector_begin(), |
| 6954 | VecEnd = CandidateTypes[0].vector_end(); |
| 6955 | Vec != VecEnd; ++Vec) { |
| 6956 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6957 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6958 | } |
| 6959 | } |
| 6960 | |
| 6961 | // C++ [over.match.oper]p16: |
| 6962 | // For every pointer to member type T, there exist candidate operator |
| 6963 | // functions of the form |
| 6964 | // |
| 6965 | // bool operator==(T,T); |
| 6966 | // bool operator!=(T,T); |
| 6967 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6968 | /// Set of (canonical) types that we've already handled. |
| 6969 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6970 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6971 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6972 | for (BuiltinCandidateTypeSet::iterator |
| 6973 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6974 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6975 | MemPtr != MemPtrEnd; |
| 6976 | ++MemPtr) { |
| 6977 | // Don't add the same builtin candidate twice. |
| 6978 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6979 | continue; |
| 6980 | |
| 6981 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6982 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6983 | } |
| 6984 | } |
| 6985 | } |
| 6986 | |
| 6987 | // C++ [over.built]p15: |
| 6988 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6989 | // For every T, where T is an enumeration type, a pointer type, or |
| 6990 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6991 | // |
| 6992 | // bool operator<(T, T); |
| 6993 | // bool operator>(T, T); |
| 6994 | // bool operator<=(T, T); |
| 6995 | // bool operator>=(T, T); |
| 6996 | // bool operator==(T, T); |
| 6997 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6998 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6999 | // C++ [over.match.oper]p3: |
| 7000 | // [...]the built-in candidates include all of the candidate operator |
| 7001 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 7002 | // do not have the same parameter-type-list as any non-template non-member |
| 7003 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7004 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7005 | // Note that in practice, this only affects enumeration types because there |
| 7006 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7007 | // must have an operand of record or enumeration type. Also, the only other |
| 7008 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7009 | // cannot be overloaded for enumeration types, so this is the only place |
| 7010 | // where we must suppress candidates like this. |
| 7011 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7012 | UserDefinedBinaryOperators; |
| 7013 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7014 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7015 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7016 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7017 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7018 | CEnd = CandidateSet.end(); |
| 7019 | C != CEnd; ++C) { |
| 7020 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7021 | continue; |
| 7022 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7023 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7024 | continue; |
| 7025 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7026 | QualType FirstParamType = |
| 7027 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7028 | QualType SecondParamType = |
| 7029 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7030 | |
| 7031 | // Skip if either parameter isn't of enumeral type. |
| 7032 | if (!FirstParamType->isEnumeralType() || |
| 7033 | !SecondParamType->isEnumeralType()) |
| 7034 | continue; |
| 7035 | |
| 7036 | // Add this operator to the set of known user-defined operators. |
| 7037 | UserDefinedBinaryOperators.insert( |
| 7038 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7039 | S.Context.getCanonicalType(SecondParamType))); |
| 7040 | } |
| 7041 | } |
| 7042 | } |
| 7043 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7044 | /// Set of (canonical) types that we've already handled. |
| 7045 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7046 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7047 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7048 | for (BuiltinCandidateTypeSet::iterator |
| 7049 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7050 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7051 | Ptr != PtrEnd; ++Ptr) { |
| 7052 | // Don't add the same builtin candidate twice. |
| 7053 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7054 | continue; |
| 7055 | |
| 7056 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7057 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7058 | } |
| 7059 | for (BuiltinCandidateTypeSet::iterator |
| 7060 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7061 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7062 | Enum != EnumEnd; ++Enum) { |
| 7063 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7064 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7065 | // Don't add the same builtin candidate twice, or if a user defined |
| 7066 | // candidate exists. |
| 7067 | if (!AddedTypes.insert(CanonType) || |
| 7068 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7069 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7070 | continue; |
| 7071 | |
| 7072 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7073 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7074 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7075 | |
| 7076 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7077 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7078 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7079 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7080 | NullPtrTy))) { |
| 7081 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7082 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7083 | CandidateSet); |
| 7084 | } |
| 7085 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7086 | } |
| 7087 | } |
| 7088 | |
| 7089 | // C++ [over.built]p13: |
| 7090 | // |
| 7091 | // For every cv-qualified or cv-unqualified object type T |
| 7092 | // there exist candidate operator functions of the form |
| 7093 | // |
| 7094 | // T* operator+(T*, ptrdiff_t); |
| 7095 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7096 | // T* operator-(T*, ptrdiff_t); |
| 7097 | // T* operator+(ptrdiff_t, T*); |
| 7098 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7099 | // |
| 7100 | // C++ [over.built]p14: |
| 7101 | // |
| 7102 | // For every T, where T is a pointer to object type, there |
| 7103 | // exist candidate operator functions of the form |
| 7104 | // |
| 7105 | // ptrdiff_t operator-(T, T); |
| 7106 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7107 | /// Set of (canonical) types that we've already handled. |
| 7108 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7109 | |
| 7110 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7111 | QualType AsymetricParamTypes[2] = { |
| 7112 | S.Context.getPointerDiffType(), |
| 7113 | S.Context.getPointerDiffType(), |
| 7114 | }; |
| 7115 | for (BuiltinCandidateTypeSet::iterator |
| 7116 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7117 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7118 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7119 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7120 | if (!PointeeTy->isObjectType()) |
| 7121 | continue; |
| 7122 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7123 | AsymetricParamTypes[Arg] = *Ptr; |
| 7124 | if (Arg == 0 || Op == OO_Plus) { |
| 7125 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7126 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7127 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7128 | } |
| 7129 | if (Op == OO_Minus) { |
| 7130 | // ptrdiff_t operator-(T, T); |
| 7131 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7132 | continue; |
| 7133 | |
| 7134 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7135 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7136 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7137 | } |
| 7138 | } |
| 7139 | } |
| 7140 | } |
| 7141 | |
| 7142 | // C++ [over.built]p12: |
| 7143 | // |
| 7144 | // For every pair of promoted arithmetic types L and R, there |
| 7145 | // exist candidate operator functions of the form |
| 7146 | // |
| 7147 | // LR operator*(L, R); |
| 7148 | // LR operator/(L, R); |
| 7149 | // LR operator+(L, R); |
| 7150 | // LR operator-(L, R); |
| 7151 | // bool operator<(L, R); |
| 7152 | // bool operator>(L, R); |
| 7153 | // bool operator<=(L, R); |
| 7154 | // bool operator>=(L, R); |
| 7155 | // bool operator==(L, R); |
| 7156 | // bool operator!=(L, R); |
| 7157 | // |
| 7158 | // where LR is the result of the usual arithmetic conversions |
| 7159 | // between types L and R. |
| 7160 | // |
| 7161 | // C++ [over.built]p24: |
| 7162 | // |
| 7163 | // For every pair of promoted arithmetic types L and R, there exist |
| 7164 | // candidate operator functions of the form |
| 7165 | // |
| 7166 | // LR operator?(bool, L, R); |
| 7167 | // |
| 7168 | // where LR is the result of the usual arithmetic conversions |
| 7169 | // between types L and R. |
| 7170 | // Our candidates ignore the first parameter. |
| 7171 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7172 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7173 | return; |
| 7174 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7175 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7176 | Left < LastPromotedArithmeticType; ++Left) { |
| 7177 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7178 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7179 | QualType LandR[2] = { getArithmeticType(Left), |
| 7180 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7181 | QualType Result = |
| 7182 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7183 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7184 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7185 | } |
| 7186 | } |
| 7187 | |
| 7188 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7189 | // conditional operator for vector types. |
| 7190 | for (BuiltinCandidateTypeSet::iterator |
| 7191 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7192 | Vec1End = CandidateTypes[0].vector_end(); |
| 7193 | Vec1 != Vec1End; ++Vec1) { |
| 7194 | for (BuiltinCandidateTypeSet::iterator |
| 7195 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7196 | Vec2End = CandidateTypes[1].vector_end(); |
| 7197 | Vec2 != Vec2End; ++Vec2) { |
| 7198 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7199 | QualType Result = S.Context.BoolTy; |
| 7200 | if (!isComparison) { |
| 7201 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7202 | Result = *Vec1; |
| 7203 | else |
| 7204 | Result = *Vec2; |
| 7205 | } |
| 7206 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7207 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7208 | } |
| 7209 | } |
| 7210 | } |
| 7211 | |
| 7212 | // C++ [over.built]p17: |
| 7213 | // |
| 7214 | // For every pair of promoted integral types L and R, there |
| 7215 | // exist candidate operator functions of the form |
| 7216 | // |
| 7217 | // LR operator%(L, R); |
| 7218 | // LR operator&(L, R); |
| 7219 | // LR operator^(L, R); |
| 7220 | // LR operator|(L, R); |
| 7221 | // L operator<<(L, R); |
| 7222 | // L operator>>(L, R); |
| 7223 | // |
| 7224 | // where LR is the result of the usual arithmetic conversions |
| 7225 | // between types L and R. |
| 7226 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7227 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7228 | return; |
| 7229 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7230 | for (unsigned Left = FirstPromotedIntegralType; |
| 7231 | Left < LastPromotedIntegralType; ++Left) { |
| 7232 | for (unsigned Right = FirstPromotedIntegralType; |
| 7233 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7234 | QualType LandR[2] = { getArithmeticType(Left), |
| 7235 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7236 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7237 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7238 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7239 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7240 | } |
| 7241 | } |
| 7242 | } |
| 7243 | |
| 7244 | // C++ [over.built]p20: |
| 7245 | // |
| 7246 | // For every pair (T, VQ), where T is an enumeration or |
| 7247 | // pointer to member type and VQ is either volatile or |
| 7248 | // empty, there exist candidate operator functions of the form |
| 7249 | // |
| 7250 | // VQ T& operator=(VQ T&, T); |
| 7251 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7252 | /// Set of (canonical) types that we've already handled. |
| 7253 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7254 | |
| 7255 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7256 | for (BuiltinCandidateTypeSet::iterator |
| 7257 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7258 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7259 | Enum != EnumEnd; ++Enum) { |
| 7260 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7261 | continue; |
| 7262 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7263 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
| 7266 | for (BuiltinCandidateTypeSet::iterator |
| 7267 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7268 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7269 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7270 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7271 | continue; |
| 7272 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7273 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7274 | } |
| 7275 | } |
| 7276 | } |
| 7277 | |
| 7278 | // C++ [over.built]p19: |
| 7279 | // |
| 7280 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7281 | // volatile or empty, there exist candidate operator functions |
| 7282 | // of the form |
| 7283 | // |
| 7284 | // T*VQ& operator=(T*VQ&, T*); |
| 7285 | // |
| 7286 | // C++ [over.built]p21: |
| 7287 | // |
| 7288 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7289 | // cv-unqualified object type and VQ is either volatile or |
| 7290 | // empty, there exist candidate operator functions of the form |
| 7291 | // |
| 7292 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7293 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7294 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7295 | /// Set of (canonical) types that we've already handled. |
| 7296 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7297 | |
| 7298 | for (BuiltinCandidateTypeSet::iterator |
| 7299 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7300 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7301 | Ptr != PtrEnd; ++Ptr) { |
| 7302 | // If this is operator=, keep track of the builtin candidates we added. |
| 7303 | if (isEqualOp) |
| 7304 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7305 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7306 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7307 | |
| 7308 | // non-volatile version |
| 7309 | QualType ParamTypes[2] = { |
| 7310 | S.Context.getLValueReferenceType(*Ptr), |
| 7311 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7312 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7313 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7314 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7315 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7316 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7317 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7318 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7319 | // volatile version |
| 7320 | ParamTypes[0] = |
| 7321 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7322 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7323 | /*IsAssigmentOperator=*/isEqualOp); |
| 7324 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7325 | |
| 7326 | if (!(*Ptr).isRestrictQualified() && |
| 7327 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7328 | // restrict version |
| 7329 | ParamTypes[0] |
| 7330 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7331 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7332 | /*IsAssigmentOperator=*/isEqualOp); |
| 7333 | |
| 7334 | if (NeedVolatile) { |
| 7335 | // volatile restrict version |
| 7336 | ParamTypes[0] |
| 7337 | = S.Context.getLValueReferenceType( |
| 7338 | S.Context.getCVRQualifiedType(*Ptr, |
| 7339 | (Qualifiers::Volatile | |
| 7340 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7341 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7342 | /*IsAssigmentOperator=*/isEqualOp); |
| 7343 | } |
| 7344 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | if (isEqualOp) { |
| 7348 | for (BuiltinCandidateTypeSet::iterator |
| 7349 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7350 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7351 | Ptr != PtrEnd; ++Ptr) { |
| 7352 | // Make sure we don't add the same candidate twice. |
| 7353 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7354 | continue; |
| 7355 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7356 | QualType ParamTypes[2] = { |
| 7357 | S.Context.getLValueReferenceType(*Ptr), |
| 7358 | *Ptr, |
| 7359 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7360 | |
| 7361 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7362 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7363 | /*IsAssigmentOperator=*/true); |
| 7364 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7365 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7366 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7367 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7368 | // volatile version |
| 7369 | ParamTypes[0] = |
| 7370 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7371 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7372 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7373 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7374 | |
| 7375 | if (!(*Ptr).isRestrictQualified() && |
| 7376 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7377 | // restrict version |
| 7378 | ParamTypes[0] |
| 7379 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7380 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7381 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7382 | |
| 7383 | if (NeedVolatile) { |
| 7384 | // volatile restrict version |
| 7385 | ParamTypes[0] |
| 7386 | = S.Context.getLValueReferenceType( |
| 7387 | S.Context.getCVRQualifiedType(*Ptr, |
| 7388 | (Qualifiers::Volatile | |
| 7389 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7390 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7391 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7392 | } |
| 7393 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7394 | } |
| 7395 | } |
| 7396 | } |
| 7397 | |
| 7398 | // C++ [over.built]p18: |
| 7399 | // |
| 7400 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7401 | // VQ is either volatile or empty, and R is a promoted |
| 7402 | // arithmetic type, there exist candidate operator functions of |
| 7403 | // the form |
| 7404 | // |
| 7405 | // VQ L& operator=(VQ L&, R); |
| 7406 | // VQ L& operator*=(VQ L&, R); |
| 7407 | // VQ L& operator/=(VQ L&, R); |
| 7408 | // VQ L& operator+=(VQ L&, R); |
| 7409 | // VQ L& operator-=(VQ L&, R); |
| 7410 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7411 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7412 | return; |
| 7413 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7414 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7415 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7416 | Right < LastPromotedArithmeticType; ++Right) { |
| 7417 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7418 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7419 | |
| 7420 | // Add this built-in operator as a candidate (VQ is empty). |
| 7421 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7422 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7423 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7424 | /*IsAssigmentOperator=*/isEqualOp); |
| 7425 | |
| 7426 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7427 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7428 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7429 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7430 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7431 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7432 | /*IsAssigmentOperator=*/isEqualOp); |
| 7433 | } |
| 7434 | } |
| 7435 | } |
| 7436 | |
| 7437 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7438 | for (BuiltinCandidateTypeSet::iterator |
| 7439 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7440 | Vec1End = CandidateTypes[0].vector_end(); |
| 7441 | Vec1 != Vec1End; ++Vec1) { |
| 7442 | for (BuiltinCandidateTypeSet::iterator |
| 7443 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7444 | Vec2End = CandidateTypes[1].vector_end(); |
| 7445 | Vec2 != Vec2End; ++Vec2) { |
| 7446 | QualType ParamTypes[2]; |
| 7447 | ParamTypes[1] = *Vec2; |
| 7448 | // Add this built-in operator as a candidate (VQ is empty). |
| 7449 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7450 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7451 | /*IsAssigmentOperator=*/isEqualOp); |
| 7452 | |
| 7453 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7454 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7455 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7456 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7457 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7458 | /*IsAssigmentOperator=*/isEqualOp); |
| 7459 | } |
| 7460 | } |
| 7461 | } |
| 7462 | } |
| 7463 | |
| 7464 | // C++ [over.built]p22: |
| 7465 | // |
| 7466 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7467 | // is either volatile or empty, and R is a promoted integral |
| 7468 | // type, there exist candidate operator functions of the form |
| 7469 | // |
| 7470 | // VQ L& operator%=(VQ L&, R); |
| 7471 | // VQ L& operator<<=(VQ L&, R); |
| 7472 | // VQ L& operator>>=(VQ L&, R); |
| 7473 | // VQ L& operator&=(VQ L&, R); |
| 7474 | // VQ L& operator^=(VQ L&, R); |
| 7475 | // VQ L& operator|=(VQ L&, R); |
| 7476 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7477 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7478 | return; |
| 7479 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7480 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7481 | for (unsigned Right = FirstPromotedIntegralType; |
| 7482 | Right < LastPromotedIntegralType; ++Right) { |
| 7483 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7484 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7485 | |
| 7486 | // Add this built-in operator as a candidate (VQ is empty). |
| 7487 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7488 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7489 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7490 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7491 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7492 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7493 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7494 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7495 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7496 | } |
| 7497 | } |
| 7498 | } |
| 7499 | } |
| 7500 | |
| 7501 | // C++ [over.operator]p23: |
| 7502 | // |
| 7503 | // There also exist candidate operator functions of the form |
| 7504 | // |
| 7505 | // bool operator!(bool); |
| 7506 | // bool operator&&(bool, bool); |
| 7507 | // bool operator||(bool, bool); |
| 7508 | void addExclaimOverload() { |
| 7509 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7510 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7511 | /*IsAssignmentOperator=*/false, |
| 7512 | /*NumContextualBoolArguments=*/1); |
| 7513 | } |
| 7514 | void addAmpAmpOrPipePipeOverload() { |
| 7515 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7516 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7517 | /*IsAssignmentOperator=*/false, |
| 7518 | /*NumContextualBoolArguments=*/2); |
| 7519 | } |
| 7520 | |
| 7521 | // C++ [over.built]p13: |
| 7522 | // |
| 7523 | // For every cv-qualified or cv-unqualified object type T there |
| 7524 | // exist candidate operator functions of the form |
| 7525 | // |
| 7526 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7527 | // T& operator[](T*, ptrdiff_t); |
| 7528 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7529 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7530 | // T& operator[](ptrdiff_t, T*); |
| 7531 | void addSubscriptOverloads() { |
| 7532 | for (BuiltinCandidateTypeSet::iterator |
| 7533 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7534 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7535 | Ptr != PtrEnd; ++Ptr) { |
| 7536 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7537 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7538 | if (!PointeeType->isObjectType()) |
| 7539 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7540 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7541 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7542 | |
| 7543 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7544 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7545 | } |
| 7546 | |
| 7547 | for (BuiltinCandidateTypeSet::iterator |
| 7548 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7549 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7550 | Ptr != PtrEnd; ++Ptr) { |
| 7551 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7552 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7553 | if (!PointeeType->isObjectType()) |
| 7554 | continue; |
| 7555 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7556 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7557 | |
| 7558 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7559 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7560 | } |
| 7561 | } |
| 7562 | |
| 7563 | // C++ [over.built]p11: |
| 7564 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7565 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7566 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7567 | // there exist candidate operator functions of the form |
| 7568 | // |
| 7569 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7570 | // |
| 7571 | // where CV12 is the union of CV1 and CV2. |
| 7572 | void addArrowStarOverloads() { |
| 7573 | for (BuiltinCandidateTypeSet::iterator |
| 7574 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7575 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7576 | Ptr != PtrEnd; ++Ptr) { |
| 7577 | QualType C1Ty = (*Ptr); |
| 7578 | QualType C1; |
| 7579 | QualifierCollector Q1; |
| 7580 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7581 | if (!isa<RecordType>(C1)) |
| 7582 | continue; |
| 7583 | // heuristic to reduce number of builtin candidates in the set. |
| 7584 | // Add volatile/restrict version only if there are conversions to a |
| 7585 | // volatile/restrict type. |
| 7586 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7587 | continue; |
| 7588 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7589 | continue; |
| 7590 | for (BuiltinCandidateTypeSet::iterator |
| 7591 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7592 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7593 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7594 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7595 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7596 | C2 = C2.getUnqualifiedType(); |
| 7597 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7598 | break; |
| 7599 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7600 | // build CV12 T& |
| 7601 | QualType T = mptr->getPointeeType(); |
| 7602 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7603 | T.isVolatileQualified()) |
| 7604 | continue; |
| 7605 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7606 | T.isRestrictQualified()) |
| 7607 | continue; |
| 7608 | T = Q1.apply(S.Context, T); |
| 7609 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7610 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7611 | } |
| 7612 | } |
| 7613 | } |
| 7614 | |
| 7615 | // Note that we don't consider the first argument, since it has been |
| 7616 | // contextually converted to bool long ago. The candidates below are |
| 7617 | // therefore added as binary. |
| 7618 | // |
| 7619 | // C++ [over.built]p25: |
| 7620 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7621 | // enumeration type, there exist candidate operator functions of the form |
| 7622 | // |
| 7623 | // T operator?(bool, T, T); |
| 7624 | // |
| 7625 | void addConditionalOperatorOverloads() { |
| 7626 | /// Set of (canonical) types that we've already handled. |
| 7627 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7628 | |
| 7629 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7630 | for (BuiltinCandidateTypeSet::iterator |
| 7631 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7632 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7633 | Ptr != PtrEnd; ++Ptr) { |
| 7634 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7635 | continue; |
| 7636 | |
| 7637 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7638 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | for (BuiltinCandidateTypeSet::iterator |
| 7642 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7643 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7644 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7645 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7646 | continue; |
| 7647 | |
| 7648 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7649 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7652 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7653 | for (BuiltinCandidateTypeSet::iterator |
| 7654 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7655 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7656 | Enum != EnumEnd; ++Enum) { |
| 7657 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7658 | continue; |
| 7659 | |
| 7660 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7661 | continue; |
| 7662 | |
| 7663 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7664 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7665 | } |
| 7666 | } |
| 7667 | } |
| 7668 | } |
| 7669 | }; |
| 7670 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7671 | } // end anonymous namespace |
| 7672 | |
| 7673 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7674 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7675 | /// on the operator @p Op and the arguments given. For example, if the |
| 7676 | /// operator is a binary '+', this routine might add "int |
| 7677 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7678 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7679 | SourceLocation OpLoc, |
| 7680 | ArrayRef<Expr *> Args, |
| 7681 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7682 | // Find all of the types that the arguments can convert to, but only |
| 7683 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7684 | // that make use of these types. Also record whether we encounter non-record |
| 7685 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7686 | Qualifiers VisibleTypeConversionsQuals; |
| 7687 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7688 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7689 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7690 | |
| 7691 | bool HasNonRecordCandidateType = false; |
| 7692 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7693 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7694 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7695 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7696 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7697 | OpLoc, |
| 7698 | true, |
| 7699 | (Op == OO_Exclaim || |
| 7700 | Op == OO_AmpAmp || |
| 7701 | Op == OO_PipePipe), |
| 7702 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7703 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7704 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7705 | HasArithmeticOrEnumeralCandidateType = |
| 7706 | HasArithmeticOrEnumeralCandidateType || |
| 7707 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7708 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7709 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7710 | // Exit early when no non-record types have been added to the candidate set |
| 7711 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7712 | // |
| 7713 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7714 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7715 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7716 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7717 | return; |
| 7718 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7719 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7720 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7721 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7722 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7723 | CandidateTypes, CandidateSet); |
| 7724 | |
| 7725 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7726 | switch (Op) { |
| 7727 | case OO_None: |
| 7728 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7729 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7730 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7731 | case OO_New: |
| 7732 | case OO_Delete: |
| 7733 | case OO_Array_New: |
| 7734 | case OO_Array_Delete: |
| 7735 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7736 | llvm_unreachable( |
| 7737 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7738 | |
| 7739 | case OO_Comma: |
| 7740 | case OO_Arrow: |
| 7741 | // C++ [over.match.oper]p3: |
| 7742 | // -- For the operator ',', the unary operator '&', or the |
| 7743 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7744 | break; |
| 7745 | |
| 7746 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7747 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7748 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7749 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7750 | |
| 7751 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7752 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7753 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7754 | } else { |
| 7755 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7756 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7757 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7758 | break; |
| 7759 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7760 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7761 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7762 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7763 | else |
| 7764 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7765 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7766 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7767 | case OO_Slash: |
| 7768 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7769 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7770 | |
| 7771 | case OO_PlusPlus: |
| 7772 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7773 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7774 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7775 | break; |
| 7776 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7777 | case OO_EqualEqual: |
| 7778 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7779 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7780 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7781 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7782 | case OO_Less: |
| 7783 | case OO_Greater: |
| 7784 | case OO_LessEqual: |
| 7785 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7786 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7787 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7788 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7789 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7790 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7791 | case OO_Caret: |
| 7792 | case OO_Pipe: |
| 7793 | case OO_LessLess: |
| 7794 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7795 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7796 | break; |
| 7797 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7798 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7799 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7800 | // C++ [over.match.oper]p3: |
| 7801 | // -- For the operator ',', the unary operator '&', or the |
| 7802 | // operator '->', the built-in candidates set is empty. |
| 7803 | break; |
| 7804 | |
| 7805 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7806 | break; |
| 7807 | |
| 7808 | case OO_Tilde: |
| 7809 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7810 | break; |
| 7811 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7812 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7813 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7814 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7815 | |
| 7816 | case OO_PlusEqual: |
| 7817 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7818 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7819 | // Fall through. |
| 7820 | |
| 7821 | case OO_StarEqual: |
| 7822 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7823 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7824 | break; |
| 7825 | |
| 7826 | case OO_PercentEqual: |
| 7827 | case OO_LessLessEqual: |
| 7828 | case OO_GreaterGreaterEqual: |
| 7829 | case OO_AmpEqual: |
| 7830 | case OO_CaretEqual: |
| 7831 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7832 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7833 | break; |
| 7834 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7835 | case OO_Exclaim: |
| 7836 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7837 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7838 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7839 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7840 | case OO_PipePipe: |
| 7841 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7842 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7843 | |
| 7844 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7845 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7846 | break; |
| 7847 | |
| 7848 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7849 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7850 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7851 | |
| 7852 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7853 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7854 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7855 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7856 | } |
| 7857 | } |
| 7858 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7859 | /// \brief Add function candidates found via argument-dependent lookup |
| 7860 | /// to the set of overloading candidates. |
| 7861 | /// |
| 7862 | /// This routine performs argument-dependent name lookup based on the |
| 7863 | /// given function name (which may also be an operator name) and adds |
| 7864 | /// all of the overload candidates found by ADL to the overload |
| 7865 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7866 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7867 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7868 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7869 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7870 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7871 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7872 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7873 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7874 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7875 | // FIXME: This approach for uniquing ADL results (and removing |
| 7876 | // redundant candidates from the set) relies on pointer-equality, |
| 7877 | // which means we need to key off the canonical decl. However, |
| 7878 | // always going back to the canonical decl might not get us the |
| 7879 | // right set of default arguments. What default arguments are |
| 7880 | // we supposed to consider on ADL candidates, anyway? |
| 7881 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7882 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7883 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7884 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7885 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7886 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7887 | CandEnd = CandidateSet.end(); |
| 7888 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7889 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7890 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7891 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7892 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7893 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7894 | |
| 7895 | // For each of the ADL candidates we found, add it to the overload |
| 7896 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7897 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7898 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7899 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7900 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7901 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7902 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7903 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7904 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7905 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7906 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7907 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7908 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7909 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7910 | } |
| 7911 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7912 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7913 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7914 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7915 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7916 | const OverloadCandidate &Cand1, |
| 7917 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7918 | SourceLocation Loc, |
| 7919 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7920 | // Define viable functions to be better candidates than non-viable |
| 7921 | // functions. |
| 7922 | if (!Cand2.Viable) |
| 7923 | return Cand1.Viable; |
| 7924 | else if (!Cand1.Viable) |
| 7925 | return false; |
| 7926 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7927 | // C++ [over.match.best]p1: |
| 7928 | // |
| 7929 | // -- if F is a static member function, ICS1(F) is defined such |
| 7930 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7931 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7932 | // better nor worse than ICS1(F). |
| 7933 | unsigned StartArg = 0; |
| 7934 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7935 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7936 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7937 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7938 | // A viable function F1 is defined to be a better function than another |
| 7939 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7940 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7941 | unsigned NumArgs = Cand1.NumConversions; |
| 7942 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7943 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7944 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7945 | switch (CompareImplicitConversionSequences(S, |
| 7946 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7947 | Cand2.Conversions[ArgIdx])) { |
| 7948 | case ImplicitConversionSequence::Better: |
| 7949 | // Cand1 has a better conversion sequence. |
| 7950 | HasBetterConversion = true; |
| 7951 | break; |
| 7952 | |
| 7953 | case ImplicitConversionSequence::Worse: |
| 7954 | // Cand1 can't be better than Cand2. |
| 7955 | return false; |
| 7956 | |
| 7957 | case ImplicitConversionSequence::Indistinguishable: |
| 7958 | // Do nothing. |
| 7959 | break; |
| 7960 | } |
| 7961 | } |
| 7962 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7963 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7964 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7965 | if (HasBetterConversion) |
| 7966 | return true; |
| 7967 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7968 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7969 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7970 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7971 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7972 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7973 | |
| 7974 | // -- F1 and F2 are function template specializations, and the function |
| 7975 | // template for F1 is more specialized than the template for F2 |
| 7976 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7977 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7978 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7979 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7980 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7981 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7982 | Cand2.Function->getPrimaryTemplate(), |
| 7983 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7984 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7985 | : TPOC_Call, |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 7986 | Cand1.ExplicitCallArguments, |
| 7987 | Cand2.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7988 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7989 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7990 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7991 | // -- the context is an initialization by user-defined conversion |
| 7992 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7993 | // from the return type of F1 to the destination type (i.e., |
| 7994 | // the type of the entity being initialized) is a better |
| 7995 | // conversion sequence than the standard conversion sequence |
| 7996 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7997 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7998 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7999 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 8000 | // First check whether we prefer one of the conversion functions over the |
| 8001 | // other. This only distinguishes the results in non-standard, extension |
| 8002 | // cases such as the conversion from a lambda closure type to a function |
| 8003 | // pointer or block. |
| 8004 | ImplicitConversionSequence::CompareKind FuncResult |
| 8005 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 8006 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 8007 | return FuncResult; |
| 8008 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8009 | switch (CompareStandardConversionSequences(S, |
| 8010 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8011 | Cand2.FinalConversion)) { |
| 8012 | case ImplicitConversionSequence::Better: |
| 8013 | // Cand1 has a better conversion sequence. |
| 8014 | return true; |
| 8015 | |
| 8016 | case ImplicitConversionSequence::Worse: |
| 8017 | // Cand1 can't be better than Cand2. |
| 8018 | return false; |
| 8019 | |
| 8020 | case ImplicitConversionSequence::Indistinguishable: |
| 8021 | // Do nothing |
| 8022 | break; |
| 8023 | } |
| 8024 | } |
| 8025 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8026 | return false; |
| 8027 | } |
| 8028 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8029 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8030 | /// within an overload candidate set. |
| 8031 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8032 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8033 | /// which overload resolution occurs. |
| 8034 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8035 | /// \param Best If overload resolution was successful or found a deleted |
| 8036 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8037 | /// |
| 8038 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8039 | OverloadingResult |
| 8040 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8041 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8042 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8043 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8044 | Best = end(); |
| 8045 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8046 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8047 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8048 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8049 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8053 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8054 | return OR_No_Viable_Function; |
| 8055 | |
| 8056 | // Make sure that this function is better than every other viable |
| 8057 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8058 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8059 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8060 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8061 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8062 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8063 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8064 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8065 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8067 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8068 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8069 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8070 | (Best->Function->isDeleted() || |
| 8071 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8072 | return OR_Deleted; |
| 8073 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8074 | return OR_Success; |
| 8075 | } |
| 8076 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8077 | namespace { |
| 8078 | |
| 8079 | enum OverloadCandidateKind { |
| 8080 | oc_function, |
| 8081 | oc_method, |
| 8082 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8083 | oc_function_template, |
| 8084 | oc_method_template, |
| 8085 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8086 | oc_implicit_default_constructor, |
| 8087 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8088 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8089 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8090 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8091 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8092 | }; |
| 8093 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8094 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8095 | FunctionDecl *Fn, |
| 8096 | std::string &Description) { |
| 8097 | bool isTemplate = false; |
| 8098 | |
| 8099 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8100 | isTemplate = true; |
| 8101 | Description = S.getTemplateArgumentBindingsText( |
| 8102 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8103 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8104 | |
| 8105 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8106 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8107 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8108 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8109 | if (Ctor->getInheritedConstructor()) |
| 8110 | return oc_implicit_inherited_constructor; |
| 8111 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8112 | if (Ctor->isDefaultConstructor()) |
| 8113 | return oc_implicit_default_constructor; |
| 8114 | |
| 8115 | if (Ctor->isMoveConstructor()) |
| 8116 | return oc_implicit_move_constructor; |
| 8117 | |
| 8118 | assert(Ctor->isCopyConstructor() && |
| 8119 | "unexpected sort of implicit constructor"); |
| 8120 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8121 | } |
| 8122 | |
| 8123 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8124 | // This actually gets spelled 'candidate function' for now, but |
| 8125 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8126 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8127 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8128 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8129 | if (Meth->isMoveAssignmentOperator()) |
| 8130 | return oc_implicit_move_assignment; |
| 8131 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8132 | if (Meth->isCopyAssignmentOperator()) |
| 8133 | return oc_implicit_copy_assignment; |
| 8134 | |
| 8135 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8136 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8139 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8140 | } |
| 8141 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8142 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8143 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8144 | if (!Ctor) return; |
| 8145 | |
| 8146 | Ctor = Ctor->getInheritedConstructor(); |
| 8147 | if (!Ctor) return; |
| 8148 | |
| 8149 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8150 | } |
| 8151 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8152 | } // end anonymous namespace |
| 8153 | |
| 8154 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8155 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8156 | std::string FnDesc; |
| 8157 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8158 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8159 | << (unsigned) K << FnDesc; |
| 8160 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8161 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8162 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8163 | } |
| 8164 | |
Nick Lewycky | 199e370 | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 8165 | // Notes the location of all overload candidates designated through |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8166 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8167 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8168 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8169 | |
| 8170 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8171 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8172 | |
| 8173 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8174 | IEnd = OvlExpr->decls_end(); |
| 8175 | I != IEnd; ++I) { |
| 8176 | if (FunctionTemplateDecl *FunTmpl = |
| 8177 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8178 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8179 | } else if (FunctionDecl *Fun |
| 8180 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8181 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8182 | } |
| 8183 | } |
| 8184 | } |
| 8185 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8186 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8187 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8188 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8189 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8190 | Sema &S, |
| 8191 | SourceLocation CaretLoc, |
| 8192 | const PartialDiagnostic &PDiag) const { |
| 8193 | S.Diag(CaretLoc, PDiag) |
| 8194 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8195 | // FIXME: The note limiting machinery is borrowed from |
| 8196 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8197 | // refactoring here. |
| 8198 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8199 | unsigned CandsShown = 0; |
| 8200 | AmbiguousConversionSequence::const_iterator I, E; |
| 8201 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8202 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8203 | break; |
| 8204 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8205 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8206 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8207 | if (I != E) |
| 8208 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8209 | } |
| 8210 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8211 | namespace { |
| 8212 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8213 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8214 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8215 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8216 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8217 | FunctionDecl *Fn = Cand->Function; |
| 8218 | |
| 8219 | // There's a conversion slot for the object argument if this is a |
| 8220 | // non-constructor method. Note that 'I' corresponds the |
| 8221 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8222 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8223 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8224 | if (I == 0) |
| 8225 | isObjectArgument = true; |
| 8226 | else |
| 8227 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8228 | } |
| 8229 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8230 | std::string FnDesc; |
| 8231 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8232 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8233 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8234 | QualType FromTy = Conv.Bad.getFromType(); |
| 8235 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8236 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8237 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8238 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8239 | Expr *E = FromExpr->IgnoreParens(); |
| 8240 | if (isa<UnaryOperator>(E)) |
| 8241 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8242 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8243 | |
| 8244 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8245 | << (unsigned) FnKind << FnDesc |
| 8246 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8247 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8248 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8249 | return; |
| 8250 | } |
| 8251 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8252 | // Do some hand-waving analysis to see if the non-viability is due |
| 8253 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8254 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8255 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8256 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8257 | CToTy = RT->getPointeeType(); |
| 8258 | else { |
| 8259 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8260 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8261 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8262 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8263 | } |
| 8264 | |
| 8265 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8266 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8267 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8268 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8269 | |
| 8270 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8271 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8272 | << (unsigned) FnKind << FnDesc |
| 8273 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8274 | << FromTy |
| 8275 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8276 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8277 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8278 | return; |
| 8279 | } |
| 8280 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8281 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8282 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8283 | << (unsigned) FnKind << FnDesc |
| 8284 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8285 | << FromTy |
| 8286 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8287 | << (unsigned) isObjectArgument << I+1; |
| 8288 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8289 | return; |
| 8290 | } |
| 8291 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8292 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8293 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8294 | << (unsigned) FnKind << FnDesc |
| 8295 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8296 | << FromTy |
| 8297 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8298 | << (unsigned) isObjectArgument << I+1; |
| 8299 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8300 | return; |
| 8301 | } |
| 8302 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8303 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8304 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8305 | |
| 8306 | if (isObjectArgument) { |
| 8307 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8308 | << (unsigned) FnKind << FnDesc |
| 8309 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8310 | << FromTy << (CVR - 1); |
| 8311 | } else { |
| 8312 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8313 | << (unsigned) FnKind << FnDesc |
| 8314 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8315 | << FromTy << (CVR - 1) << I+1; |
| 8316 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8317 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8318 | return; |
| 8319 | } |
| 8320 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8321 | // Special diagnostic for failure to convert an initializer list, since |
| 8322 | // telling the user that it has type void is not useful. |
| 8323 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8324 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8325 | << (unsigned) FnKind << FnDesc |
| 8326 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8327 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8328 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8329 | return; |
| 8330 | } |
| 8331 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8332 | // Diagnose references or pointers to incomplete types differently, |
| 8333 | // since it's far from impossible that the incompleteness triggered |
| 8334 | // the failure. |
| 8335 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8336 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8337 | TempFromTy = PTy->getPointeeType(); |
| 8338 | if (TempFromTy->isIncompleteType()) { |
| 8339 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8340 | << (unsigned) FnKind << FnDesc |
| 8341 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8342 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8343 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8344 | return; |
| 8345 | } |
| 8346 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8347 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8348 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8349 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8350 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8351 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8352 | FromPtrTy->getPointeeType()) && |
| 8353 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8354 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8355 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8356 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8357 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8358 | } |
| 8359 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8360 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8361 | if (const ObjCObjectPointerType *ToPtrTy |
| 8362 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8363 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8364 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8365 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8366 | FromPtrTy->getPointeeType()) && |
| 8367 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8368 | BaseToDerivedConversion = 2; |
| 8369 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8370 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8371 | !FromTy->isIncompleteType() && |
| 8372 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8373 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8374 | BaseToDerivedConversion = 3; |
| 8375 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8376 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8377 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8378 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8379 | << (unsigned) FnKind << FnDesc |
| 8380 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8381 | << (unsigned) isObjectArgument << I + 1; |
| 8382 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8383 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8384 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8385 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8386 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8387 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8388 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8389 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8390 | << (unsigned) FnKind << FnDesc |
| 8391 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8392 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8393 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8394 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8395 | return; |
| 8396 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8397 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8398 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8399 | isa<PointerType>(CToTy)) { |
| 8400 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8401 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8402 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8403 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8404 | << (unsigned) FnKind << FnDesc |
| 8405 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8406 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8407 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8408 | return; |
| 8409 | } |
| 8410 | } |
| 8411 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8412 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8413 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8414 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8415 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8416 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8417 | << (unsigned) (Cand->Fix.Kind); |
| 8418 | |
| 8419 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8420 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8421 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8422 | FDiag << *HI; |
| 8423 | S.Diag(Fn->getLocation(), FDiag); |
| 8424 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8425 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8426 | } |
| 8427 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8428 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8429 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8430 | /// over a candidate in any candidate set. |
| 8431 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8432 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8433 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8434 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8435 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8436 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8437 | // have an arity mismatch when in fact it looks like we have the |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8438 | // right number of arguments, because only overloaded operators have |
| 8439 | // the weird behavior of overloading member and non-member functions. |
| 8440 | // Just don't report anything. |
| 8441 | if (Fn->isInvalidDecl() && |
| 8442 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8443 | return true; |
| 8444 | |
| 8445 | if (NumArgs < MinParams) { |
| 8446 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8447 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8448 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8449 | } else { |
| 8450 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8451 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8452 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8453 | } |
| 8454 | |
| 8455 | return false; |
| 8456 | } |
| 8457 | |
| 8458 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8459 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8460 | assert(isa<FunctionDecl>(D) && |
| 8461 | "The templated declaration should at least be a function" |
| 8462 | " when diagnosing bad template argument deduction due to too many" |
| 8463 | " or too few arguments"); |
| 8464 | |
| 8465 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8466 | |
| 8467 | // TODO: treat calls to a missing default constructor as a special case |
| 8468 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8469 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8470 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8471 | // at least / at most / exactly |
| 8472 | unsigned mode, modeCount; |
| 8473 | if (NumFormalArgs < MinParams) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8474 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8475 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8476 | mode = 0; // "at least" |
| 8477 | else |
| 8478 | mode = 2; // "exactly" |
| 8479 | modeCount = MinParams; |
| 8480 | } else { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8481 | if (MinParams != FnTy->getNumArgs()) |
| 8482 | mode = 1; // "at most" |
| 8483 | else |
| 8484 | mode = 2; // "exactly" |
| 8485 | modeCount = FnTy->getNumArgs(); |
| 8486 | } |
| 8487 | |
| 8488 | std::string Description; |
| 8489 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8490 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8491 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8492 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8493 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8494 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8495 | else |
| 8496 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8497 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8498 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8499 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8500 | } |
| 8501 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8502 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8503 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8504 | unsigned NumFormalArgs) { |
| 8505 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8506 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8507 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8508 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8509 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8510 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8511 | return FD->getDescribedFunctionTemplate(); |
| 8512 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8513 | return RD->getDescribedClassTemplate(); |
| 8514 | |
| 8515 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8516 | " for bad deduction diagnosis"); |
| 8517 | } |
| 8518 | |
| 8519 | /// Diagnose a failed template-argument deduction. |
| 8520 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8521 | DeductionFailureInfo &DeductionFailure, |
| 8522 | unsigned NumArgs) { |
| 8523 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8524 | NamedDecl *ParamD; |
| 8525 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8526 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8527 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8528 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8529 | case Sema::TDK_Success: |
| 8530 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8531 | |
| 8532 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8533 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8534 | S.Diag(Templated->getLocation(), |
| 8535 | diag::note_ovl_candidate_incomplete_deduction) |
| 8536 | << ParamD->getDeclName(); |
| 8537 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8538 | return; |
| 8539 | } |
| 8540 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8541 | case Sema::TDK_Underqualified: { |
| 8542 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8543 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8544 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8545 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8546 | |
| 8547 | // Param will have been canonicalized, but it should just be a |
| 8548 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8549 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8550 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8551 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8552 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8553 | |
| 8554 | // Arg has also been canonicalized, but there's nothing we can do |
| 8555 | // about that. It also doesn't matter as much, because it won't |
| 8556 | // have any template parameters in it (because deduction isn't |
| 8557 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8558 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8559 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8560 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8561 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8562 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8563 | return; |
| 8564 | } |
| 8565 | |
| 8566 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8567 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8568 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8569 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8570 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8571 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8572 | which = 1; |
| 8573 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8574 | which = 2; |
| 8575 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8576 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8577 | S.Diag(Templated->getLocation(), |
| 8578 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8579 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8580 | << *DeductionFailure.getSecondArg(); |
| 8581 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8582 | return; |
| 8583 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8584 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8585 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8586 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8587 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8588 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8589 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8590 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8591 | else { |
| 8592 | int index = 0; |
| 8593 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8594 | index = TTP->getIndex(); |
| 8595 | else if (NonTypeTemplateParmDecl *NTTP |
| 8596 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8597 | index = NTTP->getIndex(); |
| 8598 | else |
| 8599 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8600 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8601 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8602 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8603 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8604 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8605 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8606 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8607 | case Sema::TDK_TooManyArguments: |
| 8608 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8609 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8610 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8611 | |
| 8612 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8613 | S.Diag(Templated->getLocation(), |
| 8614 | diag::note_ovl_candidate_instantiation_depth); |
| 8615 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8616 | return; |
| 8617 | |
| 8618 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8619 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8620 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8621 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8622 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8623 | TemplateArgString = " "; |
| 8624 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8625 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8626 | } |
| 8627 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8628 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8629 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8630 | if (PDiag && PDiag->second.getDiagID() == |
| 8631 | diag::err_typename_nested_not_found_enable_if) { |
| 8632 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8633 | // name of the enable_if template. These are both present in PDiag. |
| 8634 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8635 | << "'enable_if'" << TemplateArgString; |
| 8636 | return; |
| 8637 | } |
| 8638 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8639 | // Format the SFINAE diagnostic into the argument string. |
| 8640 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8641 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8642 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8643 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8644 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8645 | SFINAEArgString = ": "; |
| 8646 | R = SourceRange(PDiag->first, PDiag->first); |
| 8647 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8648 | } |
| 8649 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8650 | S.Diag(Templated->getLocation(), |
| 8651 | diag::note_ovl_candidate_substitution_failure) |
| 8652 | << TemplateArgString << SFINAEArgString << R; |
| 8653 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8654 | return; |
| 8655 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8656 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8657 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8658 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8659 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8660 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8661 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8662 | return; |
| 8663 | } |
| 8664 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8665 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8666 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8667 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8668 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8669 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8670 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8671 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8672 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8673 | if (FirstTN.getKind() == TemplateName::Template && |
| 8674 | SecondTN.getKind() == TemplateName::Template) { |
| 8675 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8676 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8677 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8678 | // the same. This particular case is a bit difficult since: |
| 8679 | // 1) It is passed as a string to the diagnostic printer. |
| 8680 | // 2) The diagnostic printer only attempts to find a better |
| 8681 | // name for types, not decls. |
| 8682 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8683 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8684 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8685 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8686 | return; |
| 8687 | } |
| 8688 | } |
| 8689 | } |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 8690 | // FIXME: For generic lambda parameters, check if the function is a lambda |
| 8691 | // call operator, and if so, emit a prettier and more informative |
| 8692 | // diagnostic that mentions 'auto' and lambda in addition to |
| 8693 | // (or instead of?) the canonical template type parameters. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8694 | S.Diag(Templated->getLocation(), |
| 8695 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8696 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8697 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8698 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8699 | // TODO: diagnose these individually, then kill off |
| 8700 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8701 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8702 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8703 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8704 | return; |
| 8705 | } |
| 8706 | } |
| 8707 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8708 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8709 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8710 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8711 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8712 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8713 | return; |
| 8714 | } |
| 8715 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8716 | Cand->DeductionFailure, NumArgs); |
| 8717 | } |
| 8718 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8719 | /// CUDA: diagnose an invalid call across targets. |
| 8720 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8721 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8722 | FunctionDecl *Callee = Cand->Function; |
| 8723 | |
| 8724 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8725 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8726 | |
| 8727 | std::string FnDesc; |
| 8728 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8729 | |
| 8730 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8731 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8732 | } |
| 8733 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8734 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8735 | /// already generated a primary error at the call site. |
| 8736 | /// |
| 8737 | /// It really does need to be a single diagnostic with its caret |
| 8738 | /// pointed at the candidate declaration. Yes, this creates some |
| 8739 | /// major challenges of technical writing. Yes, this makes pointing |
| 8740 | /// out problems with specific arguments quite awkward. It's still |
| 8741 | /// better than generating twenty screens of text for every failed |
| 8742 | /// overload. |
| 8743 | /// |
| 8744 | /// It would be great to be able to express per-candidate problems |
| 8745 | /// more richly for those diagnostic clients that cared, but we'd |
| 8746 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8747 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8748 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8749 | FunctionDecl *Fn = Cand->Function; |
| 8750 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8751 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8752 | if (Cand->Viable && (Fn->isDeleted() || |
| 8753 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8754 | std::string FnDesc; |
| 8755 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8756 | |
| 8757 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8758 | << FnKind << FnDesc |
| 8759 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8760 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8761 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8762 | } |
| 8763 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8764 | // We don't really have anything else to say about viable candidates. |
| 8765 | if (Cand->Viable) { |
| 8766 | S.NoteOverloadCandidate(Fn); |
| 8767 | return; |
| 8768 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8769 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8770 | switch (Cand->FailureKind) { |
| 8771 | case ovl_fail_too_many_arguments: |
| 8772 | case ovl_fail_too_few_arguments: |
| 8773 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8774 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8775 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8776 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8777 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8778 | case ovl_fail_trivial_conversion: |
| 8779 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8780 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8781 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8782 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8783 | case ovl_fail_bad_conversion: { |
| 8784 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8785 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8786 | if (Cand->Conversions[I].isBad()) |
| 8787 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8788 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8789 | // FIXME: this currently happens when we're called from SemaInit |
| 8790 | // when user-conversion overload fails. Figure out how to handle |
| 8791 | // those conditions and diagnose them well. |
| 8792 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8793 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8794 | |
| 8795 | case ovl_fail_bad_target: |
| 8796 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8797 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8798 | } |
| 8799 | |
| 8800 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8801 | // Desugar the type of the surrogate down to a function type, |
| 8802 | // retaining as many typedefs as possible while still showing |
| 8803 | // the function type (and, therefore, its parameter types). |
| 8804 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8805 | bool isLValueReference = false; |
| 8806 | bool isRValueReference = false; |
| 8807 | bool isPointer = false; |
| 8808 | if (const LValueReferenceType *FnTypeRef = |
| 8809 | FnType->getAs<LValueReferenceType>()) { |
| 8810 | FnType = FnTypeRef->getPointeeType(); |
| 8811 | isLValueReference = true; |
| 8812 | } else if (const RValueReferenceType *FnTypeRef = |
| 8813 | FnType->getAs<RValueReferenceType>()) { |
| 8814 | FnType = FnTypeRef->getPointeeType(); |
| 8815 | isRValueReference = true; |
| 8816 | } |
| 8817 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8818 | FnType = FnTypePtr->getPointeeType(); |
| 8819 | isPointer = true; |
| 8820 | } |
| 8821 | // Desugar down to a function type. |
| 8822 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8823 | // Reconstruct the pointer/reference as appropriate. |
| 8824 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8825 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8826 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8827 | |
| 8828 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8829 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8830 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8831 | } |
| 8832 | |
| 8833 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8834 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8835 | SourceLocation OpLoc, |
| 8836 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8837 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8838 | std::string TypeStr("operator"); |
| 8839 | TypeStr += Opc; |
| 8840 | TypeStr += "("; |
| 8841 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8842 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8843 | TypeStr += ")"; |
| 8844 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8845 | } else { |
| 8846 | TypeStr += ", "; |
| 8847 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8848 | TypeStr += ")"; |
| 8849 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8850 | } |
| 8851 | } |
| 8852 | |
| 8853 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8854 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8855 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8856 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8857 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8858 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8859 | if (!ICS.isAmbiguous()) continue; |
| 8860 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8861 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8862 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8863 | } |
| 8864 | } |
| 8865 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8866 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8867 | if (Cand->Function) |
| 8868 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8869 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8870 | return Cand->Surrogate->getLocation(); |
| 8871 | return SourceLocation(); |
| 8872 | } |
| 8873 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8874 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8875 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8876 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8877 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8878 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8879 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8880 | case Sema::TDK_Incomplete: |
| 8881 | return 1; |
| 8882 | |
| 8883 | case Sema::TDK_Underqualified: |
| 8884 | case Sema::TDK_Inconsistent: |
| 8885 | return 2; |
| 8886 | |
| 8887 | case Sema::TDK_SubstitutionFailure: |
| 8888 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8889 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8890 | return 3; |
| 8891 | |
| 8892 | case Sema::TDK_InstantiationDepth: |
| 8893 | case Sema::TDK_FailedOverloadResolution: |
| 8894 | return 4; |
| 8895 | |
| 8896 | case Sema::TDK_InvalidExplicitArguments: |
| 8897 | return 5; |
| 8898 | |
| 8899 | case Sema::TDK_TooManyArguments: |
| 8900 | case Sema::TDK_TooFewArguments: |
| 8901 | return 6; |
| 8902 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8903 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8904 | } |
| 8905 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8906 | struct CompareOverloadCandidatesForDisplay { |
| 8907 | Sema &S; |
| 8908 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8909 | |
| 8910 | bool operator()(const OverloadCandidate *L, |
| 8911 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8912 | // Fast-path this check. |
| 8913 | if (L == R) return false; |
| 8914 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8915 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8916 | if (L->Viable) { |
| 8917 | if (!R->Viable) return true; |
| 8918 | |
| 8919 | // TODO: introduce a tri-valued comparison for overload |
| 8920 | // candidates. Would be more worthwhile if we had a sort |
| 8921 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8922 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8923 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8924 | } else if (R->Viable) |
| 8925 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8926 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8927 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8928 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8929 | // Criteria by which we can sort non-viable candidates: |
| 8930 | if (!L->Viable) { |
| 8931 | // 1. Arity mismatches come after other candidates. |
| 8932 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8933 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8934 | return false; |
| 8935 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8936 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8937 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8938 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8939 | // 2. Bad conversions come first and are ordered by the number |
| 8940 | // of bad conversions and quality of good conversions. |
| 8941 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8942 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8943 | return true; |
| 8944 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8945 | // The conversion that can be fixed with a smaller number of changes, |
| 8946 | // comes first. |
| 8947 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8948 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8949 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8950 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8951 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8952 | if (numLFixes < numRFixes) |
| 8953 | return true; |
| 8954 | else |
| 8955 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8956 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8957 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8958 | // If there's any ordering between the defined conversions... |
| 8959 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8960 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8961 | |
| 8962 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8963 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8964 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8965 | switch (CompareImplicitConversionSequences(S, |
| 8966 | L->Conversions[I], |
| 8967 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8968 | case ImplicitConversionSequence::Better: |
| 8969 | leftBetter++; |
| 8970 | break; |
| 8971 | |
| 8972 | case ImplicitConversionSequence::Worse: |
| 8973 | leftBetter--; |
| 8974 | break; |
| 8975 | |
| 8976 | case ImplicitConversionSequence::Indistinguishable: |
| 8977 | break; |
| 8978 | } |
| 8979 | } |
| 8980 | if (leftBetter > 0) return true; |
| 8981 | if (leftBetter < 0) return false; |
| 8982 | |
| 8983 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8984 | return false; |
| 8985 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8986 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8987 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8988 | return true; |
| 8989 | |
| 8990 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8991 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8992 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8993 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8994 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8995 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8996 | // TODO: others? |
| 8997 | } |
| 8998 | |
| 8999 | // Sort everything else by location. |
| 9000 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9001 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9002 | |
| 9003 | // Put candidates without locations (e.g. builtins) at the end. |
| 9004 | if (LLoc.isInvalid()) return false; |
| 9005 | if (RLoc.isInvalid()) return true; |
| 9006 | |
| 9007 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9008 | } |
| 9009 | }; |
| 9010 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9011 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9012 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9013 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9014 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9015 | assert(!Cand->Viable); |
| 9016 | |
| 9017 | // Don't do anything on failures other than bad conversion. |
| 9018 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9019 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9020 | // We only want the FixIts if all the arguments can be corrected. |
| 9021 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9022 | // Use a implicit copy initialization to check conversion fixes. |
| 9023 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9024 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9025 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9026 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9027 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9028 | while (true) { |
| 9029 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9030 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9031 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9032 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9033 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9034 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9035 | } |
| 9036 | |
| 9037 | if (ConvIdx == ConvCount) |
| 9038 | return; |
| 9039 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9040 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9041 | "remaining conversion is initialized?"); |
| 9042 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9043 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9044 | // operation somehow. |
| 9045 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9046 | |
| 9047 | const FunctionProtoType* Proto; |
| 9048 | unsigned ArgIdx = ConvIdx; |
| 9049 | |
| 9050 | if (Cand->IsSurrogate) { |
| 9051 | QualType ConvType |
| 9052 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9053 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9054 | ConvType = ConvPtrType->getPointeeType(); |
| 9055 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9056 | ArgIdx--; |
| 9057 | } else if (Cand->Function) { |
| 9058 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9059 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9060 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9061 | ArgIdx--; |
| 9062 | } else { |
| 9063 | // Builtin binary operator with a bad first conversion. |
| 9064 | assert(ConvCount <= 3); |
| 9065 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9066 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9067 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9068 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9069 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9070 | /*InOverloadResolution*/ true, |
| 9071 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9072 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9073 | return; |
| 9074 | } |
| 9075 | |
| 9076 | // Fill in the rest of the conversions. |
| 9077 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9078 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9079 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9080 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9081 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9082 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9083 | /*InOverloadResolution=*/true, |
| 9084 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9085 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9086 | // Store the FixIt in the candidate if it exists. |
| 9087 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9088 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9089 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9090 | else |
| 9091 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9092 | } |
| 9093 | } |
| 9094 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9095 | } // end anonymous namespace |
| 9096 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9097 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9098 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9099 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9100 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9101 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9102 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9103 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9104 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9105 | // Sort the candidates by viability and position. Sorting directly would |
| 9106 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9107 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9108 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9109 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9110 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9111 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9112 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9113 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9114 | if (Cand->Function || Cand->IsSurrogate) |
| 9115 | Cands.push_back(Cand); |
| 9116 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9117 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9118 | } |
| 9119 | } |
| 9120 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9121 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9122 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9123 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9124 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9125 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9126 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9127 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9128 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9129 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9130 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9131 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9132 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9133 | // the user with. FIXME: This limit should depend on details of the |
| 9134 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9135 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9136 | break; |
| 9137 | } |
| 9138 | ++CandsShown; |
| 9139 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9140 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9141 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9142 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9143 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9144 | else { |
| 9145 | assert(Cand->Viable && |
| 9146 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9147 | // Generally we only see ambiguities including viable builtin |
| 9148 | // operators if overload resolution got screwed up by an |
| 9149 | // ambiguous user-defined conversion. |
| 9150 | // |
| 9151 | // FIXME: It's quite possible for different conversions to see |
| 9152 | // different ambiguities, though. |
| 9153 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9154 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9155 | ReportedAmbiguousConversions = true; |
| 9156 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9157 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9158 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9159 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9160 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9161 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9162 | |
| 9163 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9164 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9165 | } |
| 9166 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9167 | static SourceLocation |
| 9168 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9169 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9170 | : SourceLocation(); |
| 9171 | } |
| 9172 | |
| 9173 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9174 | Sema &S; |
| 9175 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9176 | |
| 9177 | bool operator()(const TemplateSpecCandidate *L, |
| 9178 | const TemplateSpecCandidate *R) { |
| 9179 | // Fast-path this check. |
| 9180 | if (L == R) |
| 9181 | return false; |
| 9182 | |
| 9183 | // Assuming that both candidates are not matches... |
| 9184 | |
| 9185 | // Sort by the ranking of deduction failures. |
| 9186 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9187 | return RankDeductionFailure(L->DeductionFailure) < |
| 9188 | RankDeductionFailure(R->DeductionFailure); |
| 9189 | |
| 9190 | // Sort everything else by location. |
| 9191 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9192 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9193 | |
| 9194 | // Put candidates without locations (e.g. builtins) at the end. |
| 9195 | if (LLoc.isInvalid()) |
| 9196 | return false; |
| 9197 | if (RLoc.isInvalid()) |
| 9198 | return true; |
| 9199 | |
| 9200 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9201 | } |
| 9202 | }; |
| 9203 | |
| 9204 | /// Diagnose a template argument deduction failure. |
| 9205 | /// We are treating these failures as overload failures due to bad |
| 9206 | /// deductions. |
| 9207 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9208 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9209 | DeductionFailure, /*NumArgs=*/0); |
| 9210 | } |
| 9211 | |
| 9212 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9213 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9214 | i->DeductionFailure.Destroy(); |
| 9215 | } |
| 9216 | } |
| 9217 | |
| 9218 | void TemplateSpecCandidateSet::clear() { |
| 9219 | destroyCandidates(); |
| 9220 | Candidates.clear(); |
| 9221 | } |
| 9222 | |
| 9223 | /// NoteCandidates - When no template specialization match is found, prints |
| 9224 | /// diagnostic messages containing the non-matching specializations that form |
| 9225 | /// the candidate set. |
| 9226 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9227 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9228 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9229 | // Sort the candidates by position (assuming no candidate is a match). |
| 9230 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9231 | // and sort those. |
| 9232 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9233 | Cands.reserve(size()); |
| 9234 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9235 | if (Cand->Specialization) |
| 9236 | Cands.push_back(Cand); |
| 9237 | // Otherwise, this is a non matching builtin candidate. We do not, |
| 9238 | // in general, want to list every possible builtin candidate. |
| 9239 | } |
| 9240 | |
| 9241 | std::sort(Cands.begin(), Cands.end(), |
| 9242 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9243 | |
| 9244 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9245 | // for generalization purposes (?). |
| 9246 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9247 | |
| 9248 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9249 | unsigned CandsShown = 0; |
| 9250 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9251 | TemplateSpecCandidate *Cand = *I; |
| 9252 | |
| 9253 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9254 | // the user with. FIXME: This limit should depend on details of the |
| 9255 | // candidate list. |
| 9256 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9257 | break; |
| 9258 | ++CandsShown; |
| 9259 | |
| 9260 | assert(Cand->Specialization && |
| 9261 | "Non-matching built-in candidates are not added to Cands."); |
| 9262 | Cand->NoteDeductionFailure(S); |
| 9263 | } |
| 9264 | |
| 9265 | if (I != E) |
| 9266 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9267 | } |
| 9268 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9269 | // [PossiblyAFunctionType] --> [Return] |
| 9270 | // NonFunctionType --> NonFunctionType |
| 9271 | // R (A) --> R(A) |
| 9272 | // R (*)(A) --> R (A) |
| 9273 | // R (&)(A) --> R (A) |
| 9274 | // R (S::*)(A) --> R (A) |
| 9275 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9276 | QualType Ret = PossiblyAFunctionType; |
| 9277 | if (const PointerType *ToTypePtr = |
| 9278 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9279 | Ret = ToTypePtr->getPointeeType(); |
| 9280 | else if (const ReferenceType *ToTypeRef = |
| 9281 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9282 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9283 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9284 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9285 | Ret = MemTypePtr->getPointeeType(); |
| 9286 | Ret = |
| 9287 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9288 | return Ret; |
| 9289 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9290 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9291 | // A helper class to help with address of function resolution |
| 9292 | // - allows us to avoid passing around all those ugly parameters |
| 9293 | class AddressOfFunctionResolver |
| 9294 | { |
| 9295 | Sema& S; |
| 9296 | Expr* SourceExpr; |
| 9297 | const QualType& TargetType; |
| 9298 | QualType TargetFunctionType; // Extracted function type from target type |
| 9299 | |
| 9300 | bool Complain; |
| 9301 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9302 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9303 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9304 | bool TargetTypeIsNonStaticMemberFunction; |
| 9305 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9306 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9307 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9308 | OverloadExpr::FindResult OvlExprInfo; |
| 9309 | OverloadExpr *OvlExpr; |
| 9310 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9311 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9312 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9313 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9314 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9315 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9316 | const QualType &TargetType, bool Complain) |
| 9317 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9318 | Complain(Complain), Context(S.getASTContext()), |
| 9319 | TargetTypeIsNonStaticMemberFunction( |
| 9320 | !!TargetType->getAs<MemberPointerType>()), |
| 9321 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9322 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9323 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9324 | OvlExpr(OvlExprInfo.Expression), |
| 9325 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9326 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9327 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9328 | if (TargetFunctionType->isFunctionType()) { |
| 9329 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9330 | if (!UME->isImplicitAccess() && |
| 9331 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9332 | StaticMemberFunctionFromBoundPointer = true; |
| 9333 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9334 | DeclAccessPair dap; |
| 9335 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9336 | OvlExpr, false, &dap)) { |
| 9337 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9338 | if (!Method->isStatic()) { |
| 9339 | // If the target type is a non-function type and the function found |
| 9340 | // is a non-static member function, pretend as if that was the |
| 9341 | // target, it's the only possible type to end up with. |
| 9342 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9343 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9344 | // And skip adding the function if its not in the proper form. |
| 9345 | // We'll diagnose this due to an empty set of functions. |
| 9346 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9347 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9348 | } |
| 9349 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9350 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9351 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9352 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9353 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9354 | |
| 9355 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9356 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9357 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9358 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9359 | // C++ [over.over]p4: |
| 9360 | // If more than one function is selected, [...] |
| 9361 | if (Matches.size() > 1) { |
| 9362 | if (FoundNonTemplateFunction) |
| 9363 | EliminateAllTemplateMatches(); |
| 9364 | else |
| 9365 | EliminateAllExceptMostSpecializedTemplate(); |
| 9366 | } |
| 9367 | } |
| 9368 | } |
| 9369 | |
| 9370 | private: |
| 9371 | bool isTargetTypeAFunction() const { |
| 9372 | return TargetFunctionType->isFunctionType(); |
| 9373 | } |
| 9374 | |
| 9375 | // [ToType] [Return] |
| 9376 | |
| 9377 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9378 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9379 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9380 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9381 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9382 | } |
| 9383 | |
| 9384 | // return true if any matching specializations were found |
| 9385 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9386 | const DeclAccessPair& CurAccessFunPair) { |
| 9387 | if (CXXMethodDecl *Method |
| 9388 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9389 | // Skip non-static function templates when converting to pointer, and |
| 9390 | // static when converting to member pointer. |
| 9391 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9392 | return false; |
| 9393 | } |
| 9394 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9395 | return false; |
| 9396 | |
| 9397 | // C++ [over.over]p2: |
| 9398 | // If the name is a function template, template argument deduction is |
| 9399 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9400 | // resulting template argument list is used to generate a single |
| 9401 | // function template specialization, which is added to the set of |
| 9402 | // overloaded functions considered. |
| 9403 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9404 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9405 | if (Sema::TemplateDeductionResult Result |
| 9406 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9407 | &OvlExplicitTemplateArgs, |
| 9408 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9409 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9410 | // Make a note of the failed deduction for diagnostics. |
| 9411 | FailedCandidates.addCandidate() |
| 9412 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9413 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9414 | return false; |
| 9415 | } |
| 9416 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9417 | // Template argument deduction ensures that we have an exact match or |
| 9418 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9419 | // This function template specicalization works. |
| 9420 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9421 | assert(S.isSameOrCompatibleFunctionType( |
| 9422 | Context.getCanonicalType(Specialization->getType()), |
| 9423 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9424 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9425 | return true; |
| 9426 | } |
| 9427 | |
| 9428 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9429 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9430 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9431 | // Skip non-static functions when converting to pointer, and static |
| 9432 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9433 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9434 | return false; |
| 9435 | } |
| 9436 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9437 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9438 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9439 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9440 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9441 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9442 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9443 | return false; |
| 9444 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9445 | // If any candidate has a placeholder return type, trigger its deduction |
| 9446 | // now. |
| 9447 | if (S.getLangOpts().CPlusPlus1y && |
| 9448 | FunDecl->getResultType()->isUndeducedType() && |
| 9449 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9450 | return false; |
| 9451 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9452 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9453 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9454 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9455 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9456 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9457 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9458 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9459 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9460 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9462 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9463 | |
| 9464 | return false; |
| 9465 | } |
| 9466 | |
| 9467 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9468 | bool Ret = false; |
| 9469 | |
| 9470 | // If the overload expression doesn't have the form of a pointer to |
| 9471 | // member, don't try to convert it to a pointer-to-member type. |
| 9472 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9473 | return false; |
| 9474 | |
| 9475 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9476 | E = OvlExpr->decls_end(); |
| 9477 | I != E; ++I) { |
| 9478 | // Look through any using declarations to find the underlying function. |
| 9479 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9480 | |
| 9481 | // C++ [over.over]p3: |
| 9482 | // Non-member functions and static member functions match |
| 9483 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9484 | // Nonstatic member functions match targets of |
| 9485 | // type "pointer-to-member-function." |
| 9486 | // Note that according to DR 247, the containing class does not matter. |
| 9487 | if (FunctionTemplateDecl *FunctionTemplate |
| 9488 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9489 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9490 | Ret = true; |
| 9491 | } |
| 9492 | // If we have explicit template arguments supplied, skip non-templates. |
| 9493 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9494 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9495 | Ret = true; |
| 9496 | } |
| 9497 | assert(Ret || Matches.empty()); |
| 9498 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9501 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9502 | // [...] and any given function template specialization F1 is |
| 9503 | // eliminated if the set contains a second function template |
| 9504 | // specialization whose function template is more specialized |
| 9505 | // than the function template of F1 according to the partial |
| 9506 | // ordering rules of 14.5.5.2. |
| 9507 | |
| 9508 | // The algorithm specified above is quadratic. We instead use a |
| 9509 | // two-pass algorithm (similar to the one used to identify the |
| 9510 | // best viable function in an overload set) that identifies the |
| 9511 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9512 | |
| 9513 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9514 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9515 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9516 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9517 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9518 | // here, since the no_viable diagnostic has index 0. |
| 9519 | UnresolvedSetIterator Result = S.getMostSpecialized( |
Richard Smith | 4ad09e6 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 9520 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9521 | SourceExpr->getLocStart(), S.PDiag(), |
| 9522 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9523 | .second->getDeclName(), |
| 9524 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9525 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9526 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9527 | if (Result != MatchesCopy.end()) { |
| 9528 | // Make it the first and only element |
| 9529 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9530 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9531 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9532 | } |
| 9533 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9534 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9535 | void EliminateAllTemplateMatches() { |
| 9536 | // [...] any function template specializations in the set are |
| 9537 | // eliminated if the set also contains a non-template function, [...] |
| 9538 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9539 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9540 | ++I; |
| 9541 | else { |
| 9542 | Matches[I] = Matches[--N]; |
| 9543 | Matches.set_size(N); |
| 9544 | } |
| 9545 | } |
| 9546 | } |
| 9547 | |
| 9548 | public: |
| 9549 | void ComplainNoMatchesFound() const { |
| 9550 | assert(Matches.empty()); |
| 9551 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9552 | << OvlExpr->getName() << TargetFunctionType |
| 9553 | << OvlExpr->getSourceRange(); |
Richard Smith | 72a36a1 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 9554 | if (FailedCandidates.empty()) |
| 9555 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 9556 | else { |
| 9557 | // We have some deduction failure messages. Use them to diagnose |
| 9558 | // the function templates, and diagnose the non-template candidates |
| 9559 | // normally. |
| 9560 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9561 | IEnd = OvlExpr->decls_end(); |
| 9562 | I != IEnd; ++I) |
| 9563 | if (FunctionDecl *Fun = |
| 9564 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 9565 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 9566 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 9567 | } |
| 9568 | } |
| 9569 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9570 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9571 | return TargetTypeIsNonStaticMemberFunction && |
| 9572 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9573 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9574 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9575 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9576 | // TODO: Should we condition this on whether any functions might |
| 9577 | // have matched, or is it more appropriate to do that in callers? |
| 9578 | // TODO: a fixit wouldn't hurt. |
| 9579 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9580 | << TargetType << OvlExpr->getSourceRange(); |
| 9581 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9582 | |
| 9583 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9584 | return StaticMemberFunctionFromBoundPointer; |
| 9585 | } |
| 9586 | |
| 9587 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9588 | S.Diag(OvlExpr->getLocStart(), |
| 9589 | diag::err_invalid_form_pointer_member_function) |
| 9590 | << OvlExpr->getSourceRange(); |
| 9591 | } |
| 9592 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9593 | void ComplainOfInvalidConversion() const { |
| 9594 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9595 | << OvlExpr->getName() << TargetType; |
| 9596 | } |
| 9597 | |
| 9598 | void ComplainMultipleMatchesFound() const { |
| 9599 | assert(Matches.size() > 1); |
| 9600 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9601 | << OvlExpr->getName() |
| 9602 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9603 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9604 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9605 | |
| 9606 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9607 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9608 | int getNumMatches() const { return Matches.size(); } |
| 9609 | |
| 9610 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9611 | if (Matches.size() != 1) return 0; |
| 9612 | return Matches[0].second; |
| 9613 | } |
| 9614 | |
| 9615 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9616 | if (Matches.size() != 1) return 0; |
| 9617 | return &Matches[0].first; |
| 9618 | } |
| 9619 | }; |
| 9620 | |
| 9621 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9622 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9623 | /// expression with overloaded function type and @p ToType is the type |
| 9624 | /// we're trying to resolve to. For example: |
| 9625 | /// |
| 9626 | /// @code |
| 9627 | /// int f(double); |
| 9628 | /// int f(int); |
| 9629 | /// |
| 9630 | /// int (*pfd)(double) = f; // selects f(double) |
| 9631 | /// @endcode |
| 9632 | /// |
| 9633 | /// This routine returns the resulting FunctionDecl if it could be |
| 9634 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9635 | /// routine will emit diagnostics if there is an error. |
| 9636 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9637 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9638 | QualType TargetType, |
| 9639 | bool Complain, |
| 9640 | DeclAccessPair &FoundResult, |
| 9641 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9642 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9643 | |
| 9644 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9645 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9646 | int NumMatches = Resolver.getNumMatches(); |
| 9647 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9648 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9649 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9650 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9651 | else |
| 9652 | Resolver.ComplainNoMatchesFound(); |
| 9653 | } |
| 9654 | else if (NumMatches > 1 && Complain) |
| 9655 | Resolver.ComplainMultipleMatchesFound(); |
| 9656 | else if (NumMatches == 1) { |
| 9657 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9658 | assert(Fn); |
| 9659 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9660 | if (Complain) { |
| 9661 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9662 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9663 | else |
| 9664 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9665 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9666 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9667 | |
| 9668 | if (pHadMultipleCandidates) |
| 9669 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9670 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9671 | } |
| 9672 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9673 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9674 | /// resolve that overloaded function expression down to a single function. |
| 9675 | /// |
| 9676 | /// This routine can only resolve template-ids that refer to a single function |
| 9677 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9678 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9679 | /// as described in C++0x [temp.arg.explicit]p3. |
Alp Toker | 8b40772 | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 9680 | /// |
| 9681 | /// If no template-ids are found, no diagnostics are emitted and NULL is |
| 9682 | /// returned. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9683 | FunctionDecl * |
| 9684 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9685 | bool Complain, |
| 9686 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9687 | // C++ [over.over]p1: |
| 9688 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9689 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9690 | // C++ [over.over]p1: |
| 9691 | // [...] The overloaded function name can be preceded by the & |
| 9692 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9693 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9694 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9695 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9696 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9697 | |
| 9698 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9699 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9700 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9701 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9702 | // Look through all of the overloaded functions, searching for one |
| 9703 | // whose type matches exactly. |
| 9704 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9705 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9706 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9707 | // C++0x [temp.arg.explicit]p3: |
| 9708 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9709 | // where deduction is not done, if a template argument list is |
| 9710 | // specified and it, along with any default template arguments, |
| 9711 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9712 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9713 | FunctionTemplateDecl *FunctionTemplate |
| 9714 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9715 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9716 | // C++ [over.over]p2: |
| 9717 | // If the name is a function template, template argument deduction is |
| 9718 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9719 | // resulting template argument list is used to generate a single |
| 9720 | // function template specialization, which is added to the set of |
| 9721 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9722 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9723 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9724 | if (TemplateDeductionResult Result |
| 9725 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9726 | Specialization, Info, |
| 9727 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9728 | // Make a note of the failed deduction for diagnostics. |
| 9729 | // TODO: Actually use the failed-deduction info? |
| 9730 | FailedCandidates.addCandidate() |
| 9731 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9732 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9733 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9734 | } |
| 9735 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9736 | assert(Specialization && "no specialization and no error?"); |
| 9737 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9738 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9739 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9740 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9741 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9742 | << ovl->getName(); |
| 9743 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9744 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9745 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9746 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9747 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9748 | Matched = Specialization; |
| 9749 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9750 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9751 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9752 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9753 | Matched->getResultType()->isUndeducedType() && |
| 9754 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9755 | return 0; |
| 9756 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9757 | return Matched; |
| 9758 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9759 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9760 | |
| 9761 | |
| 9762 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9763 | // Resolve and fix an overloaded expression that can be resolved |
| 9764 | // because it identifies a single function template specialization. |
| 9765 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9766 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9767 | // |
| 9768 | // Return true if it was logically possible to so resolve the |
| 9769 | // expression, regardless of whether or not it succeeded. Always |
| 9770 | // returns true if 'complain' is set. |
| 9771 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9772 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9773 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9774 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9775 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9776 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9777 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9778 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9779 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9780 | DeclAccessPair found; |
| 9781 | ExprResult SingleFunctionExpression; |
| 9782 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9783 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9784 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9785 | SrcExpr = ExprError(); |
| 9786 | return true; |
| 9787 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9788 | |
| 9789 | // It is only correct to resolve to an instance method if we're |
| 9790 | // resolving a form that's permitted to be a pointer to member. |
| 9791 | // Otherwise we'll end up making a bound member expression, which |
| 9792 | // is illegal in all the contexts we resolve like this. |
| 9793 | if (!ovl.HasFormOfMemberPointer && |
| 9794 | isa<CXXMethodDecl>(fn) && |
| 9795 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9796 | if (!complain) return false; |
| 9797 | |
| 9798 | Diag(ovl.Expression->getExprLoc(), |
| 9799 | diag::err_bound_member_function) |
| 9800 | << 0 << ovl.Expression->getSourceRange(); |
| 9801 | |
| 9802 | // TODO: I believe we only end up here if there's a mix of |
| 9803 | // static and non-static candidates (otherwise the expression |
| 9804 | // would have 'bound member' type, not 'overload' type). |
| 9805 | // Ideally we would note which candidate was chosen and why |
| 9806 | // the static candidates were rejected. |
| 9807 | SrcExpr = ExprError(); |
| 9808 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9809 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9810 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9811 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9812 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9813 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9814 | |
| 9815 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9816 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9817 | SingleFunctionExpression = |
| 9818 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9819 | if (SingleFunctionExpression.isInvalid()) { |
| 9820 | SrcExpr = ExprError(); |
| 9821 | return true; |
| 9822 | } |
| 9823 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9824 | } |
| 9825 | |
| 9826 | if (!SingleFunctionExpression.isUsable()) { |
| 9827 | if (complain) { |
| 9828 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9829 | << ovl.Expression->getName() |
| 9830 | << DestTypeForComplaining |
| 9831 | << OpRangeForComplaining |
| 9832 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9833 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9834 | |
| 9835 | SrcExpr = ExprError(); |
| 9836 | return true; |
| 9837 | } |
| 9838 | |
| 9839 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9840 | } |
| 9841 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9842 | SrcExpr = SingleFunctionExpression; |
| 9843 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9846 | /// \brief Add a single candidate to the overload set. |
| 9847 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9848 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9849 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9850 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9851 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9852 | bool PartialOverloading, |
| 9853 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9854 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9855 | if (isa<UsingShadowDecl>(Callee)) |
| 9856 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9857 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9858 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9859 | if (ExplicitTemplateArgs) { |
| 9860 | assert(!KnownValid && "Explicit template arguments?"); |
| 9861 | return; |
| 9862 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9863 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9864 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9865 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9866 | } |
| 9867 | |
| 9868 | if (FunctionTemplateDecl *FuncTemplate |
| 9869 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9870 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9871 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9872 | return; |
| 9873 | } |
| 9874 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9875 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9876 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9877 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9878 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9879 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9880 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9881 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9882 | OverloadCandidateSet &CandidateSet, |
| 9883 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9884 | |
| 9885 | #ifndef NDEBUG |
| 9886 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9887 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9888 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9889 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9890 | // and let Y be the lookup set produced by argument dependent |
| 9891 | // lookup (defined as follows). If X contains |
| 9892 | // |
| 9893 | // -- a declaration of a class member, or |
| 9894 | // |
| 9895 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9896 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9897 | // |
| 9898 | // -- a declaration that is neither a function or a function |
| 9899 | // template |
| 9900 | // |
| 9901 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9902 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9903 | if (ULE->requiresADL()) { |
| 9904 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9905 | E = ULE->decls_end(); I != E; ++I) { |
| 9906 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9907 | assert(isa<UsingShadowDecl>(*I) || |
| 9908 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9909 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9910 | } |
| 9911 | } |
| 9912 | #endif |
| 9913 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9914 | // It would be nice to avoid this copy. |
| 9915 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9916 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9917 | if (ULE->hasExplicitTemplateArgs()) { |
| 9918 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9919 | ExplicitTemplateArgs = &TABuffer; |
| 9920 | } |
| 9921 | |
| 9922 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9923 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9924 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9925 | CandidateSet, PartialOverloading, |
| 9926 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9927 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9928 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9929 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9930 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9931 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9932 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9933 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9934 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9935 | /// Determine whether a declaration with the specified name could be moved into |
| 9936 | /// a different namespace. |
| 9937 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 9938 | switch (Name.getCXXOverloadedOperator()) { |
| 9939 | case OO_New: case OO_Array_New: |
| 9940 | case OO_Delete: case OO_Array_Delete: |
| 9941 | return false; |
| 9942 | |
| 9943 | default: |
| 9944 | return true; |
| 9945 | } |
| 9946 | } |
| 9947 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9948 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9949 | /// template, where the non-dependent name was declared after the template |
| 9950 | /// was defined. This is common in code written for a compilers which do not |
| 9951 | /// correctly implement two-stage name lookup. |
| 9952 | /// |
| 9953 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9954 | static bool |
| 9955 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9956 | const CXXScopeSpec &SS, LookupResult &R, |
| 9957 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9958 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9959 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9960 | return false; |
| 9961 | |
| 9962 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9963 | if (DC->isTransparentContext()) |
| 9964 | continue; |
| 9965 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9966 | SemaRef.LookupQualifiedName(R, DC); |
| 9967 | |
| 9968 | if (!R.empty()) { |
| 9969 | R.suppressDiagnostics(); |
| 9970 | |
| 9971 | if (isa<CXXRecordDecl>(DC)) { |
| 9972 | // Don't diagnose names we find in classes; we get much better |
| 9973 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9974 | R.clear(); |
| 9975 | return false; |
| 9976 | } |
| 9977 | |
| 9978 | OverloadCandidateSet Candidates(FnLoc); |
| 9979 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9980 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9981 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9982 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9983 | |
| 9984 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9985 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9986 | // No viable functions. Don't bother the user with notes for functions |
| 9987 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9988 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9989 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9990 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9991 | |
| 9992 | // Find the namespaces where ADL would have looked, and suggest |
| 9993 | // declaring the function there instead. |
| 9994 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9995 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9996 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9997 | AssociatedNamespaces, |
| 9998 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9999 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10000 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 10001 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 10002 | for (Sema::AssociatedNamespaceSet::iterator |
| 10003 | it = AssociatedNamespaces.begin(), |
| 10004 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 10005 | // Never suggest declaring a function within namespace 'std'. |
| 10006 | if (Std && Std->Encloses(*it)) |
| 10007 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 10008 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10009 | // Never suggest declaring a function within a namespace with a |
| 10010 | // reserved name, like __gnu_cxx. |
| 10011 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 10012 | if (NS && |
| 10013 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 10014 | continue; |
| 10015 | |
| 10016 | SuggestedNamespaces.insert(*it); |
| 10017 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10018 | } |
| 10019 | |
| 10020 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 10021 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10022 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10023 | SemaRef.Diag(Best->Function->getLocation(), |
| 10024 | diag::note_not_found_by_two_phase_lookup) |
| 10025 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10026 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10027 | SemaRef.Diag(Best->Function->getLocation(), |
| 10028 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10029 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10030 | } else { |
| 10031 | // FIXME: It would be useful to list the associated namespaces here, |
| 10032 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10033 | // a localized representation of a list of items. |
| 10034 | SemaRef.Diag(Best->Function->getLocation(), |
| 10035 | diag::note_not_found_by_two_phase_lookup) |
| 10036 | << R.getLookupName() << 2; |
| 10037 | } |
| 10038 | |
| 10039 | // Try to recover by calling this function. |
| 10040 | return true; |
| 10041 | } |
| 10042 | |
| 10043 | R.clear(); |
| 10044 | } |
| 10045 | |
| 10046 | return false; |
| 10047 | } |
| 10048 | |
| 10049 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10050 | /// template, where the non-dependent operator was declared after the template |
| 10051 | /// was defined. |
| 10052 | /// |
| 10053 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10054 | static bool |
| 10055 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10056 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10057 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10058 | DeclarationName OpName = |
| 10059 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10060 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10061 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10062 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10063 | } |
| 10064 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10065 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10066 | class BuildRecoveryCallExprRAII { |
| 10067 | Sema &SemaRef; |
| 10068 | public: |
| 10069 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10070 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10071 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10072 | } |
| 10073 | |
| 10074 | ~BuildRecoveryCallExprRAII() { |
| 10075 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10076 | } |
| 10077 | }; |
| 10078 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10079 | } |
| 10080 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10081 | /// Attempts to recover from a call where no functions were found. |
| 10082 | /// |
| 10083 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10084 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10085 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10086 | UnresolvedLookupExpr *ULE, |
| 10087 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10088 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10089 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10090 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10091 | // Do not try to recover if it is already building a recovery call. |
| 10092 | // This stops infinite loops for template instantiations like |
| 10093 | // |
| 10094 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10095 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10096 | // |
| 10097 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10098 | return ExprError(); |
| 10099 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10100 | |
| 10101 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10102 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10103 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10104 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10105 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10106 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10107 | if (ULE->hasExplicitTemplateArgs()) { |
| 10108 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10109 | ExplicitTemplateArgs = &TABuffer; |
| 10110 | } |
| 10111 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10112 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10113 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 761695f | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10114 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
| 10115 | ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10116 | NoTypoCorrectionCCC RejectAll; |
| 10117 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10118 | (CorrectionCandidateCallback*)&Validator : |
| 10119 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10120 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10121 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10122 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10123 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10124 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10125 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10126 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10127 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10128 | |
| 10129 | // Build an implicit member call if appropriate. Just drop the |
| 10130 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10131 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10132 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10133 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10134 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10135 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10136 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10137 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10138 | else |
| 10139 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10140 | |
| 10141 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10142 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10143 | |
| 10144 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10145 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10146 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10147 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10148 | MultiExprArg(Args.data(), Args.size()), |
| 10149 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10150 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10151 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10152 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10153 | /// the given function. |
| 10154 | /// \returns true when an the ExprResult output parameter has been set. |
| 10155 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10156 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10157 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10158 | SourceLocation RParenLoc, |
| 10159 | OverloadCandidateSet *CandidateSet, |
| 10160 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10161 | #ifndef NDEBUG |
| 10162 | if (ULE->requiresADL()) { |
| 10163 | // To do ADL, we must have found an unqualified name. |
| 10164 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10165 | |
| 10166 | // We don't perform ADL for implicit declarations of builtins. |
| 10167 | // Verify that this was correctly set up. |
| 10168 | FunctionDecl *F; |
| 10169 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10170 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10171 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10172 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10173 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10174 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10175 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10176 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10177 | #endif |
| 10178 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10179 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10180 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10181 | *Result = ExprError(); |
| 10182 | return true; |
| 10183 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10184 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10185 | // Add the functions denoted by the callee to the set of candidate |
| 10186 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10187 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10188 | |
| 10189 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10190 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10191 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10192 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10193 | // In Microsoft mode, if we are inside a template class member function then |
| 10194 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10195 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10196 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10197 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10198 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10199 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10200 | Context.DependentTy, VK_RValue, |
| 10201 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10202 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10203 | *Result = Owned(CE); |
| 10204 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10205 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10206 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10207 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10208 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10209 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10210 | return false; |
| 10211 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10212 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10213 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10214 | /// the completed call expression. If overload resolution fails, emits |
| 10215 | /// diagnostics and returns ExprError() |
| 10216 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10217 | UnresolvedLookupExpr *ULE, |
| 10218 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10219 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10220 | SourceLocation RParenLoc, |
| 10221 | Expr *ExecConfig, |
| 10222 | OverloadCandidateSet *CandidateSet, |
| 10223 | OverloadCandidateSet::iterator *Best, |
| 10224 | OverloadingResult OverloadResult, |
| 10225 | bool AllowTypoCorrection) { |
| 10226 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10227 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10228 | RParenLoc, /*EmptyLookup=*/true, |
| 10229 | AllowTypoCorrection); |
| 10230 | |
| 10231 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10232 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10233 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10234 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10235 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10236 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10237 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10238 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10239 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10240 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10241 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10242 | case OR_No_Viable_Function: { |
| 10243 | // Try to recover by looking for viable functions which the user might |
| 10244 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10245 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10246 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10247 | /*EmptyLookup=*/false, |
| 10248 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10249 | if (!Recovery.isInvalid()) |
| 10250 | return Recovery; |
| 10251 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10252 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10253 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10254 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10255 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10256 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10257 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10258 | |
| 10259 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10260 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10261 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10262 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10263 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10264 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10265 | case OR_Deleted: { |
| 10266 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10267 | << (*Best)->Function->isDeleted() |
| 10268 | << ULE->getName() |
| 10269 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10270 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10271 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10272 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10273 | // We emitted an error for the unvailable/deleted function call but keep |
| 10274 | // the call in the AST. |
| 10275 | FunctionDecl *FDecl = (*Best)->Function; |
| 10276 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10277 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10278 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10279 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10280 | } |
| 10281 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10282 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10283 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10284 | } |
| 10285 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10286 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10287 | /// (which eventually refers to the declaration Func) and the call |
| 10288 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10289 | /// to a specific function. If overload resolution succeeds, returns |
| 10290 | /// the call expression produced by overload resolution. |
| 10291 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10292 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10293 | UnresolvedLookupExpr *ULE, |
| 10294 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10295 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10296 | SourceLocation RParenLoc, |
| 10297 | Expr *ExecConfig, |
| 10298 | bool AllowTypoCorrection) { |
| 10299 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10300 | ExprResult result; |
| 10301 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10302 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10303 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10304 | return result; |
| 10305 | |
| 10306 | OverloadCandidateSet::iterator Best; |
| 10307 | OverloadingResult OverloadResult = |
| 10308 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10309 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10310 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10311 | RParenLoc, ExecConfig, &CandidateSet, |
| 10312 | &Best, OverloadResult, |
| 10313 | AllowTypoCorrection); |
| 10314 | } |
| 10315 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10316 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10317 | return Functions.size() > 1 || |
| 10318 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10319 | } |
| 10320 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10321 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10322 | /// operator. |
| 10323 | /// |
| 10324 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10325 | /// |
| 10326 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10327 | /// operator. |
| 10328 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10329 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10330 | /// considered by overload resolution. The caller needs to build this |
| 10331 | /// set based on the context using, e.g., |
| 10332 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10333 | /// set should not contain any member functions; those will be added |
| 10334 | /// by CreateOverloadedUnaryOp(). |
| 10335 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10336 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10337 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10338 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10339 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10340 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10341 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10342 | |
| 10343 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10344 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10345 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10346 | // TODO: provide better source location info. |
| 10347 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10348 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10349 | if (checkPlaceholderForOverload(*this, Input)) |
| 10350 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10351 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10352 | Expr *Args[2] = { Input, 0 }; |
| 10353 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10354 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10355 | // For post-increment and post-decrement, add the implicit '0' as |
| 10356 | // the second argument, so that we know this is a post-increment or |
| 10357 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10358 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10359 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10360 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10361 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10362 | NumArgs = 2; |
| 10363 | } |
| 10364 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10365 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10366 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10367 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10368 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10369 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10370 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10371 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10372 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10373 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10374 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10375 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10376 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10377 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10378 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10379 | /*ADL*/ true, IsOverloaded(Fns), |
| 10380 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10381 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10382 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10383 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10384 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10385 | } |
| 10386 | |
| 10387 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10388 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10389 | |
| 10390 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10391 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10392 | |
| 10393 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10394 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10395 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10396 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10397 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10398 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10399 | CandidateSet); |
| 10400 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10401 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10402 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10403 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10404 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10405 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10406 | // Perform overload resolution. |
| 10407 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10408 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10409 | case OR_Success: { |
| 10410 | // We found a built-in operator or an overloaded operator. |
| 10411 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10412 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10413 | if (FnDecl) { |
| 10414 | // We matched an overloaded operator. Build a call to that |
| 10415 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10416 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10417 | // Convert the arguments. |
| 10418 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10419 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10420 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10421 | ExprResult InputRes = |
| 10422 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10423 | Best->FoundDecl, Method); |
| 10424 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10425 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10426 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10427 | } else { |
| 10428 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10429 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10430 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10431 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10432 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10433 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10434 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10435 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10436 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10437 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10438 | } |
| 10439 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10440 | // Determine the result type. |
| 10441 | QualType ResultTy = FnDecl->getResultType(); |
| 10442 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10443 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10444 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10445 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10446 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10447 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10448 | if (FnExpr.isInvalid()) |
| 10449 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10450 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10451 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10452 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10453 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10454 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10455 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10456 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10457 | FnDecl)) |
| 10458 | return ExprError(); |
| 10459 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10460 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10461 | } else { |
| 10462 | // We matched a built-in operator. Convert the arguments, then |
| 10463 | // break out so that we will build the appropriate built-in |
| 10464 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10465 | ExprResult InputRes = |
| 10466 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10467 | Best->Conversions[0], AA_Passing); |
| 10468 | if (InputRes.isInvalid()) |
| 10469 | return ExprError(); |
| 10470 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10471 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10472 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10473 | } |
| 10474 | |
| 10475 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10476 | // This is an erroneous use of an operator which can be overloaded by |
| 10477 | // a non-member function. Check for non-member operators which were |
| 10478 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10479 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10480 | // FIXME: Recover by calling the found function. |
| 10481 | return ExprError(); |
| 10482 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10483 | // No viable function; fall through to handling this as a |
| 10484 | // built-in operator, which will produce an error message for us. |
| 10485 | break; |
| 10486 | |
| 10487 | case OR_Ambiguous: |
| 10488 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10489 | << UnaryOperator::getOpcodeStr(Opc) |
| 10490 | << Input->getType() |
| 10491 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10492 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10493 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10494 | return ExprError(); |
| 10495 | |
| 10496 | case OR_Deleted: |
| 10497 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10498 | << Best->Function->isDeleted() |
| 10499 | << UnaryOperator::getOpcodeStr(Opc) |
| 10500 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10501 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10502 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10503 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10504 | return ExprError(); |
| 10505 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10506 | |
| 10507 | // Either we found no viable overloaded operator or we matched a |
| 10508 | // built-in operator. In either case, fall through to trying to |
| 10509 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10510 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10511 | } |
| 10512 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10513 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10514 | /// operator. |
| 10515 | /// |
| 10516 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10517 | /// |
| 10518 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10519 | /// operator. |
| 10520 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10521 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10522 | /// considered by overload resolution. The caller needs to build this |
| 10523 | /// set based on the context using, e.g., |
| 10524 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10525 | /// set should not contain any member functions; those will be added |
| 10526 | /// by CreateOverloadedBinOp(). |
| 10527 | /// |
| 10528 | /// \param LHS Left-hand argument. |
| 10529 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10530 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10531 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10532 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10533 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10534 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10535 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10536 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10537 | |
| 10538 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10539 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10540 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10541 | |
| 10542 | // If either side is type-dependent, create an appropriate dependent |
| 10543 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10544 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10545 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10546 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10547 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10548 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10549 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10550 | Context.DependentTy, |
| 10551 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10552 | OpLoc, |
| 10553 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10554 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10555 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10556 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10557 | VK_LValue, |
| 10558 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10559 | Context.DependentTy, |
| 10560 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10561 | OpLoc, |
| 10562 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10563 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10564 | |
| 10565 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10566 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10567 | // TODO: provide better source location info in DNLoc component. |
| 10568 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10569 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10570 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10571 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10572 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10573 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10574 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10575 | Context.DependentTy, VK_RValue, |
| 10576 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10577 | } |
| 10578 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10579 | // Always do placeholder-like conversions on the RHS. |
| 10580 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10581 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10582 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10583 | // Do placeholder-like conversion on the LHS; note that we should |
| 10584 | // not get here with a PseudoObject LHS. |
| 10585 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10586 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10587 | return ExprError(); |
| 10588 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10589 | // If this is the assignment operator, we only perform overload resolution |
| 10590 | // if the left-hand side is a class or enumeration type. This is actually |
| 10591 | // a hack. The standard requires that we do overload resolution between the |
| 10592 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10593 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10594 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10595 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10596 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10597 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10598 | // If this is the .* operator, which is not overloadable, just |
| 10599 | // create a built-in binary operator. |
| 10600 | if (Opc == BO_PtrMemD) |
| 10601 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10602 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10603 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10604 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10605 | |
| 10606 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10607 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10608 | |
| 10609 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10610 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10611 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10612 | // Add candidates from ADL. |
| 10613 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10614 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10615 | /*ExplicitTemplateArgs*/ 0, |
| 10616 | CandidateSet); |
| 10617 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10618 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10619 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10620 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10621 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10622 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10623 | // Perform overload resolution. |
| 10624 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10625 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10626 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10627 | // We found a built-in operator or an overloaded operator. |
| 10628 | FunctionDecl *FnDecl = Best->Function; |
| 10629 | |
| 10630 | if (FnDecl) { |
| 10631 | // We matched an overloaded operator. Build a call to that |
| 10632 | // operator. |
| 10633 | |
| 10634 | // Convert the arguments. |
| 10635 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10636 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10637 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10638 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10639 | ExprResult Arg1 = |
| 10640 | PerformCopyInitialization( |
| 10641 | InitializedEntity::InitializeParameter(Context, |
| 10642 | FnDecl->getParamDecl(0)), |
| 10643 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10644 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10645 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10646 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10647 | ExprResult Arg0 = |
| 10648 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10649 | Best->FoundDecl, Method); |
| 10650 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10651 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10652 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10653 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10654 | } else { |
| 10655 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10656 | ExprResult Arg0 = PerformCopyInitialization( |
| 10657 | InitializedEntity::InitializeParameter(Context, |
| 10658 | FnDecl->getParamDecl(0)), |
| 10659 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10660 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10661 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10662 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10663 | ExprResult Arg1 = |
| 10664 | PerformCopyInitialization( |
| 10665 | InitializedEntity::InitializeParameter(Context, |
| 10666 | FnDecl->getParamDecl(1)), |
| 10667 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10668 | if (Arg1.isInvalid()) |
| 10669 | return ExprError(); |
| 10670 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10671 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10672 | } |
| 10673 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10674 | // Determine the result type. |
| 10675 | QualType ResultTy = FnDecl->getResultType(); |
| 10676 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10677 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10678 | |
| 10679 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10680 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10681 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10682 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10683 | if (FnExpr.isInvalid()) |
| 10684 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10685 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10686 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10687 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10688 | Args, ResultTy, VK, OpLoc, |
| 10689 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10690 | |
| 10691 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10692 | FnDecl)) |
| 10693 | return ExprError(); |
| 10694 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10695 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10696 | // Cut off the implicit 'this'. |
| 10697 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10698 | ArgsArray = ArgsArray.slice(1); |
| 10699 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10700 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10701 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10702 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10703 | } else { |
| 10704 | // We matched a built-in operator. Convert the arguments, then |
| 10705 | // break out so that we will build the appropriate built-in |
| 10706 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10707 | ExprResult ArgsRes0 = |
| 10708 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10709 | Best->Conversions[0], AA_Passing); |
| 10710 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10711 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10712 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10713 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10714 | ExprResult ArgsRes1 = |
| 10715 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10716 | Best->Conversions[1], AA_Passing); |
| 10717 | if (ArgsRes1.isInvalid()) |
| 10718 | return ExprError(); |
| 10719 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10720 | break; |
| 10721 | } |
| 10722 | } |
| 10723 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10724 | case OR_No_Viable_Function: { |
| 10725 | // C++ [over.match.oper]p9: |
| 10726 | // If the operator is the operator , [...] and there are no |
| 10727 | // viable functions, then the operator is assumed to be the |
| 10728 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10729 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10730 | break; |
| 10731 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10732 | // For class as left operand for assignment or compound assigment |
| 10733 | // operator do not fall through to handling in built-in, but report that |
| 10734 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10735 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10736 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10737 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10738 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10739 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10740 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 3f93d4c | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 10741 | if (Args[0]->getType()->isIncompleteType()) { |
| 10742 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 10743 | << Args[0]->getType() |
| 10744 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10745 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10746 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10747 | // This is an erroneous use of an operator which can be overloaded by |
| 10748 | // a non-member function. Check for non-member operators which were |
| 10749 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10750 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10751 | // FIXME: Recover by calling the found function. |
| 10752 | return ExprError(); |
| 10753 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10754 | // No viable function; try to create a built-in operation, which will |
| 10755 | // produce an error. Then, show the non-viable candidates. |
| 10756 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10757 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10758 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10759 | "C++ binary operator overloading is missing candidates!"); |
| 10760 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10761 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10762 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10763 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10764 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10765 | |
| 10766 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10767 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10768 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10769 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10770 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10771 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10772 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10773 | return ExprError(); |
| 10774 | |
| 10775 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10776 | if (isImplicitlyDeleted(Best->Function)) { |
| 10777 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10778 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10779 | << Context.getRecordType(Method->getParent()) |
| 10780 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10781 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10782 | // The user probably meant to call this special member. Just |
| 10783 | // explain why it's deleted. |
| 10784 | NoteDeletedFunction(Method); |
| 10785 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10786 | } else { |
| 10787 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10788 | << Best->Function->isDeleted() |
| 10789 | << BinaryOperator::getOpcodeStr(Opc) |
| 10790 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10791 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10792 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10793 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10794 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10795 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10796 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10797 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10798 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10799 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10800 | } |
| 10801 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10802 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10803 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10804 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10805 | Expr *Base, Expr *Idx) { |
| 10806 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10807 | DeclarationName OpName = |
| 10808 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10809 | |
| 10810 | // If either side is type-dependent, create an appropriate dependent |
| 10811 | // expression. |
| 10812 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10813 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10814 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10815 | // CHECKME: no 'operator' keyword? |
| 10816 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10817 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10818 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10819 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10820 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10821 | /*ADL*/ true, /*Overloaded*/ false, |
| 10822 | UnresolvedSetIterator(), |
| 10823 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10824 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10825 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10826 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10827 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10828 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10829 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10830 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10831 | } |
| 10832 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10833 | // Handle placeholders on both operands. |
| 10834 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10835 | return ExprError(); |
| 10836 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10837 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10838 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10839 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10840 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10841 | |
| 10842 | // Subscript can only be overloaded as a member function. |
| 10843 | |
| 10844 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10845 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10846 | |
| 10847 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10848 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10849 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10850 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10851 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10852 | // Perform overload resolution. |
| 10853 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10854 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10855 | case OR_Success: { |
| 10856 | // We found a built-in operator or an overloaded operator. |
| 10857 | FunctionDecl *FnDecl = Best->Function; |
| 10858 | |
| 10859 | if (FnDecl) { |
| 10860 | // We matched an overloaded operator. Build a call to that |
| 10861 | // operator. |
| 10862 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10863 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10864 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10865 | // Convert the arguments. |
| 10866 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10867 | ExprResult Arg0 = |
| 10868 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10869 | Best->FoundDecl, Method); |
| 10870 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10871 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10872 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10873 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10874 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10875 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10876 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10877 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10878 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10879 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10880 | Owned(Args[1])); |
| 10881 | if (InputInit.isInvalid()) |
| 10882 | return ExprError(); |
| 10883 | |
| 10884 | Args[1] = InputInit.takeAs<Expr>(); |
| 10885 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10886 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10887 | QualType ResultTy = FnDecl->getResultType(); |
| 10888 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10889 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10890 | |
| 10891 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10892 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10893 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10894 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10895 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10896 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10897 | OpLocInfo.getLoc(), |
| 10898 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10899 | if (FnExpr.isInvalid()) |
| 10900 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10901 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10902 | CXXOperatorCallExpr *TheCall = |
| 10903 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10904 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10905 | ResultTy, VK, RLoc, |
| 10906 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10907 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10908 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10909 | FnDecl)) |
| 10910 | return ExprError(); |
| 10911 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10912 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10913 | } else { |
| 10914 | // We matched a built-in operator. Convert the arguments, then |
| 10915 | // break out so that we will build the appropriate built-in |
| 10916 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10917 | ExprResult ArgsRes0 = |
| 10918 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10919 | Best->Conversions[0], AA_Passing); |
| 10920 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10921 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10922 | Args[0] = ArgsRes0.take(); |
| 10923 | |
| 10924 | ExprResult ArgsRes1 = |
| 10925 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10926 | Best->Conversions[1], AA_Passing); |
| 10927 | if (ArgsRes1.isInvalid()) |
| 10928 | return ExprError(); |
| 10929 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10930 | |
| 10931 | break; |
| 10932 | } |
| 10933 | } |
| 10934 | |
| 10935 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10936 | if (CandidateSet.empty()) |
| 10937 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10938 | << Args[0]->getType() << /*subscript*/ 0 |
| 10939 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10940 | else |
| 10941 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10942 | << Args[0]->getType() |
| 10943 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10944 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10945 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10946 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10947 | } |
| 10948 | |
| 10949 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10950 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10951 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10952 | << Args[0]->getType() << Args[1]->getType() |
| 10953 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10954 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10955 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10956 | return ExprError(); |
| 10957 | |
| 10958 | case OR_Deleted: |
| 10959 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10960 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10961 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10962 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10963 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10964 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10965 | return ExprError(); |
| 10966 | } |
| 10967 | |
| 10968 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10969 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10970 | } |
| 10971 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10972 | /// BuildCallToMemberFunction - Build a call to a member |
| 10973 | /// function. MemExpr is the expression that refers to the member |
| 10974 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10975 | /// arguments to the function call (not including the object |
| 10976 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10977 | /// expression refers to a non-static member function or an overloaded |
| 10978 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10979 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10980 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10981 | SourceLocation LParenLoc, |
| 10982 | MultiExprArg Args, |
| 10983 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10984 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10985 | MemExprE->getType() == Context.OverloadTy); |
| 10986 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10987 | // Dig out the member expression. This holds both the object |
| 10988 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10989 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10990 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10991 | // Determine whether this is a call to a pointer-to-member function. |
| 10992 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10993 | assert(op->getType() == Context.BoundMemberTy); |
| 10994 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10995 | |
| 10996 | QualType fnType = |
| 10997 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10998 | |
| 10999 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 11000 | QualType resultType = proto->getCallResultType(Context); |
| 11001 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 11002 | |
| 11003 | // Check that the object type isn't more qualified than the |
| 11004 | // member function we're calling. |
| 11005 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 11006 | |
| 11007 | QualType objectType = op->getLHS()->getType(); |
| 11008 | if (op->getOpcode() == BO_PtrMemI) |
| 11009 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 11010 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 11011 | |
| 11012 | Qualifiers difference = objectQuals - funcQuals; |
| 11013 | difference.removeObjCGCAttr(); |
| 11014 | difference.removeAddressSpace(); |
| 11015 | if (difference) { |
| 11016 | std::string qualsString = difference.getAsString(); |
| 11017 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 11018 | << fnType.getUnqualifiedType() |
| 11019 | << qualsString |
| 11020 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 11021 | } |
| 11022 | |
| 11023 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11024 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11025 | resultType, valueKind, RParenLoc); |
| 11026 | |
| 11027 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11028 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11029 | call, 0)) |
| 11030 | return ExprError(); |
| 11031 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11032 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11033 | return ExprError(); |
| 11034 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11035 | if (CheckOtherCall(call, proto)) |
| 11036 | return ExprError(); |
| 11037 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11038 | return MaybeBindToTemporary(call); |
| 11039 | } |
| 11040 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11041 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11042 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11043 | return ExprError(); |
| 11044 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11045 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11046 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11047 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11048 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11049 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11050 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11051 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11052 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11053 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11054 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11055 | } else { |
| 11056 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11057 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11058 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11059 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11060 | Expr::Classification ObjectClassification |
| 11061 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11062 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11063 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11064 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11065 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11066 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11067 | // FIXME: avoid copy. |
| 11068 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11069 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11070 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11071 | TemplateArgs = &TemplateArgsBuffer; |
| 11072 | } |
| 11073 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11074 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11075 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11076 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11077 | NamedDecl *Func = *I; |
| 11078 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11079 | if (isa<UsingShadowDecl>(Func)) |
| 11080 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11081 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11082 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11083 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11084 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11085 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11086 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11087 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11088 | // If explicit template arguments were provided, we can't call a |
| 11089 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11090 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11091 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11092 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11093 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11094 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11095 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11096 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11097 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11098 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11099 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11100 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11101 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11102 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11104 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11105 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11106 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11107 | UnbridgedCasts.restore(); |
| 11108 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11109 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11110 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11111 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11112 | case OR_Success: |
| 11113 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11114 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11115 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11116 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11117 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11118 | // If FoundDecl is different from Method (such as if one is a template |
| 11119 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11120 | // called on both. |
| 11121 | // FIXME: This would be more comprehensively addressed by modifying |
| 11122 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11123 | // being used. |
| 11124 | if (Method != FoundDecl.getDecl() && |
| 11125 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11126 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11127 | break; |
| 11128 | |
| 11129 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11130 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11131 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11132 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11133 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11134 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11135 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11136 | |
| 11137 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11138 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11139 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11140 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11141 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11142 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11143 | |
| 11144 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11145 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11146 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11147 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11148 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11149 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11150 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11151 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11152 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11153 | } |
| 11154 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11155 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11156 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11157 | // If overload resolution picked a static member, build a |
| 11158 | // non-member call based on that function. |
| 11159 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11160 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11161 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11162 | } |
| 11163 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11164 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11165 | } |
| 11166 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11167 | QualType ResultType = Method->getResultType(); |
| 11168 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11169 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11170 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11171 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11172 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11173 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11174 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11175 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11176 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11177 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11178 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11179 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11180 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11181 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11182 | // We only need to do this if there was actually an overload; otherwise |
| 11183 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11184 | if (!Method->isStatic()) { |
| 11185 | ExprResult ObjectArg = |
| 11186 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11187 | FoundDecl, Method); |
| 11188 | if (ObjectArg.isInvalid()) |
| 11189 | return ExprError(); |
| 11190 | MemExpr->setBase(ObjectArg.take()); |
| 11191 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11192 | |
| 11193 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11194 | const FunctionProtoType *Proto = |
| 11195 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11196 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11197 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11198 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11199 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11200 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11201 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11202 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11203 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11204 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11205 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11206 | isa<CXXDestructorDecl>(CurContext)) && |
| 11207 | TheCall->getMethodDecl()->isPure()) { |
| 11208 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11209 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11210 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11211 | Diag(MemExpr->getLocStart(), |
| 11212 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11213 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11214 | << MD->getParent()->getDeclName(); |
| 11215 | |
| 11216 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11217 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11218 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11219 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11220 | } |
| 11221 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11222 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11223 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11224 | /// overloaded function call operator (@c operator()) or performing a |
| 11225 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11226 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11227 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11228 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11229 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11230 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11231 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11232 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11233 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11234 | |
| 11235 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11236 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11237 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11238 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11239 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11240 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11241 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11242 | // C++ [over.call.object]p1: |
| 11243 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11244 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11245 | // candidate functions includes at least the function call |
| 11246 | // operators of T. The function call operators of T are obtained by |
| 11247 | // ordinary lookup of the name operator() in the context of |
| 11248 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11249 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11250 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11251 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11252 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11253 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11254 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11255 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11256 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11257 | LookupQualifiedName(R, Record->getDecl()); |
| 11258 | R.suppressDiagnostics(); |
| 11259 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11260 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11261 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11262 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11263 | Object.get()->Classify(Context), |
| 11264 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11265 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11266 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11267 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11268 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11269 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11270 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11271 | // |
| 11272 | // operator conversion-type-id () cv-qualifier; |
| 11273 | // |
| 11274 | // where cv-qualifier is the same cv-qualification as, or a |
| 11275 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11276 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11277 | // R", or the type "reference to pointer to function of |
| 11278 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11279 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11280 | // is also considered as a candidate function. Similarly, |
| 11281 | // surrogate call functions are added to the set of candidate |
| 11282 | // functions for each conversion function declared in an |
| 11283 | // accessible base class provided the function is not hidden |
| 11284 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11285 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11286 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11287 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11288 | for (CXXRecordDecl::conversion_iterator |
| 11289 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11290 | NamedDecl *D = *I; |
| 11291 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11292 | if (isa<UsingShadowDecl>(D)) |
| 11293 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11294 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11295 | // Skip over templated conversion functions; they aren't |
| 11296 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11297 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11298 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11299 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11300 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11301 | if (!Conv->isExplicit()) { |
| 11302 | // Strip the reference type (if any) and then the pointer type (if |
| 11303 | // any) to get down to what might be a function type. |
| 11304 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11305 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11306 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11307 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11308 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11309 | { |
| 11310 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11311 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11312 | } |
| 11313 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11314 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11315 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11316 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11317 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11318 | // Perform overload resolution. |
| 11319 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11320 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11321 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11322 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11323 | // Overload resolution succeeded; we'll build the appropriate call |
| 11324 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11325 | break; |
| 11326 | |
| 11327 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11328 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11329 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11330 | << Object.get()->getType() << /*call*/ 1 |
| 11331 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11332 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11333 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11334 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11335 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11336 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11337 | break; |
| 11338 | |
| 11339 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11340 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11341 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11342 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11343 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11344 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11345 | |
| 11346 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11347 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11348 | diag::err_ovl_deleted_object_call) |
| 11349 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11350 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11351 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11352 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11353 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11354 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11355 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11356 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11357 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11358 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11359 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11360 | UnbridgedCasts.restore(); |
| 11361 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11362 | if (Best->Function == 0) { |
| 11363 | // Since there is no function declaration, this is one of the |
| 11364 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11365 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11366 | = cast<CXXConversionDecl>( |
| 11367 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11368 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11369 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11370 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11371 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11372 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11373 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11374 | // We selected one of the surrogate functions that converts the |
| 11375 | // object parameter to a function pointer. Perform the conversion |
| 11376 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11377 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11378 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11379 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11380 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11381 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11382 | if (Call.isInvalid()) |
| 11383 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11384 | // Record usage of conversion in an implicit cast. |
| 11385 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11386 | CK_UserDefinedConversion, |
| 11387 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11388 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11389 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11390 | } |
| 11391 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11392 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11393 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11394 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11395 | // that calls this method, using Object for the implicit object |
| 11396 | // parameter and passing along the remaining arguments. |
| 11397 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11398 | |
| 11399 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11400 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11401 | return ExprError(); |
| 11402 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11403 | const FunctionProtoType *Proto = |
| 11404 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11405 | |
| 11406 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11407 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11408 | DeclarationNameInfo OpLocInfo( |
| 11409 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11410 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11411 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11412 | HadMultipleCandidates, |
| 11413 | OpLocInfo.getLoc(), |
| 11414 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11415 | if (NewFn.isInvalid()) |
| 11416 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11417 | |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11418 | // Build the full argument list for the method call (the implicit object |
| 11419 | // parameter is placed at the beginning of the list). |
| 11420 | llvm::OwningArrayPtr<Expr *> MethodArgs(new Expr*[Args.size() + 1]); |
| 11421 | MethodArgs[0] = Object.get(); |
| 11422 | std::copy(Args.begin(), Args.end(), &MethodArgs[1]); |
| 11423 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11424 | // Once we've built TheCall, all of the expressions are properly |
| 11425 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11426 | QualType ResultTy = Method->getResultType(); |
| 11427 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11428 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11429 | |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11430 | CXXOperatorCallExpr *TheCall = new (Context) |
| 11431 | CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
| 11432 | llvm::makeArrayRef(MethodArgs.get(), Args.size() + 1), |
| 11433 | ResultTy, VK, RParenLoc, false); |
| 11434 | MethodArgs.reset(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11435 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11436 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11437 | Method)) |
| 11438 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11439 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11440 | // We may have default arguments. If so, we need to allocate more |
| 11441 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11442 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11443 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11444 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11445 | bool IsError = false; |
| 11446 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11447 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11448 | ExprResult ObjRes = |
| 11449 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11450 | Best->FoundDecl, Method); |
| 11451 | if (ObjRes.isInvalid()) |
| 11452 | IsError = true; |
| 11453 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11454 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11455 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11456 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11457 | // Check the argument types. |
Benjamin Kramer | 83372f8 | 2013-10-05 10:03:01 +0000 | [diff] [blame] | 11458 | for (unsigned i = 0; i != NumArgsInProto; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11459 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11460 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11461 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11462 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11463 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11464 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11465 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11466 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11467 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11468 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11469 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11470 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11471 | IsError |= InputInit.isInvalid(); |
| 11472 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11473 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11474 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11475 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11476 | if (DefArg.isInvalid()) { |
| 11477 | IsError = true; |
| 11478 | break; |
| 11479 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11480 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11481 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11482 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11483 | |
| 11484 | TheCall->setArg(i + 1, Arg); |
| 11485 | } |
| 11486 | |
| 11487 | // If this is a variadic call, handle args passed through "...". |
| 11488 | if (Proto->isVariadic()) { |
| 11489 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11490 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11491 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11492 | IsError |= Arg.isInvalid(); |
| 11493 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11494 | } |
| 11495 | } |
| 11496 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11497 | if (IsError) return true; |
| 11498 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11499 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11500 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11501 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11502 | return true; |
| 11503 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11504 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11505 | } |
| 11506 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11507 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11508 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11509 | /// @c Member is the name of the member we're trying to find. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11510 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11511 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11512 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11513 | assert(Base->getType()->isRecordType() && |
| 11514 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11515 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11516 | if (checkPlaceholderForOverload(*this, Base)) |
| 11517 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11518 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11519 | SourceLocation Loc = Base->getExprLoc(); |
| 11520 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11521 | // C++ [over.ref]p1: |
| 11522 | // |
| 11523 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11524 | // for a class object x of type T if T::operator->() exists and if |
| 11525 | // the operator is selected as the best match function by the |
| 11526 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11527 | DeclarationName OpName = |
| 11528 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11529 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11530 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11531 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11532 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11533 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11534 | return ExprError(); |
| 11535 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11536 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11537 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11538 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11539 | |
| 11540 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11541 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11542 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11543 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11544 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11545 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11546 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11547 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11548 | // Perform overload resolution. |
| 11549 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11550 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11551 | case OR_Success: |
| 11552 | // Overload resolution succeeded; we'll build the call below. |
| 11553 | break; |
| 11554 | |
| 11555 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11556 | if (CandidateSet.empty()) { |
| 11557 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11558 | if (NoArrowOperatorFound) { |
| 11559 | // Report this specific error to the caller instead of emitting a |
| 11560 | // diagnostic, as requested. |
| 11561 | *NoArrowOperatorFound = true; |
| 11562 | return ExprError(); |
| 11563 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11564 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11565 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11566 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11567 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11568 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11569 | } |
| 11570 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11571 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11572 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11573 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11574 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11575 | |
| 11576 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11577 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11578 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11579 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11580 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11581 | |
| 11582 | case OR_Deleted: |
| 11583 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11584 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11585 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11586 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11587 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11588 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11589 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11590 | } |
| 11591 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11592 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11593 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11594 | // Convert the object parameter. |
| 11595 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11596 | ExprResult BaseResult = |
| 11597 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11598 | Best->FoundDecl, Method); |
| 11599 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11600 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11601 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11602 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11603 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11604 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11605 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11606 | if (FnExpr.isInvalid()) |
| 11607 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11608 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11609 | QualType ResultTy = Method->getResultType(); |
| 11610 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11611 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11612 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11613 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11614 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11615 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11616 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11617 | Method)) |
| 11618 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11619 | |
| 11620 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11621 | } |
| 11622 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11623 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11624 | /// a literal operator described by the provided lookup results. |
| 11625 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11626 | DeclarationNameInfo &SuffixInfo, |
| 11627 | ArrayRef<Expr*> Args, |
| 11628 | SourceLocation LitEndLoc, |
| 11629 | TemplateArgumentListInfo *TemplateArgs) { |
| 11630 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11631 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11632 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11633 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11634 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11635 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11636 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11637 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11638 | // Perform overload resolution. This will usually be trivial, but might need |
| 11639 | // to perform substitutions for a literal operator template. |
| 11640 | OverloadCandidateSet::iterator Best; |
| 11641 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11642 | case OR_Success: |
| 11643 | case OR_Deleted: |
| 11644 | break; |
| 11645 | |
| 11646 | case OR_No_Viable_Function: |
| 11647 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11648 | << R.getLookupName(); |
| 11649 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11650 | return ExprError(); |
| 11651 | |
| 11652 | case OR_Ambiguous: |
| 11653 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11654 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11655 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11656 | } |
| 11657 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11658 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11659 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11660 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11661 | SuffixInfo.getLoc(), |
| 11662 | SuffixInfo.getInfo()); |
| 11663 | if (Fn.isInvalid()) |
| 11664 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11665 | |
| 11666 | // Check the argument types. This should almost always be a no-op, except |
| 11667 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11668 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11669 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11670 | ExprResult InputInit = PerformCopyInitialization( |
| 11671 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11672 | SourceLocation(), Args[ArgIdx]); |
| 11673 | if (InputInit.isInvalid()) |
| 11674 | return true; |
| 11675 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11676 | } |
| 11677 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11678 | QualType ResultTy = FD->getResultType(); |
| 11679 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11680 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11681 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11682 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11683 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11684 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11685 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11686 | |
| 11687 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11688 | return ExprError(); |
| 11689 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11690 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11691 | return ExprError(); |
| 11692 | |
| 11693 | return MaybeBindToTemporary(UDL); |
| 11694 | } |
| 11695 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11696 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11697 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11698 | /// will be invoked. Otherwise, the function will be found via argument |
| 11699 | /// dependent lookup. |
| 11700 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11701 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11702 | /// is returned. |
| 11703 | Sema::ForRangeStatus |
| 11704 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11705 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11706 | BeginEndFunction BEF, |
| 11707 | const DeclarationNameInfo &NameInfo, |
| 11708 | LookupResult &MemberLookup, |
| 11709 | OverloadCandidateSet *CandidateSet, |
| 11710 | Expr *Range, ExprResult *CallExpr) { |
| 11711 | CandidateSet->clear(); |
| 11712 | if (!MemberLookup.empty()) { |
| 11713 | ExprResult MemberRef = |
| 11714 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11715 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11716 | /*TemplateKWLoc=*/SourceLocation(), |
| 11717 | /*FirstQualifierInScope=*/0, |
| 11718 | MemberLookup, |
| 11719 | /*TemplateArgs=*/0); |
| 11720 | if (MemberRef.isInvalid()) { |
| 11721 | *CallExpr = ExprError(); |
| 11722 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11723 | << RangeLoc << BEF << Range->getType(); |
| 11724 | return FRS_DiagnosticIssued; |
| 11725 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11726 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11727 | if (CallExpr->isInvalid()) { |
| 11728 | *CallExpr = ExprError(); |
| 11729 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11730 | << RangeLoc << BEF << Range->getType(); |
| 11731 | return FRS_DiagnosticIssued; |
| 11732 | } |
| 11733 | } else { |
| 11734 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11735 | UnresolvedLookupExpr *Fn = |
| 11736 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11737 | NestedNameSpecifierLoc(), NameInfo, |
| 11738 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11739 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11740 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11741 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11742 | CandidateSet, CallExpr); |
| 11743 | if (CandidateSet->empty() || CandidateSetError) { |
| 11744 | *CallExpr = ExprError(); |
| 11745 | return FRS_NoViableFunction; |
| 11746 | } |
| 11747 | OverloadCandidateSet::iterator Best; |
| 11748 | OverloadingResult OverloadResult = |
| 11749 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11750 | |
| 11751 | if (OverloadResult == OR_No_Viable_Function) { |
| 11752 | *CallExpr = ExprError(); |
| 11753 | return FRS_NoViableFunction; |
| 11754 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11755 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11756 | Loc, 0, CandidateSet, &Best, |
| 11757 | OverloadResult, |
| 11758 | /*AllowTypoCorrection=*/false); |
| 11759 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11760 | *CallExpr = ExprError(); |
| 11761 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11762 | << RangeLoc << BEF << Range->getType(); |
| 11763 | return FRS_DiagnosticIssued; |
| 11764 | } |
| 11765 | } |
| 11766 | return FRS_Success; |
| 11767 | } |
| 11768 | |
| 11769 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11770 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11771 | /// a C++ overloaded function (possibly with some parentheses and |
| 11772 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11773 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11774 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11775 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11776 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11777 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11778 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11779 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11780 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11781 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11782 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11783 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11784 | } |
| 11785 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11786 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11787 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11788 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11789 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11790 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11791 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11792 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11793 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11794 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11795 | |
| 11796 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11797 | ICE->getCastKind(), |
| 11798 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11799 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11800 | } |
| 11801 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11802 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11803 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11804 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11805 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11806 | if (Method->isStatic()) { |
| 11807 | // Do nothing: static member functions aren't any different |
| 11808 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11809 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11810 | // Fix the sub expression, which really has to be an |
| 11811 | // UnresolvedLookupExpr holding an overloaded member function |
| 11812 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11813 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11814 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11815 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11816 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11817 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11818 | assert(isa<DeclRefExpr>(SubExpr) |
| 11819 | && "fixed to something other than a decl ref"); |
| 11820 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11821 | && "fixed to a member ref with no nested name qualifier"); |
| 11822 | |
| 11823 | // We have taken the address of a pointer to member |
| 11824 | // function. Perform the computation here so that we get the |
| 11825 | // appropriate pointer to member type. |
| 11826 | QualType ClassType |
| 11827 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11828 | QualType MemPtrType |
| 11829 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11830 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11831 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11832 | VK_RValue, OK_Ordinary, |
| 11833 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11834 | } |
| 11835 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11836 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11837 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11838 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11839 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11840 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11841 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11842 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11843 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11844 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11845 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11846 | |
| 11847 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11848 | // FIXME: avoid copy. |
| 11849 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11850 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11851 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11852 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11853 | } |
| 11854 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11855 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11856 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11857 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11858 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11859 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11860 | ULE->getNameLoc(), |
| 11861 | Fn->getType(), |
| 11862 | VK_LValue, |
| 11863 | Found.getDecl(), |
| 11864 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11865 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11866 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11867 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11868 | } |
| 11869 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11870 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11871 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11872 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11873 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11874 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11875 | TemplateArgs = &TemplateArgsBuffer; |
| 11876 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11877 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11878 | Expr *Base; |
| 11879 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11880 | // If we're filling in a static method where we used to have an |
| 11881 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11882 | if (MemExpr->isImplicitAccess()) { |
| 11883 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11884 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11885 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11886 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11887 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11888 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11889 | MemExpr->getMemberLoc(), |
| 11890 | Fn->getType(), |
| 11891 | VK_LValue, |
| 11892 | Found.getDecl(), |
| 11893 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11894 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11895 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11896 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11897 | } else { |
| 11898 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11899 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11900 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11901 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11902 | Base = new (Context) CXXThisExpr(Loc, |
| 11903 | MemExpr->getBaseType(), |
| 11904 | /*isImplicit=*/true); |
| 11905 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11906 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11907 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11908 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11909 | ExprValueKind valueKind; |
| 11910 | QualType type; |
| 11911 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11912 | valueKind = VK_LValue; |
| 11913 | type = Fn->getType(); |
| 11914 | } else { |
| 11915 | valueKind = VK_RValue; |
| 11916 | type = Context.BoundMemberTy; |
| 11917 | } |
| 11918 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11919 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11920 | MemExpr->isArrow(), |
| 11921 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11922 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11923 | Fn, |
| 11924 | Found, |
| 11925 | MemExpr->getMemberNameInfo(), |
| 11926 | TemplateArgs, |
| 11927 | type, valueKind, OK_Ordinary); |
| 11928 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11929 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11930 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11931 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11932 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11933 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11934 | } |
| 11935 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11936 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11937 | DeclAccessPair Found, |
| 11938 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11939 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11940 | } |
| 11941 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11942 | } // end namespace clang |