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, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 85 | bool AllowExplicit, |
| 86 | bool AllowObjCConversionOnExplicit); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | static ImplicitConversionSequence::CompareKind |
| 90 | CompareStandardConversionSequences(Sema &S, |
| 91 | const StandardConversionSequence& SCS1, |
| 92 | const StandardConversionSequence& SCS2); |
| 93 | |
| 94 | static ImplicitConversionSequence::CompareKind |
| 95 | CompareQualificationConversions(Sema &S, |
| 96 | const StandardConversionSequence& SCS1, |
| 97 | const StandardConversionSequence& SCS2); |
| 98 | |
| 99 | static ImplicitConversionSequence::CompareKind |
| 100 | CompareDerivedToBaseConversions(Sema &S, |
| 101 | const StandardConversionSequence& SCS1, |
| 102 | const StandardConversionSequence& SCS2); |
| 103 | |
| 104 | |
| 105 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | /// GetConversionCategory - Retrieve the implicit conversion |
| 107 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 109 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 110 | static const ImplicitConversionCategory |
| 111 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 112 | ICC_Identity, |
| 113 | ICC_Lvalue_Transformation, |
| 114 | ICC_Lvalue_Transformation, |
| 115 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 116 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 117 | ICC_Qualification_Adjustment, |
| 118 | ICC_Promotion, |
| 119 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 120 | ICC_Promotion, |
| 121 | ICC_Conversion, |
| 122 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 123 | ICC_Conversion, |
| 124 | ICC_Conversion, |
| 125 | ICC_Conversion, |
| 126 | ICC_Conversion, |
| 127 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 128 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 129 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 130 | ICC_Conversion, |
| 131 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 132 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 133 | ICC_Conversion |
| 134 | }; |
| 135 | return Category[(int)Kind]; |
| 136 | } |
| 137 | |
| 138 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 139 | /// corresponding to the given implicit conversion kind. |
| 140 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 141 | static const ImplicitConversionRank |
| 142 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 143 | ICR_Exact_Match, |
| 144 | ICR_Exact_Match, |
| 145 | ICR_Exact_Match, |
| 146 | ICR_Exact_Match, |
| 147 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 148 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 149 | ICR_Promotion, |
| 150 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 151 | ICR_Promotion, |
| 152 | ICR_Conversion, |
| 153 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 154 | ICR_Conversion, |
| 155 | ICR_Conversion, |
| 156 | ICR_Conversion, |
| 157 | ICR_Conversion, |
| 158 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 159 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 160 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 161 | ICR_Conversion, |
| 162 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 163 | ICR_Complex_Real_Conversion, |
| 164 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 165 | ICR_Conversion, |
| 166 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 167 | }; |
| 168 | return Rank[(int)Kind]; |
| 169 | } |
| 170 | |
| 171 | /// GetImplicitConversionName - Return the name of this kind of |
| 172 | /// implicit conversion. |
| 173 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 174 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 175 | "No conversion", |
| 176 | "Lvalue-to-rvalue", |
| 177 | "Array-to-pointer", |
| 178 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 179 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 180 | "Qualification", |
| 181 | "Integral promotion", |
| 182 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 183 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 184 | "Integral conversion", |
| 185 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 186 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 187 | "Floating-integral conversion", |
| 188 | "Pointer conversion", |
| 189 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 190 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 191 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 192 | "Derived-to-base conversion", |
| 193 | "Vector conversion", |
| 194 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 195 | "Complex-real conversion", |
| 196 | "Block Pointer conversion", |
| 197 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 199 | }; |
| 200 | return Name[Kind]; |
| 201 | } |
| 202 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 203 | /// StandardConversionSequence - Set the standard conversion |
| 204 | /// sequence to the identity conversion. |
| 205 | void StandardConversionSequence::setAsIdentityConversion() { |
| 206 | First = ICK_Identity; |
| 207 | Second = ICK_Identity; |
| 208 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 209 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 210 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 211 | ReferenceBinding = false; |
| 212 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 213 | IsLvalueReference = true; |
| 214 | BindsToFunctionLvalue = false; |
| 215 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 216 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 217 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 218 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 222 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 223 | /// implicit conversions. |
| 224 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 225 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 226 | if (GetConversionRank(First) > Rank) |
| 227 | Rank = GetConversionRank(First); |
| 228 | if (GetConversionRank(Second) > Rank) |
| 229 | Rank = GetConversionRank(Second); |
| 230 | if (GetConversionRank(Third) > Rank) |
| 231 | Rank = GetConversionRank(Third); |
| 232 | return Rank; |
| 233 | } |
| 234 | |
| 235 | /// isPointerConversionToBool - Determines whether this conversion is |
| 236 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 238 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 240 | // Note that FromType has not necessarily been transformed by the |
| 241 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 242 | // check for their presence as well as checking whether FromType is |
| 243 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 244 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 245 | (getFromType()->isPointerType() || |
| 246 | getFromType()->isObjCObjectPointerType() || |
| 247 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 248 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 249 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 250 | return true; |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 255 | /// isPointerConversionToVoidPointer - Determines whether this |
| 256 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 257 | /// used as part of the ranking of standard conversion sequences (C++ |
| 258 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 260 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 262 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 263 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 264 | |
| 265 | // Note that FromType has not necessarily been transformed by the |
| 266 | // array-to-pointer implicit conversion, so check for its presence |
| 267 | // and redo the conversion to get a pointer. |
| 268 | if (First == ICK_Array_To_Pointer) |
| 269 | FromType = Context.getArrayDecayedType(FromType); |
| 270 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 271 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 272 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 273 | return ToPtrType->getPointeeType()->isVoidType(); |
| 274 | |
| 275 | return false; |
| 276 | } |
| 277 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 278 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 279 | /// or after one in an implicit conversion. |
| 280 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 281 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 282 | switch (ICE->getCastKind()) { |
| 283 | case CK_NoOp: |
| 284 | case CK_IntegralCast: |
| 285 | case CK_IntegralToBoolean: |
| 286 | case CK_IntegralToFloating: |
| 287 | case CK_FloatingToIntegral: |
| 288 | case CK_FloatingToBoolean: |
| 289 | case CK_FloatingCast: |
| 290 | Converted = ICE->getSubExpr(); |
| 291 | continue; |
| 292 | |
| 293 | default: |
| 294 | return Converted; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return Converted; |
| 299 | } |
| 300 | |
| 301 | /// Check if this standard conversion sequence represents a narrowing |
| 302 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 303 | /// |
| 304 | /// \param Ctx The AST context. |
| 305 | /// \param Converted The result of applying this standard conversion sequence. |
| 306 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 307 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 308 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 309 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 310 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 311 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 312 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 313 | APValue &ConstantValue, |
| 314 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 315 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 316 | |
| 317 | // C++11 [dcl.init.list]p7: |
| 318 | // A narrowing conversion is an implicit conversion ... |
| 319 | QualType FromType = getToType(0); |
| 320 | QualType ToType = getToType(1); |
| 321 | switch (Second) { |
| 322 | // -- from a floating-point type to an integer type, or |
| 323 | // |
| 324 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 325 | // type, except where the source is a constant expression and the actual |
| 326 | // value after conversion will fit into the target type and will produce |
| 327 | // the original value when converted back to the original type, or |
| 328 | case ICK_Floating_Integral: |
| 329 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 330 | return NK_Type_Narrowing; |
| 331 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 332 | llvm::APSInt IntConstantValue; |
| 333 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 334 | if (Initializer && |
| 335 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 336 | // Convert the integer to the floating type. |
| 337 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 338 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 339 | llvm::APFloat::rmNearestTiesToEven); |
| 340 | // And back. |
| 341 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 342 | bool ignored; |
| 343 | Result.convertToInteger(ConvertedValue, |
| 344 | llvm::APFloat::rmTowardZero, &ignored); |
| 345 | // If the resulting value is different, this was a narrowing conversion. |
| 346 | if (IntConstantValue != ConvertedValue) { |
| 347 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 348 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 349 | return NK_Constant_Narrowing; |
| 350 | } |
| 351 | } else { |
| 352 | // Variables are always narrowings. |
| 353 | return NK_Variable_Narrowing; |
| 354 | } |
| 355 | } |
| 356 | return NK_Not_Narrowing; |
| 357 | |
| 358 | // -- from long double to double or float, or from double to float, except |
| 359 | // where the source is a constant expression and the actual value after |
| 360 | // conversion is within the range of values that can be represented (even |
| 361 | // if it cannot be represented exactly), or |
| 362 | case ICK_Floating_Conversion: |
| 363 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 364 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 365 | // FromType is larger than ToType. |
| 366 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 367 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 368 | // Constant! |
| 369 | assert(ConstantValue.isFloat()); |
| 370 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 371 | // Convert the source value into the target type. |
| 372 | bool ignored; |
| 373 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 374 | Ctx.getFloatTypeSemantics(ToType), |
| 375 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 376 | // If there was no overflow, the source value is within the range of |
| 377 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 378 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 379 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 380 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 381 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 382 | } else { |
| 383 | return NK_Variable_Narrowing; |
| 384 | } |
| 385 | } |
| 386 | return NK_Not_Narrowing; |
| 387 | |
| 388 | // -- from an integer type or unscoped enumeration type to an integer type |
| 389 | // that cannot represent all the values of the original type, except where |
| 390 | // the source is a constant expression and the actual value after |
| 391 | // conversion will fit into the target type and will produce the original |
| 392 | // value when converted back to the original type. |
| 393 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 394 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 395 | // Boolean conversions can be from pointers and pointers to members |
| 396 | // [conv.bool], and those aren't considered narrowing conversions. |
| 397 | return NK_Not_Narrowing; |
| 398 | } // Otherwise, fall through to the integral case. |
| 399 | case ICK_Integral_Conversion: { |
| 400 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 401 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 402 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 403 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 404 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 405 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 406 | |
| 407 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 408 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 409 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 410 | // Not all values of FromType can be represented in ToType. |
| 411 | llvm::APSInt InitializerValue; |
| 412 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 413 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 414 | // Such conversions on variables are always narrowing. |
| 415 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 416 | } |
| 417 | bool Narrowing = false; |
| 418 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 419 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 420 | // narrowing. |
| 421 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 422 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 423 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 424 | // Add a bit to the InitializerValue so we don't have to worry about |
| 425 | // signed vs. unsigned comparisons. |
| 426 | InitializerValue = InitializerValue.extend( |
| 427 | InitializerValue.getBitWidth() + 1); |
| 428 | // Convert the initializer to and from the target width and signed-ness. |
| 429 | llvm::APSInt ConvertedValue = InitializerValue; |
| 430 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 431 | ConvertedValue.setIsSigned(ToSigned); |
| 432 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 433 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 434 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 435 | if (ConvertedValue != InitializerValue) |
| 436 | Narrowing = true; |
| 437 | } |
| 438 | if (Narrowing) { |
| 439 | ConstantType = Initializer->getType(); |
| 440 | ConstantValue = APValue(InitializerValue); |
| 441 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | return NK_Not_Narrowing; |
| 445 | } |
| 446 | |
| 447 | default: |
| 448 | // Other kinds of conversions are not narrowings. |
| 449 | return NK_Not_Narrowing; |
| 450 | } |
| 451 | } |
| 452 | |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 453 | /// dump - Print this standard conversion sequence to standard |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 454 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 455 | void StandardConversionSequence::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 456 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | bool PrintedSomething = false; |
| 458 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 459 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 460 | PrintedSomething = true; |
| 461 | } |
| 462 | |
| 463 | if (Second != ICK_Identity) { |
| 464 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 465 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 466 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 467 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 468 | |
| 469 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 470 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 471 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 472 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 473 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 474 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 475 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 476 | PrintedSomething = true; |
| 477 | } |
| 478 | |
| 479 | if (Third != ICK_Identity) { |
| 480 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 481 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 483 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 484 | PrintedSomething = true; |
| 485 | } |
| 486 | |
| 487 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 488 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 492 | /// dump - Print this user-defined conversion sequence to standard |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 493 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 494 | void UserDefinedConversionSequence::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 495 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 496 | if (Before.First || Before.Second || Before.Third) { |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 497 | Before.dump(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 498 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 499 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 500 | if (ConversionFunction) |
| 501 | OS << '\'' << *ConversionFunction << '\''; |
| 502 | else |
| 503 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 505 | OS << " -> "; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 506 | After.dump(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 510 | /// dump - Print this implicit conversion sequence to standard |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 511 | /// error. Useful for debugging overloading issues. |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 512 | void ImplicitConversionSequence::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 513 | raw_ostream &OS = llvm::errs(); |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 514 | if (isStdInitializerListElement()) |
| 515 | OS << "Worst std::initializer_list element conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | switch (ConversionKind) { |
| 517 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 518 | OS << "Standard conversion: "; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 519 | Standard.dump(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 520 | break; |
| 521 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 522 | OS << "User-defined conversion: "; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 523 | UserDefined.dump(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | break; |
| 525 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 526 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 528 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 529 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 530 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 531 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 532 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 536 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 537 | } |
| 538 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 539 | void AmbiguousConversionSequence::construct() { |
| 540 | new (&conversions()) ConversionSet(); |
| 541 | } |
| 542 | |
| 543 | void AmbiguousConversionSequence::destruct() { |
| 544 | conversions().~ConversionSet(); |
| 545 | } |
| 546 | |
| 547 | void |
| 548 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 549 | FromTypePtr = O.FromTypePtr; |
| 550 | ToTypePtr = O.ToTypePtr; |
| 551 | new (&conversions()) ConversionSet(O.conversions()); |
| 552 | } |
| 553 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 554 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 555 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 556 | // template argument information. |
| 557 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 558 | TemplateArgument FirstArg; |
| 559 | TemplateArgument SecondArg; |
| 560 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 561 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 562 | // template parameter and template argument information. |
| 563 | struct DFIParamWithArguments : DFIArguments { |
| 564 | TemplateParameter Param; |
| 565 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 566 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 568 | /// \brief Convert from Sema's representation of template deduction information |
| 569 | /// to the form used in overload-candidate information. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 570 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 571 | Sema::TemplateDeductionResult TDK, |
| 572 | TemplateDeductionInfo &Info) { |
| 573 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 574 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 575 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 576 | Result.Data = 0; |
| 577 | switch (TDK) { |
| 578 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 579 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 580 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 581 | case Sema::TDK_TooManyArguments: |
| 582 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 583 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 585 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 586 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 587 | Result.Data = Info.Param.getOpaqueValue(); |
| 588 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 589 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 590 | case Sema::TDK_NonDeducedMismatch: { |
| 591 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 592 | DFIArguments *Saved = new (Context) DFIArguments; |
| 593 | Saved->FirstArg = Info.FirstArg; |
| 594 | Saved->SecondArg = Info.SecondArg; |
| 595 | Result.Data = Saved; |
| 596 | break; |
| 597 | } |
| 598 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 599 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 600 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 601 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 602 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 603 | Saved->Param = Info.Param; |
| 604 | Saved->FirstArg = Info.FirstArg; |
| 605 | Saved->SecondArg = Info.SecondArg; |
| 606 | Result.Data = Saved; |
| 607 | break; |
| 608 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 610 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 611 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 612 | if (Info.hasSFINAEDiagnostic()) { |
| 613 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 614 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 615 | Info.takeSFINAEDiagnostic(*Diag); |
| 616 | Result.HasDiagnostic = true; |
| 617 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 618 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 619 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 620 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 621 | Result.Data = Info.Expression; |
| 622 | break; |
| 623 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 624 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 625 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 626 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 628 | return Result; |
| 629 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 630 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 631 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 632 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 633 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 634 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 635 | case Sema::TDK_InstantiationDepth: |
| 636 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 637 | case Sema::TDK_TooManyArguments: |
| 638 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 639 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 640 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 641 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 643 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 644 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 645 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 646 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 647 | Data = 0; |
| 648 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 649 | |
| 650 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 651 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 652 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 653 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 654 | Diag->~PartialDiagnosticAt(); |
| 655 | HasDiagnostic = false; |
| 656 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 657 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 659 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 660 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 664 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 665 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 666 | if (HasDiagnostic) |
| 667 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 668 | return 0; |
| 669 | } |
| 670 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 671 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 672 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 673 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 674 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 675 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 676 | case Sema::TDK_TooManyArguments: |
| 677 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 678 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 679 | case Sema::TDK_NonDeducedMismatch: |
| 680 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 681 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 683 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 684 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 685 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 686 | |
| 687 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 688 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 689 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 691 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 692 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 696 | return TemplateParameter(); |
| 697 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 698 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 699 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 700 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 701 | case Sema::TDK_Success: |
| 702 | case Sema::TDK_Invalid: |
| 703 | case Sema::TDK_InstantiationDepth: |
| 704 | case Sema::TDK_TooManyArguments: |
| 705 | case Sema::TDK_TooFewArguments: |
| 706 | case Sema::TDK_Incomplete: |
| 707 | case Sema::TDK_InvalidExplicitArguments: |
| 708 | case Sema::TDK_Inconsistent: |
| 709 | case Sema::TDK_Underqualified: |
| 710 | case Sema::TDK_NonDeducedMismatch: |
| 711 | case Sema::TDK_FailedOverloadResolution: |
| 712 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 713 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 714 | case Sema::TDK_SubstitutionFailure: |
| 715 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 717 | // Unhandled |
| 718 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 719 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 725 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 726 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 727 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 728 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 729 | case Sema::TDK_InstantiationDepth: |
| 730 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 731 | case Sema::TDK_TooManyArguments: |
| 732 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 733 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 734 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 735 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 736 | return 0; |
| 737 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 738 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 739 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 740 | case Sema::TDK_NonDeducedMismatch: |
| 741 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 743 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 744 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 748 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 750 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 751 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 752 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 753 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 754 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 755 | case Sema::TDK_InstantiationDepth: |
| 756 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 757 | case Sema::TDK_TooManyArguments: |
| 758 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 759 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 760 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 761 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 762 | return 0; |
| 763 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 764 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 765 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 766 | case Sema::TDK_NonDeducedMismatch: |
| 767 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 768 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 769 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 770 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 777 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 778 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 779 | Sema::TDK_FailedOverloadResolution) |
| 780 | return static_cast<Expr*>(Data); |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 785 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 786 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 787 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 788 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 789 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 790 | i->DeductionFailure.Destroy(); |
| 791 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | void OverloadCandidateSet::clear() { |
| 795 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 796 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 797 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 798 | Functions.clear(); |
| 799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 801 | namespace { |
| 802 | class UnbridgedCastsSet { |
| 803 | struct Entry { |
| 804 | Expr **Addr; |
| 805 | Expr *Saved; |
| 806 | }; |
| 807 | SmallVector<Entry, 2> Entries; |
| 808 | |
| 809 | public: |
| 810 | void save(Sema &S, Expr *&E) { |
| 811 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 812 | Entry entry = { &E, E }; |
| 813 | Entries.push_back(entry); |
| 814 | E = S.stripARCUnbridgedCast(E); |
| 815 | } |
| 816 | |
| 817 | void restore() { |
| 818 | for (SmallVectorImpl<Entry>::iterator |
| 819 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 820 | *i->Addr = i->Saved; |
| 821 | } |
| 822 | }; |
| 823 | } |
| 824 | |
| 825 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 826 | /// preprocessing on the given expression. |
| 827 | /// |
| 828 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 829 | /// without this, they will be immediately diagnosed as errors |
| 830 | /// |
| 831 | /// Return true on unrecoverable error. |
| 832 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 833 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 834 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 835 | // We can't handle overloaded expressions here because overload |
| 836 | // resolution might reasonably tweak them. |
| 837 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 838 | |
| 839 | // If the context potentially accepts unbridged ARC casts, strip |
| 840 | // the unbridged cast and add it to the collection for later restoration. |
| 841 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 842 | unbridgedCasts) { |
| 843 | unbridgedCasts->save(S, E); |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | // Go ahead and check everything else. |
| 848 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 849 | if (result.isInvalid()) |
| 850 | return true; |
| 851 | |
| 852 | E = result.take(); |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | // Nothing to do. |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 861 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 862 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 863 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 864 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 865 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 866 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 867 | return true; |
| 868 | |
| 869 | return false; |
| 870 | } |
| 871 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 872 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 873 | // overload of the declarations in Old. This routine returns false if |
| 874 | // New and Old cannot be overloaded, e.g., if New has the same |
| 875 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 876 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 877 | // it does return false, MatchedDecl will point to the decl that New |
| 878 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 879 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 880 | // |
| 881 | // Example: Given the following input: |
| 882 | // |
| 883 | // void f(int, float); // #1 |
| 884 | // void f(int, int); // #2 |
| 885 | // int f(int, int); // #3 |
| 886 | // |
| 887 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 889 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 890 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 891 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 892 | // (since they have different signatures), so this routine returns |
| 893 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 894 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 895 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 896 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 897 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 898 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 899 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 900 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 901 | // |
| 902 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 903 | // into a class by a using declaration. The rules for whether to hide |
| 904 | // shadow declarations ignore some properties which otherwise figure |
| 905 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 906 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 907 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 908 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 909 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 910 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 911 | NamedDecl *OldD = *I; |
| 912 | |
| 913 | bool OldIsUsingDecl = false; |
| 914 | if (isa<UsingShadowDecl>(OldD)) { |
| 915 | OldIsUsingDecl = true; |
| 916 | |
| 917 | // We can always introduce two using declarations into the same |
| 918 | // context, even if they have identical signatures. |
| 919 | if (NewIsUsingDecl) continue; |
| 920 | |
| 921 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 922 | } |
| 923 | |
| 924 | // If either declaration was introduced by a using declaration, |
| 925 | // we'll need to use slightly different rules for matching. |
| 926 | // Essentially, these rules are the normal rules, except that |
| 927 | // function templates hide function templates with different |
| 928 | // return types or template parameter lists. |
| 929 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 930 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 931 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 932 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 933 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 934 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 935 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 936 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 937 | continue; |
| 938 | } |
| 939 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 940 | Match = *I; |
| 941 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 942 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 943 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 944 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 945 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 946 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 947 | continue; |
| 948 | } |
| 949 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 950 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 951 | continue; |
| 952 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 953 | Match = *I; |
| 954 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 955 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 956 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 957 | // We can overload with these, which can show up when doing |
| 958 | // redeclaration checks for UsingDecls. |
| 959 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 960 | } else if (isa<TagDecl>(OldD)) { |
| 961 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 962 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 963 | // Optimistically assume that an unresolved using decl will |
| 964 | // overload; if it doesn't, we'll have to diagnose during |
| 965 | // template instantiation. |
| 966 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 967 | // (C++ 13p1): |
| 968 | // Only function declarations can be overloaded; object and type |
| 969 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 970 | Match = *I; |
| 971 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 973 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 974 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 975 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 978 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 979 | bool UseUsingDeclRules) { |
| 980 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 981 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 982 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 983 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 984 | // MSVCRT user defined entry points cannot be overloaded. |
| 985 | if (New->isMSVCRTEntryPoint()) |
| 986 | return false; |
| 987 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 988 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 989 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 990 | |
| 991 | // C++ [temp.fct]p2: |
| 992 | // A function template can be overloaded with other function templates |
| 993 | // and with normal (non-template) functions. |
| 994 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 995 | return true; |
| 996 | |
| 997 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 998 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 999 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 1002 | // determine whether they are overloads. If we find any mismatch |
| 1003 | // in the signature, they are overloads. |
| 1004 | |
| 1005 | // If either of these functions is a K&R-style function (no |
| 1006 | // prototype), then we consider them to have matching signatures. |
| 1007 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1008 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1009 | return false; |
| 1010 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1011 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1012 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1013 | |
| 1014 | // The signature of a function includes the types of its |
| 1015 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1016 | // of the ellipsis; see C++ DR 357). |
| 1017 | if (OldQType != NewQType && |
| 1018 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1019 | OldType->isVariadic() != NewType->isVariadic() || |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1020 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1021 | return true; |
| 1022 | |
| 1023 | // C++ [temp.over.link]p4: |
| 1024 | // The signature of a function template consists of its function |
| 1025 | // signature, its return type and its template parameter list. The names |
| 1026 | // of the template parameters are significant only for establishing the |
| 1027 | // relationship between the template parameters and the rest of the |
| 1028 | // signature. |
| 1029 | // |
| 1030 | // We check the return type and template parameter lists for function |
| 1031 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1032 | // |
| 1033 | // However, we don't consider either of these when deciding whether |
| 1034 | // a member introduced by a shadow declaration is hidden. |
| 1035 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1036 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1037 | OldTemplate->getTemplateParameters(), |
| 1038 | false, TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1039 | OldType->getResultType() != NewType->getResultType())) |
| 1040 | return true; |
| 1041 | |
| 1042 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1043 | // 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] | 1044 | // |
| 1045 | // As part of this, also check whether one of the member functions |
| 1046 | // is static, in which case they are not overloads (C++ |
| 1047 | // 13.1p2). While not part of the definition of the signature, |
| 1048 | // this check is important to determine whether these functions |
| 1049 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1050 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1051 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1052 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1053 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1054 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1055 | if (!UseUsingDeclRules && |
| 1056 | (OldMethod->getRefQualifier() == RQ_None || |
| 1057 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1058 | // C++0x [over.load]p2: |
| 1059 | // - Member function declarations with the same name and the same |
| 1060 | // parameter-type-list as well as member function template |
| 1061 | // declarations with the same name, the same parameter-type-list, and |
| 1062 | // the same template parameter lists cannot be overloaded if any of |
| 1063 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1064 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1065 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1066 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1067 | } |
| 1068 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1069 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1070 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1071 | // We may not have applied the implicit const for a constexpr member |
| 1072 | // function yet (because we haven't yet resolved whether this is a static |
| 1073 | // or non-static member function). Add it now, on the assumption that this |
| 1074 | // is a redeclaration of OldMethod. |
David Majnemer | 62e9370 | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1075 | unsigned OldQuals = OldMethod->getTypeQualifiers(); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1076 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1077 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1078 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1079 | NewQuals |= Qualifiers::Const; |
David Majnemer | 62e9370 | 2013-11-03 23:51:28 +0000 | [diff] [blame] | 1080 | |
| 1081 | // We do not allow overloading based off of '__restrict'. |
| 1082 | OldQuals &= ~Qualifiers::Restrict; |
| 1083 | NewQuals &= ~Qualifiers::Restrict; |
| 1084 | if (OldQuals != NewQuals) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1085 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1086 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1087 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1088 | // The signatures match; this is not an overload. |
| 1089 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1092 | /// \brief Checks availability of the function depending on the current |
| 1093 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1094 | /// |
| 1095 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1096 | /// an available function, false otherwise. |
| 1097 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1098 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1099 | } |
| 1100 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1101 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1102 | /// |
| 1103 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1104 | /// is not an option. See TryImplicitConversion for more information. |
| 1105 | static ImplicitConversionSequence |
| 1106 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1107 | bool SuppressUserConversions, |
| 1108 | bool AllowExplicit, |
| 1109 | bool InOverloadResolution, |
| 1110 | bool CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1111 | bool AllowObjCWritebackConversion, |
| 1112 | bool AllowObjCConversionOnExplicit) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1113 | ImplicitConversionSequence ICS; |
| 1114 | |
| 1115 | if (SuppressUserConversions) { |
| 1116 | // We're not in the case above, so there is no conversion that |
| 1117 | // we can perform. |
| 1118 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1119 | return ICS; |
| 1120 | } |
| 1121 | |
| 1122 | // Attempt user-defined conversion. |
| 1123 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1124 | OverloadingResult UserDefResult |
| 1125 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1126 | AllowExplicit, AllowObjCConversionOnExplicit); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (UserDefResult == OR_Success) { |
| 1129 | ICS.setUserDefined(); |
| 1130 | // C++ [over.ics.user]p4: |
| 1131 | // A conversion of an expression of class type to the same class |
| 1132 | // type is given Exact Match rank, and a conversion of an |
| 1133 | // expression of class type to a base class of that type is |
| 1134 | // given Conversion rank, in spite of the fact that a copy |
| 1135 | // constructor (i.e., a user-defined conversion function) is |
| 1136 | // called for those cases. |
| 1137 | if (CXXConstructorDecl *Constructor |
| 1138 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1139 | QualType FromCanon |
| 1140 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1141 | QualType ToCanon |
| 1142 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1143 | if (Constructor->isCopyConstructor() && |
| 1144 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1145 | // Turn this into a "standard" conversion sequence, so that it |
| 1146 | // gets ranked with standard conversion sequences. |
| 1147 | ICS.setStandard(); |
| 1148 | ICS.Standard.setAsIdentityConversion(); |
| 1149 | ICS.Standard.setFromType(From->getType()); |
| 1150 | ICS.Standard.setAllToTypes(ToType); |
| 1151 | ICS.Standard.CopyConstructor = Constructor; |
| 1152 | if (ToCanon != FromCanon) |
| 1153 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | // C++ [over.best.ics]p4: |
| 1158 | // However, when considering the argument of a user-defined |
| 1159 | // conversion function that is a candidate by 13.3.1.3 when |
| 1160 | // invoked for the copying of the temporary in the second step |
| 1161 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1162 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1163 | // ellipsis conversion sequences are allowed. |
| 1164 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1165 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1166 | } |
| 1167 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1168 | ICS.setAmbiguous(); |
| 1169 | ICS.Ambiguous.setFromType(From->getType()); |
| 1170 | ICS.Ambiguous.setToType(ToType); |
| 1171 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1172 | Cand != Conversions.end(); ++Cand) |
| 1173 | if (Cand->Viable) |
| 1174 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1175 | } else { |
| 1176 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1177 | } |
| 1178 | |
| 1179 | return ICS; |
| 1180 | } |
| 1181 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1182 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1183 | /// from the given expression (Expr) to the given type (ToType). This |
| 1184 | /// function returns an implicit conversion sequence that can be used |
| 1185 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1186 | /// |
| 1187 | /// void f(float f); |
| 1188 | /// void g(int i) { f(i); } |
| 1189 | /// |
| 1190 | /// this routine would produce an implicit conversion sequence to |
| 1191 | /// describe the initialization of f from i, which will be a standard |
| 1192 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1193 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1194 | // |
| 1195 | /// Note that this routine only determines how the conversion can be |
| 1196 | /// performed; it does not actually perform the conversion. As such, |
| 1197 | /// it will not produce any diagnostics if no conversion is available, |
| 1198 | /// but will instead return an implicit conversion sequence of kind |
| 1199 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1200 | /// |
| 1201 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1202 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1203 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1204 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1205 | /// |
| 1206 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1207 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1208 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1209 | static ImplicitConversionSequence |
| 1210 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1211 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1212 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1213 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1214 | bool CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1215 | bool AllowObjCWritebackConversion, |
| 1216 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1217 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1218 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1219 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1220 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1221 | return ICS; |
| 1222 | } |
| 1223 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1224 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1225 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1226 | return ICS; |
| 1227 | } |
| 1228 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1229 | // C++ [over.ics.user]p4: |
| 1230 | // A conversion of an expression of class type to the same class |
| 1231 | // type is given Exact Match rank, and a conversion of an |
| 1232 | // expression of class type to a base class of that type is |
| 1233 | // given Conversion rank, in spite of the fact that a copy/move |
| 1234 | // constructor (i.e., a user-defined conversion function) is |
| 1235 | // called for those cases. |
| 1236 | QualType FromType = From->getType(); |
| 1237 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1238 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1239 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1240 | ICS.setStandard(); |
| 1241 | ICS.Standard.setAsIdentityConversion(); |
| 1242 | ICS.Standard.setFromType(FromType); |
| 1243 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1245 | // We don't actually check at this point whether there is a valid |
| 1246 | // copy/move constructor, since overloading just assumes that it |
| 1247 | // exists. When we actually perform initialization, we'll find the |
| 1248 | // appropriate constructor to copy the returned object, if needed. |
| 1249 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1250 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1251 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1252 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1253 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1254 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1255 | return ICS; |
| 1256 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1257 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1258 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1259 | AllowExplicit, InOverloadResolution, CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1260 | AllowObjCWritebackConversion, |
| 1261 | AllowObjCConversionOnExplicit); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1264 | ImplicitConversionSequence |
| 1265 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1266 | bool SuppressUserConversions, |
| 1267 | bool AllowExplicit, |
| 1268 | bool InOverloadResolution, |
| 1269 | bool CStyle, |
| 1270 | bool AllowObjCWritebackConversion) { |
| 1271 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1272 | SuppressUserConversions, AllowExplicit, |
| 1273 | InOverloadResolution, CStyle, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1274 | AllowObjCWritebackConversion, |
| 1275 | /*AllowObjCConversionOnExplicit=*/false); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1278 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1279 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1280 | /// converted expression. Flavor is the kind of conversion we're |
| 1281 | /// performing, used in the error message. If @p AllowExplicit, |
| 1282 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1283 | ExprResult |
| 1284 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1285 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1286 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1287 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1290 | ExprResult |
| 1291 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1292 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1293 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1294 | if (checkPlaceholderForOverload(*this, From)) |
| 1295 | return ExprError(); |
| 1296 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1297 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1298 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1299 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1300 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1301 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1302 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1303 | /*SuppressUserConversions=*/false, |
| 1304 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1305 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1306 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 1307 | AllowObjCWritebackConversion, |
| 1308 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1309 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1310 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1311 | |
| 1312 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1313 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1314 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1315 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1316 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1317 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1318 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1319 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1320 | // where F adds one of the following at most once: |
| 1321 | // - a pointer |
| 1322 | // - a member pointer |
| 1323 | // - a block pointer |
| 1324 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1325 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1326 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1327 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1328 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1329 | if (TyClass == Type::Pointer) { |
| 1330 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1331 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1332 | } else if (TyClass == Type::BlockPointer) { |
| 1333 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1334 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1335 | } else if (TyClass == Type::MemberPointer) { |
| 1336 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1337 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1338 | } else { |
| 1339 | return false; |
| 1340 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1341 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1342 | TyClass = CanTo->getTypeClass(); |
| 1343 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1344 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
| 1348 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1349 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1350 | if (!EInfo.getNoReturn()) return false; |
| 1351 | |
| 1352 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1353 | assert(QualType(FromFn, 0).isCanonical()); |
| 1354 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1355 | |
| 1356 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1357 | return true; |
| 1358 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1359 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1360 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1361 | /// vector conversion. |
| 1362 | /// |
| 1363 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1364 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1365 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1366 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1367 | // We need at least one of these types to be a vector type to have a vector |
| 1368 | // conversion. |
| 1369 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1370 | return false; |
| 1371 | |
| 1372 | // Identical types require no conversions. |
| 1373 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1374 | return false; |
| 1375 | |
| 1376 | // There are no conversions between extended vector types, only identity. |
| 1377 | if (ToType->isExtVectorType()) { |
| 1378 | // There are no conversions between extended vector types other than the |
| 1379 | // identity conversion. |
| 1380 | if (FromType->isExtVectorType()) |
| 1381 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1382 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1383 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1384 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1385 | ICK = ICK_Vector_Splat; |
| 1386 | return true; |
| 1387 | } |
| 1388 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1389 | |
| 1390 | // We can perform the conversion between vector types in the following cases: |
| 1391 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1392 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1393 | // same size |
| 1394 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1395 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1396 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1397 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1398 | ICK = ICK_Vector_Conversion; |
| 1399 | return true; |
| 1400 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1401 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1403 | return false; |
| 1404 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1406 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1407 | bool InOverloadResolution, |
| 1408 | StandardConversionSequence &SCS, |
| 1409 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1411 | /// IsStandardConversion - Determines whether there is a standard |
| 1412 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1413 | /// expression From to the type ToType. Standard conversion sequences |
| 1414 | /// only consider non-class types; for conversions that involve class |
| 1415 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1416 | /// contain the standard conversion sequence required to perform this |
| 1417 | /// conversion and this routine will return true. Otherwise, this |
| 1418 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1419 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1420 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1421 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1422 | bool CStyle, |
| 1423 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1424 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1425 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1426 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1427 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1428 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1429 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1430 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1431 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1432 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1433 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1435 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1436 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1442 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1443 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1444 | // (C++ 4p1). |
| 1445 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1446 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1447 | DeclAccessPair AccessPair; |
| 1448 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1450 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1451 | // We were able to resolve the address of the overloaded function, |
| 1452 | // so we can convert to the type of that function. |
| 1453 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1454 | |
| 1455 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1456 | // if the type matches (identity) or we are converting to bool |
| 1457 | if (!S.Context.hasSameUnqualifiedType( |
| 1458 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1459 | QualType resultTy; |
| 1460 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1461 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1462 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1463 | // otherwise, only a boolean conversion is standard |
| 1464 | if (!ToType->isBooleanType()) |
| 1465 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1467 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1468 | // Check if the "from" expression is taking the address of an overloaded |
| 1469 | // function and recompute the FromType accordingly. Take advantage of the |
| 1470 | // fact that non-static member functions *must* have such an address-of |
| 1471 | // expression. |
| 1472 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1473 | if (Method && !Method->isStatic()) { |
| 1474 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1475 | "Non-unary operator on non-static member address"); |
| 1476 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1477 | == UO_AddrOf && |
| 1478 | "Non-address-of operator on non-static member address"); |
| 1479 | const Type *ClassType |
| 1480 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1481 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1482 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1483 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1484 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1485 | "Non-address-of operator for overloaded function expression"); |
| 1486 | FromType = S.Context.getPointerType(FromType); |
| 1487 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1488 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1489 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1490 | assert(S.Context.hasSameType( |
| 1491 | FromType, |
| 1492 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1493 | } else { |
| 1494 | return false; |
| 1495 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1496 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1497 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1498 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1499 | // be converted to a prvalue. |
| 1500 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1501 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1502 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1503 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1504 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1505 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1506 | // C11 6.3.2.1p2: |
| 1507 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1508 | // of the type of the lvalue ... |
| 1509 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1510 | FromType = Atomic->getValueType(); |
| 1511 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1512 | // If T is a non-class type, the type of the rvalue is the |
| 1513 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1514 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1515 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1516 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1517 | } else if (FromType->isArrayType()) { |
| 1518 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1519 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1520 | |
| 1521 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1522 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1523 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1524 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1525 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1526 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1527 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1528 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1529 | |
| 1530 | // For the purpose of ranking in overload resolution |
| 1531 | // (13.3.3.1.1), this conversion is considered an |
| 1532 | // array-to-pointer conversion followed by a qualification |
| 1533 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1534 | SCS.Second = ICK_Identity; |
| 1535 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1536 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1537 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1538 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1539 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1540 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1541 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1542 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1543 | |
| 1544 | // An lvalue of function type T can be converted to an rvalue of |
| 1545 | // type "pointer to T." The result is a pointer to the |
| 1546 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1547 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1548 | } else { |
| 1549 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1550 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1551 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1552 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1553 | |
| 1554 | // The second conversion can be an integral promotion, floating |
| 1555 | // point promotion, integral conversion, floating point conversion, |
| 1556 | // floating-integral conversion, pointer conversion, |
| 1557 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1558 | // For overloading in C, this can also be a "compatible-type" |
| 1559 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1560 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1561 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1562 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1563 | // The unqualified versions of the types are the same: there's no |
| 1564 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1565 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1566 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1568 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1569 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1570 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1571 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1572 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1573 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1574 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1575 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1576 | SCS.Second = ICK_Complex_Promotion; |
| 1577 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1578 | } else if (ToType->isBooleanType() && |
| 1579 | (FromType->isArithmeticType() || |
| 1580 | FromType->isAnyPointerType() || |
| 1581 | FromType->isBlockPointerType() || |
| 1582 | FromType->isMemberPointerType() || |
| 1583 | FromType->isNullPtrType())) { |
| 1584 | // Boolean conversions (C++ 4.12). |
| 1585 | SCS.Second = ICK_Boolean_Conversion; |
| 1586 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1587 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1588 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1589 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1590 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1591 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1592 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1593 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1594 | SCS.Second = ICK_Complex_Conversion; |
| 1595 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1596 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1597 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1598 | // Complex-real conversions (C99 6.3.1.7) |
| 1599 | SCS.Second = ICK_Complex_Real; |
| 1600 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1601 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1602 | // Floating point conversions (C++ 4.8). |
| 1603 | SCS.Second = ICK_Floating_Conversion; |
| 1604 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1605 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1606 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1607 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1608 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1609 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1610 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1611 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1612 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1613 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1614 | } else if (AllowObjCWritebackConversion && |
| 1615 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1616 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1617 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1618 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1619 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1620 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1621 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1622 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1623 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1624 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1625 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1626 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1627 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1628 | SCS.Second = SecondICK; |
| 1629 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1630 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1631 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1632 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1633 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1634 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1635 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1636 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1637 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1638 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1639 | InOverloadResolution, |
| 1640 | SCS, CStyle)) { |
| 1641 | SCS.Second = ICK_TransparentUnionConversion; |
| 1642 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1643 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1644 | CStyle)) { |
| 1645 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1646 | // appropriately. |
| 1647 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1648 | } else if (ToType->isEventT() && |
| 1649 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1650 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1651 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1652 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1653 | } else { |
| 1654 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1655 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1656 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1657 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1659 | QualType CanonFrom; |
| 1660 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1661 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1662 | bool ObjCLifetimeConversion; |
| 1663 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1664 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1665 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1666 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1667 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1668 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1669 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1670 | } else { |
| 1671 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1672 | SCS.Third = ICK_Identity; |
| 1673 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1675 | // [...] Any difference in top-level cv-qualification is |
| 1676 | // subsumed by the initialization itself and does not constitute |
| 1677 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1678 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1679 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1680 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1681 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1682 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1683 | FromType = ToType; |
| 1684 | CanonFrom = CanonTo; |
| 1685 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1686 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1687 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1688 | |
| 1689 | // If we have not converted the argument type to the parameter type, |
| 1690 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1691 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1692 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1694 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1695 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1696 | |
| 1697 | static bool |
| 1698 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1699 | QualType &ToType, |
| 1700 | bool InOverloadResolution, |
| 1701 | StandardConversionSequence &SCS, |
| 1702 | bool CStyle) { |
| 1703 | |
| 1704 | const RecordType *UT = ToType->getAsUnionType(); |
| 1705 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1706 | return false; |
| 1707 | // The field to initialize within the transparent union. |
| 1708 | RecordDecl *UD = UT->getDecl(); |
| 1709 | // It's compatible if the expression matches any of the fields. |
| 1710 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1711 | itend = UD->field_end(); |
| 1712 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1713 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1714 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1715 | ToType = it->getType(); |
| 1716 | return true; |
| 1717 | } |
| 1718 | } |
| 1719 | return false; |
| 1720 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1721 | |
| 1722 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1723 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1724 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1725 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1727 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1728 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1729 | if (!To) { |
| 1730 | return false; |
| 1731 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1732 | |
| 1733 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1734 | // unsigned short int can be converted to an rvalue of type int if |
| 1735 | // int can represent all the values of the source type; otherwise, |
| 1736 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1737 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1738 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1739 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1740 | if (// We can promote any signed, promotable integer type to an int |
| 1741 | (FromType->isSignedIntegerType() || |
| 1742 | // We can promote any unsigned integer type whose size is |
| 1743 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1745 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1746 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1749 | return To->getKind() == BuiltinType::UInt; |
| 1750 | } |
| 1751 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1752 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1753 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1754 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1755 | // following types that can represent all the values of the enumeration |
| 1756 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1757 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1758 | // 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] | 1759 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1760 | // 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] | 1761 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1762 | // 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] | 1763 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1764 | // C++11 [conv.prom]p4: |
| 1765 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1766 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1767 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1768 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1769 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1770 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1771 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1772 | // provided for a scoped enumeration. |
| 1773 | if (FromEnumType->getDecl()->isScoped()) |
| 1774 | return false; |
| 1775 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1776 | // We can perform an integral promotion to the underlying type of the enum, |
| 1777 | // even if that's not the promoted type. |
| 1778 | if (FromEnumType->getDecl()->isFixed()) { |
| 1779 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1780 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1781 | IsIntegralPromotion(From, Underlying, ToType); |
| 1782 | } |
| 1783 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1784 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1785 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1786 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1787 | return Context.hasSameUnqualifiedType(ToType, |
| 1788 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1789 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1790 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1791 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1792 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1793 | // to an rvalue a prvalue of the first of the following types that can |
| 1794 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1795 | // 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] | 1796 | // 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] | 1797 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1798 | // 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] | 1799 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1800 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1801 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1802 | // Determine whether the type we're converting from is signed or |
| 1803 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1804 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1805 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1807 | // The types we'll try to promote to, in the appropriate |
| 1808 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | QualType PromoteTypes[6] = { |
| 1810 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1811 | Context.LongTy, Context.UnsignedLongTy , |
| 1812 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1813 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1814 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1815 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1816 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1818 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1819 | // We found the type that we can promote to. If this is the |
| 1820 | // type we wanted, we have a promotion. Otherwise, no |
| 1821 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1822 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1828 | // rvalue of type int if int can represent all the values of the |
| 1829 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1830 | // unsigned int can represent all the values of the bit-field. If |
| 1831 | // the bit-field is larger yet, no integral promotion applies to |
| 1832 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1833 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1834 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1835 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1836 | using llvm::APSInt; |
| 1837 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1838 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1839 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1840 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1841 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1842 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1843 | ToSize = Context.getTypeSize(ToType); |
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 int from a bitfield that fits in an int? |
| 1846 | if (BitWidth < ToSize || |
| 1847 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1848 | return To->getKind() == BuiltinType::Int; |
| 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 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1852 | // that fits into an unsigned int? |
| 1853 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1854 | return To->getKind() == BuiltinType::UInt; |
| 1855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1857 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1858 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1859 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1861 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1862 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1863 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1864 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1865 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1866 | |
| 1867 | return false; |
| 1868 | } |
| 1869 | |
| 1870 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1871 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1872 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1874 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1875 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1876 | /// An rvalue of type float can be converted to an rvalue of type |
| 1877 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1878 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1879 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1880 | return true; |
| 1881 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1882 | // C99 6.3.1.5p1: |
| 1883 | // When a float is promoted to double or long double, or a |
| 1884 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1885 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1886 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1887 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1888 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1889 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1890 | |
| 1891 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1892 | if (!getLangOpts().NativeHalfType && |
| 1893 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1894 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1895 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1898 | return false; |
| 1899 | } |
| 1900 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1901 | /// \brief Determine if a conversion is a complex promotion. |
| 1902 | /// |
| 1903 | /// A complex promotion is defined as a complex -> complex conversion |
| 1904 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1905 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1906 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1907 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1908 | if (!FromComplex) |
| 1909 | return false; |
| 1910 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1911 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1912 | if (!ToComplex) |
| 1913 | return false; |
| 1914 | |
| 1915 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1916 | ToComplex->getElementType()) || |
| 1917 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1918 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1921 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1922 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1923 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1924 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1925 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1926 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1928 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1929 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1930 | ASTContext &Context, |
| 1931 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1932 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1933 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1934 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1935 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1936 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1937 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1938 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1939 | |
| 1940 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1941 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1942 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1943 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1945 | if (StripObjCLifetime) |
| 1946 | Quals.removeObjCLifetime(); |
| 1947 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1949 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1950 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1951 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1952 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1955 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1956 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1957 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1958 | return Context.getPointerType(ToPointee); |
| 1959 | } |
| 1960 | |
| 1961 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1962 | QualType QualifiedCanonToPointee |
| 1963 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1965 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1966 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1967 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1968 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1969 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1971 | bool InOverloadResolution, |
| 1972 | ASTContext &Context) { |
| 1973 | // Handle value-dependent integral null pointer constants correctly. |
| 1974 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1975 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1976 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1977 | return !InOverloadResolution; |
| 1978 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1979 | return Expr->isNullPointerConstant(Context, |
| 1980 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1981 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1982 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1984 | /// IsPointerConversion - Determines whether the conversion of the |
| 1985 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1986 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1987 | /// 4.10). If so, returns true and places the converted type (that |
| 1988 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1989 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1990 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1991 | /// This routine also supports conversions to and from block pointers |
| 1992 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1993 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1994 | /// appropriate overloading rules for Objective-C, we may want to |
| 1995 | /// split the Objective-C checks into a different routine; however, |
| 1996 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1997 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1998 | /// set if the conversion is an allowed Objective-C conversion that |
| 1999 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2000 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2001 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2002 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2004 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2005 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 2006 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2007 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2008 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 2010 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2011 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 2012 | ConvertedType = ToType; |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2016 | // Blocks: Block pointers can be converted to void*. |
| 2017 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2018 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2019 | ConvertedType = ToType; |
| 2020 | return true; |
| 2021 | } |
| 2022 | // Blocks: A null pointer constant can be converted to a block |
| 2023 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2025 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2026 | ConvertedType = ToType; |
| 2027 | return true; |
| 2028 | } |
| 2029 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2030 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2031 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2033 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2034 | ConvertedType = ToType; |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2038 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2039 | if (!ToTypePtr) |
| 2040 | return false; |
| 2041 | |
| 2042 | // 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] | 2043 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2044 | ConvertedType = ToType; |
| 2045 | return true; |
| 2046 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2047 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2048 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2049 | // , including objective-c pointers. |
| 2050 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2051 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2052 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2053 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2054 | FromType->getAs<ObjCObjectPointerType>(), |
| 2055 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2056 | ToType, Context); |
| 2057 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2058 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2059 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2060 | if (!FromTypePtr) |
| 2061 | return false; |
| 2062 | |
| 2063 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2064 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2065 | // 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] | 2066 | // pointer conversion, so don't do all of the work below. |
| 2067 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2068 | return false; |
| 2069 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2070 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2071 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2072 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2073 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2074 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2076 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2077 | ToType, Context, |
| 2078 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2079 | return true; |
| 2080 | } |
| 2081 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2082 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2083 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2084 | ToPointeeType->isVoidType()) { |
| 2085 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2086 | ToPointeeType, |
| 2087 | ToType, Context); |
| 2088 | return true; |
| 2089 | } |
| 2090 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2091 | // When we're overloading in C, we allow a special kind of pointer |
| 2092 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2093 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2094 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2096 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2098 | return true; |
| 2099 | } |
| 2100 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2101 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2103 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2104 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2105 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2106 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2107 | // necessitates this conversion is ill-formed. The result of the |
| 2108 | // conversion is a pointer to the base class sub-object of the |
| 2109 | // derived class object. The null pointer value is converted to |
| 2110 | // the null pointer value of the destination type. |
| 2111 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2112 | // Note that we do not check for ambiguity or inaccessibility |
| 2113 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2114 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2115 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2116 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2117 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2118 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2120 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2121 | ToType, Context); |
| 2122 | return true; |
| 2123 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2124 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2125 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2126 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2127 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2128 | ToPointeeType, |
| 2129 | ToType, Context); |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2133 | return false; |
| 2134 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2135 | |
| 2136 | /// \brief Adopt the given qualifiers for the given type. |
| 2137 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2138 | Qualifiers TQs = T.getQualifiers(); |
| 2139 | |
| 2140 | // Check whether qualifiers already match. |
| 2141 | if (TQs == Qs) |
| 2142 | return T; |
| 2143 | |
| 2144 | if (Qs.compatiblyIncludes(TQs)) |
| 2145 | return Context.getQualifiedType(T, Qs); |
| 2146 | |
| 2147 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2148 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2149 | |
| 2150 | /// isObjCPointerConversion - Determines whether this is an |
| 2151 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2152 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2154 | QualType& ConvertedType, |
| 2155 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2156 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2157 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2159 | // The set of qualifiers on the type we're converting from. |
| 2160 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2161 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2162 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2163 | const ObjCObjectPointerType* ToObjCPtr = |
| 2164 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2166 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2167 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2168 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2169 | // If the pointee types are the same (ignoring qualifications), |
| 2170 | // then this is not a pointer conversion. |
| 2171 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2172 | FromObjCPtr->getPointeeType())) |
| 2173 | return false; |
| 2174 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2175 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2176 | // 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] | 2177 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2178 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2179 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2180 | return true; |
| 2181 | } |
| 2182 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2184 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2186 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2187 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2188 | return true; |
| 2189 | } |
| 2190 | // Objective C++: We're able to convert from a pointer to an |
| 2191 | // interface to a pointer to a different interface. |
| 2192 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2193 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2194 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2195 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2196 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2197 | FromObjCPtr->getPointeeType())) |
| 2198 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2199 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2200 | ToObjCPtr->getPointeeType(), |
| 2201 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2202 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2203 | return true; |
| 2204 | } |
| 2205 | |
| 2206 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2207 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2208 | // interfaces, which is permitted. However, we're going to |
| 2209 | // complain about it. |
| 2210 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2211 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2212 | ToObjCPtr->getPointeeType(), |
| 2213 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2214 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2215 | return true; |
| 2216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2218 | // 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] | 2219 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2220 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2221 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2222 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2223 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2224 | // 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] | 2225 | // to a block pointer type. |
| 2226 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2227 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2228 | return true; |
| 2229 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2230 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2231 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2232 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2233 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2234 | // 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] | 2235 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2236 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2237 | return true; |
| 2238 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2239 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2240 | return false; |
| 2241 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2242 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2243 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2244 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2245 | else if (const BlockPointerType *FromBlockPtr = |
| 2246 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2247 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2248 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2249 | return false; |
| 2250 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2251 | // If we have pointers to pointers, recursively check whether this |
| 2252 | // is an Objective-C conversion. |
| 2253 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2254 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2255 | IncompatibleObjC)) { |
| 2256 | // We always complain about this conversion. |
| 2257 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2258 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2259 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2260 | return true; |
| 2261 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2262 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2263 | // as in I* to id. |
| 2264 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2265 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2266 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2267 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2268 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2269 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2270 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2271 | return true; |
| 2272 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2273 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2274 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2275 | // differences in the argument and result types are in Objective-C |
| 2276 | // pointer conversions. If so, we permit the conversion (but |
| 2277 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2279 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2280 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2281 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2282 | if (FromFunctionType && ToFunctionType) { |
| 2283 | // If the function types are exactly the same, this isn't an |
| 2284 | // Objective-C pointer conversion. |
| 2285 | if (Context.getCanonicalType(FromPointeeType) |
| 2286 | == Context.getCanonicalType(ToPointeeType)) |
| 2287 | return false; |
| 2288 | |
| 2289 | // Perform the quick checks that will tell us whether these |
| 2290 | // function types are obviously different. |
| 2291 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2292 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2293 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2294 | return false; |
| 2295 | |
| 2296 | bool HasObjCConversion = false; |
| 2297 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2298 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2299 | // Okay, the types match exactly. Nothing to do. |
| 2300 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2301 | ToFunctionType->getResultType(), |
| 2302 | ConvertedType, IncompatibleObjC)) { |
| 2303 | // Okay, we have an Objective-C pointer conversion. |
| 2304 | HasObjCConversion = true; |
| 2305 | } else { |
| 2306 | // Function types are too different. Abort. |
| 2307 | return false; |
| 2308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2310 | // Check argument types. |
| 2311 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2312 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2313 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2314 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2315 | if (Context.getCanonicalType(FromArgType) |
| 2316 | == Context.getCanonicalType(ToArgType)) { |
| 2317 | // Okay, the types match exactly. Nothing to do. |
| 2318 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2319 | ConvertedType, IncompatibleObjC)) { |
| 2320 | // Okay, we have an Objective-C pointer conversion. |
| 2321 | HasObjCConversion = true; |
| 2322 | } else { |
| 2323 | // Argument types are too different. Abort. |
| 2324 | return false; |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | if (HasObjCConversion) { |
| 2329 | // We had an Objective-C conversion. Allow this pointer |
| 2330 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2331 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2332 | IncompatibleObjC = true; |
| 2333 | return true; |
| 2334 | } |
| 2335 | } |
| 2336 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2337 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2339 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2340 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2341 | /// used for parameter passing when performing automatic reference counting. |
| 2342 | /// |
| 2343 | /// \param FromType The type we're converting form. |
| 2344 | /// |
| 2345 | /// \param ToType The type we're converting to. |
| 2346 | /// |
| 2347 | /// \param ConvertedType The type that will be produced after applying |
| 2348 | /// this conversion. |
| 2349 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2350 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2351 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2352 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2353 | return false; |
| 2354 | |
| 2355 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2356 | QualType ToPointee; |
| 2357 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2358 | ToPointee = ToPointer->getPointeeType(); |
| 2359 | else |
| 2360 | return false; |
| 2361 | |
| 2362 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2363 | if (!ToPointee->isObjCLifetimeType() || |
| 2364 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2365 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2366 | return false; |
| 2367 | |
| 2368 | // Argument must be a pointer to __strong to __weak. |
| 2369 | QualType FromPointee; |
| 2370 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2371 | FromPointee = FromPointer->getPointeeType(); |
| 2372 | else |
| 2373 | return false; |
| 2374 | |
| 2375 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2376 | if (!FromPointee->isObjCLifetimeType() || |
| 2377 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2378 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2379 | return false; |
| 2380 | |
| 2381 | // Make sure that we have compatible qualifiers. |
| 2382 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2383 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2384 | return false; |
| 2385 | |
| 2386 | // Remove qualifiers from the pointee type we're converting from; they |
| 2387 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2388 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2389 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2390 | |
| 2391 | // The unqualified form of the pointee types must be compatible. |
| 2392 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2393 | bool IncompatibleObjC; |
| 2394 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2395 | FromPointee = ToPointee; |
| 2396 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2397 | IncompatibleObjC)) |
| 2398 | return false; |
| 2399 | |
| 2400 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2401 | /// __autoreleasing pointee. |
| 2402 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2403 | ConvertedType = Context.getPointerType(FromPointee); |
| 2404 | return true; |
| 2405 | } |
| 2406 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2407 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2408 | QualType& ConvertedType) { |
| 2409 | QualType ToPointeeType; |
| 2410 | if (const BlockPointerType *ToBlockPtr = |
| 2411 | ToType->getAs<BlockPointerType>()) |
| 2412 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2413 | else |
| 2414 | return false; |
| 2415 | |
| 2416 | QualType FromPointeeType; |
| 2417 | if (const BlockPointerType *FromBlockPtr = |
| 2418 | FromType->getAs<BlockPointerType>()) |
| 2419 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2420 | else |
| 2421 | return false; |
| 2422 | // We have pointer to blocks, check whether the only |
| 2423 | // differences in the argument and result types are in Objective-C |
| 2424 | // pointer conversions. If so, we permit the conversion. |
| 2425 | |
| 2426 | const FunctionProtoType *FromFunctionType |
| 2427 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2428 | const FunctionProtoType *ToFunctionType |
| 2429 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2430 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2431 | if (!FromFunctionType || !ToFunctionType) |
| 2432 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2433 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2434 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2435 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2436 | |
| 2437 | // Perform the quick checks that will tell us whether these |
| 2438 | // function types are obviously different. |
| 2439 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2440 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2441 | return false; |
| 2442 | |
| 2443 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2444 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2445 | if (FromEInfo != ToEInfo) |
| 2446 | return false; |
| 2447 | |
| 2448 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2449 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2450 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2451 | // Okay, the types match exactly. Nothing to do. |
| 2452 | } else { |
| 2453 | QualType RHS = FromFunctionType->getResultType(); |
| 2454 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2455 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2456 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2457 | LHS = LHS.getUnqualifiedType(); |
| 2458 | |
| 2459 | if (Context.hasSameType(RHS,LHS)) { |
| 2460 | // OK exact match. |
| 2461 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2462 | ConvertedType, IncompatibleObjC)) { |
| 2463 | if (IncompatibleObjC) |
| 2464 | return false; |
| 2465 | // Okay, we have an Objective-C pointer conversion. |
| 2466 | } |
| 2467 | else |
| 2468 | return false; |
| 2469 | } |
| 2470 | |
| 2471 | // Check argument types. |
| 2472 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2473 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2474 | IncompatibleObjC = false; |
| 2475 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2476 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2477 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2478 | // Okay, the types match exactly. Nothing to do. |
| 2479 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2480 | ConvertedType, IncompatibleObjC)) { |
| 2481 | if (IncompatibleObjC) |
| 2482 | return false; |
| 2483 | // Okay, we have an Objective-C pointer conversion. |
| 2484 | } else |
| 2485 | // Argument types are too different. Abort. |
| 2486 | return false; |
| 2487 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2488 | if (LangOpts.ObjCAutoRefCount && |
| 2489 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2490 | ToFunctionType)) |
| 2491 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2492 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2493 | ConvertedType = ToType; |
| 2494 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2497 | enum { |
| 2498 | ft_default, |
| 2499 | ft_different_class, |
| 2500 | ft_parameter_arity, |
| 2501 | ft_parameter_mismatch, |
| 2502 | ft_return_type, |
| 2503 | ft_qualifer_mismatch |
| 2504 | }; |
| 2505 | |
| 2506 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2507 | /// function types. Catches different number of parameter, mismatch in |
| 2508 | /// parameter types, and different return types. |
| 2509 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2510 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2511 | // If either type is not valid, include no extra info. |
| 2512 | if (FromType.isNull() || ToType.isNull()) { |
| 2513 | PDiag << ft_default; |
| 2514 | return; |
| 2515 | } |
| 2516 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2517 | // Get the function type from the pointers. |
| 2518 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2519 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2520 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2521 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2522 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2523 | << QualType(FromMember->getClass(), 0); |
| 2524 | return; |
| 2525 | } |
| 2526 | FromType = FromMember->getPointeeType(); |
| 2527 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2530 | if (FromType->isPointerType()) |
| 2531 | FromType = FromType->getPointeeType(); |
| 2532 | if (ToType->isPointerType()) |
| 2533 | ToType = ToType->getPointeeType(); |
| 2534 | |
| 2535 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2536 | FromType = FromType.getNonReferenceType(); |
| 2537 | ToType = ToType.getNonReferenceType(); |
| 2538 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2539 | // Don't print extra info for non-specialized template functions. |
| 2540 | if (FromType->isInstantiationDependentType() && |
| 2541 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2542 | PDiag << ft_default; |
| 2543 | return; |
| 2544 | } |
| 2545 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2546 | // No extra info for same types. |
| 2547 | if (Context.hasSameType(FromType, ToType)) { |
| 2548 | PDiag << ft_default; |
| 2549 | return; |
| 2550 | } |
| 2551 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2552 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2553 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2554 | |
| 2555 | // Both types need to be function types. |
| 2556 | if (!FromFunction || !ToFunction) { |
| 2557 | PDiag << ft_default; |
| 2558 | return; |
| 2559 | } |
| 2560 | |
| 2561 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2562 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2563 | << FromFunction->getNumArgs(); |
| 2564 | return; |
| 2565 | } |
| 2566 | |
| 2567 | // Handle different parameter types. |
| 2568 | unsigned ArgPos; |
| 2569 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2570 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2571 | << ToFunction->getArgType(ArgPos) |
| 2572 | << FromFunction->getArgType(ArgPos); |
| 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | // Handle different return type. |
| 2577 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2578 | ToFunction->getResultType())) { |
| 2579 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2580 | << FromFunction->getResultType(); |
| 2581 | return; |
| 2582 | } |
| 2583 | |
| 2584 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2585 | ToQuals = ToFunction->getTypeQuals(); |
| 2586 | if (FromQuals != ToQuals) { |
| 2587 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2588 | return; |
| 2589 | } |
| 2590 | |
| 2591 | // Unable to find a difference, so add no extra info. |
| 2592 | PDiag << ft_default; |
| 2593 | } |
| 2594 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2595 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2596 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2597 | /// they have same number of arguments. If the parameters are different, |
| 2598 | /// ArgPos will have the parameter index of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2599 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2600 | const FunctionProtoType *NewType, |
| 2601 | unsigned *ArgPos) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2602 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2603 | N = NewType->arg_type_begin(), |
| 2604 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2605 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2606 | N->getUnqualifiedType())) { |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2607 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2608 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | return true; |
| 2612 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2614 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2615 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2616 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2617 | /// conversions for which IsPointerConversion has already returned |
| 2618 | /// true. It returns true and produces a diagnostic if there was an |
| 2619 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2620 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2621 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2622 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2623 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2624 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2625 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2626 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2627 | Kind = CK_BitCast; |
| 2628 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2629 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2630 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2631 | Expr::NPCK_ZeroExpression) { |
| 2632 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2633 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2634 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2635 | << ToType << From->getSourceRange()); |
| 2636 | else if (!isUnevaluatedContext()) |
| 2637 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2638 | << ToType << From->getSourceRange(); |
| 2639 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2640 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2641 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2642 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2643 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2645 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2646 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2647 | // We must have a derived-to-base conversion. Check an |
| 2648 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2649 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2650 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2651 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2652 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2653 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2654 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2655 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2656 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2657 | } |
| 2658 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2659 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2660 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2661 | if (const ObjCObjectPointerType *FromPtrType = |
| 2662 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2663 | // Objective-C++ conversions are always okay. |
| 2664 | // FIXME: We should have a different class of conversions for the |
| 2665 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2666 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2667 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2668 | } else if (FromType->isBlockPointerType()) { |
| 2669 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2670 | } else { |
| 2671 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2672 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2673 | } else if (ToType->isBlockPointerType()) { |
| 2674 | if (!FromType->isBlockPointerType()) |
| 2675 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2676 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2677 | |
| 2678 | // We shouldn't fall into this case unless it's valid for other |
| 2679 | // reasons. |
| 2680 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2681 | Kind = CK_NullToPointer; |
| 2682 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2683 | return false; |
| 2684 | } |
| 2685 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2686 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2687 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2688 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2689 | /// If so, returns true and places the converted type (that might differ from |
| 2690 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2691 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2692 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2693 | bool InOverloadResolution, |
| 2694 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2695 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2696 | if (!ToTypePtr) |
| 2697 | return false; |
| 2698 | |
| 2699 | // 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] | 2700 | if (From->isNullPointerConstant(Context, |
| 2701 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2702 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2703 | ConvertedType = ToType; |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
| 2707 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2708 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2709 | if (!FromTypePtr) |
| 2710 | return false; |
| 2711 | |
| 2712 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2713 | // where D is derived from B (C++ 4.11p2). |
| 2714 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2715 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2717 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2718 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2719 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2720 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2721 | ToClass.getTypePtr()); |
| 2722 | return true; |
| 2723 | } |
| 2724 | |
| 2725 | return false; |
| 2726 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2728 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2729 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2730 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2731 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2732 | /// true and produces a diagnostic if there was an error, or returns false |
| 2733 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2735 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2736 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2737 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2738 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2739 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2740 | if (!FromPtrType) { |
| 2741 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2743 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2744 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2745 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2746 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2747 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2748 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2749 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2750 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2751 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2752 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2753 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2754 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2755 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2756 | // FIXME: What about dependent types? |
| 2757 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2758 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2759 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2760 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2761 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2762 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2763 | assert(DerivationOkay && |
| 2764 | "Should not have been called if derivation isn't OK."); |
| 2765 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2766 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2767 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2768 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2769 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2770 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2771 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2772 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2773 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2775 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2776 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2777 | << FromClass << ToClass << QualType(VBase, 0) |
| 2778 | << From->getSourceRange(); |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2782 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2783 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2784 | Paths.front(), |
| 2785 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2786 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2787 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2788 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2789 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2790 | return false; |
| 2791 | } |
| 2792 | |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2793 | /// Determine whether the lifetime conversion between the two given |
| 2794 | /// qualifiers sets is nontrivial. |
| 2795 | static bool isNonTrivialObjCLifetimeConversion(Qualifiers FromQuals, |
| 2796 | Qualifiers ToQuals) { |
| 2797 | // Converting anything to const __unsafe_unretained is trivial. |
| 2798 | if (ToQuals.hasConst() && |
| 2799 | ToQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 2800 | return false; |
| 2801 | |
| 2802 | return true; |
| 2803 | } |
| 2804 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2805 | /// IsQualificationConversion - Determines whether the conversion from |
| 2806 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2807 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2808 | /// |
| 2809 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2810 | /// when the qualification conversion involves a change in the Objective-C |
| 2811 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2814 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2815 | FromType = Context.getCanonicalType(FromType); |
| 2816 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2817 | ObjCLifetimeConversion = false; |
| 2818 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2819 | // If FromType and ToType are the same type, this is not a |
| 2820 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2821 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2822 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2824 | // (C++ 4.4p4): |
| 2825 | // A conversion can add cv-qualifiers at levels other than the first |
| 2826 | // in multi-level pointers, subject to the following rules: [...] |
| 2827 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2828 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2829 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2830 | // Within each iteration of the loop, we check the qualifiers to |
| 2831 | // determine if this still looks like a qualification |
| 2832 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2833 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2834 | // until there are no more pointers or pointers-to-members left to |
| 2835 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2836 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2837 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2838 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2839 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2840 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2841 | // Objective-C ARC: |
| 2842 | // Check Objective-C lifetime conversions. |
| 2843 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2844 | UnwrappedAnyPointer) { |
| 2845 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2846 | if (isNonTrivialObjCLifetimeConversion(FromQuals, ToQuals)) |
| 2847 | ObjCLifetimeConversion = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2848 | FromQuals.removeObjCLifetime(); |
| 2849 | ToQuals.removeObjCLifetime(); |
| 2850 | } else { |
| 2851 | // Qualification conversions cannot cast between different |
| 2852 | // Objective-C lifetime qualifiers. |
| 2853 | return false; |
| 2854 | } |
| 2855 | } |
| 2856 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2857 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2858 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2859 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2860 | FromQuals.removeObjCGCAttr(); |
| 2861 | ToQuals.removeObjCGCAttr(); |
| 2862 | } |
| 2863 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2864 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2865 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2866 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2867 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2869 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2870 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2871 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2872 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2873 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2875 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2876 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2878 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2879 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2880 | |
| 2881 | // We are left with FromType and ToType being the pointee types |
| 2882 | // after unwrapping the original FromType and ToType the same number |
| 2883 | // of types. If we unwrapped any pointers, and if FromType and |
| 2884 | // ToType have the same unqualified type (since we checked |
| 2885 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2886 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2889 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2890 | /// atomic type. |
| 2891 | /// |
| 2892 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2893 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2894 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2895 | bool InOverloadResolution, |
| 2896 | StandardConversionSequence &SCS, |
| 2897 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2898 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2899 | if (!ToAtomic) |
| 2900 | return false; |
| 2901 | |
| 2902 | StandardConversionSequence InnerSCS; |
| 2903 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2904 | InOverloadResolution, InnerSCS, |
| 2905 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2906 | return false; |
| 2907 | |
| 2908 | SCS.Second = InnerSCS.Second; |
| 2909 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2910 | SCS.Third = InnerSCS.Third; |
| 2911 | SCS.QualificationIncludesObjCLifetime |
| 2912 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2913 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2914 | return true; |
| 2915 | } |
| 2916 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2917 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2918 | CXXConstructorDecl *Constructor, |
| 2919 | QualType Type) { |
| 2920 | const FunctionProtoType *CtorType = |
| 2921 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2922 | if (CtorType->getNumArgs() > 0) { |
| 2923 | QualType FirstArg = CtorType->getArgType(0); |
| 2924 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2925 | return true; |
| 2926 | } |
| 2927 | return false; |
| 2928 | } |
| 2929 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2930 | static OverloadingResult |
| 2931 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2932 | CXXRecordDecl *To, |
| 2933 | UserDefinedConversionSequence &User, |
| 2934 | OverloadCandidateSet &CandidateSet, |
| 2935 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2936 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2937 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2938 | Con != ConEnd; ++Con) { |
| 2939 | NamedDecl *D = *Con; |
| 2940 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2941 | |
| 2942 | // Find the constructor (which may be a template). |
| 2943 | CXXConstructorDecl *Constructor = 0; |
| 2944 | FunctionTemplateDecl *ConstructorTmpl |
| 2945 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2946 | if (ConstructorTmpl) |
| 2947 | Constructor |
| 2948 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2949 | else |
| 2950 | Constructor = cast<CXXConstructorDecl>(D); |
| 2951 | |
| 2952 | bool Usable = !Constructor->isInvalidDecl() && |
| 2953 | S.isInitListConstructor(Constructor) && |
| 2954 | (AllowExplicit || !Constructor->isExplicit()); |
| 2955 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2956 | // If the first argument is (a reference to) the target type, |
| 2957 | // suppress conversions. |
| 2958 | bool SuppressUserConversions = |
| 2959 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2960 | if (ConstructorTmpl) |
| 2961 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2962 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2963 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2964 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2965 | else |
| 2966 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2967 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2968 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2973 | |
| 2974 | OverloadCandidateSet::iterator Best; |
| 2975 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2976 | case OR_Success: { |
| 2977 | // Record the standard conversion we used and the conversion function. |
| 2978 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2979 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2980 | // Initializer lists don't have conversions as such. |
| 2981 | User.Before.setAsIdentityConversion(); |
| 2982 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2983 | User.ConversionFunction = Constructor; |
| 2984 | User.FoundConversionFunction = Best->FoundDecl; |
| 2985 | User.After.setAsIdentityConversion(); |
| 2986 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2987 | User.After.setAllToTypes(ToType); |
| 2988 | return OR_Success; |
| 2989 | } |
| 2990 | |
| 2991 | case OR_No_Viable_Function: |
| 2992 | return OR_No_Viable_Function; |
| 2993 | case OR_Deleted: |
| 2994 | return OR_Deleted; |
| 2995 | case OR_Ambiguous: |
| 2996 | return OR_Ambiguous; |
| 2997 | } |
| 2998 | |
| 2999 | llvm_unreachable("Invalid OverloadResult!"); |
| 3000 | } |
| 3001 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3002 | /// Determines whether there is a user-defined conversion sequence |
| 3003 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 3004 | /// ToType. If such a conversion exists, User will contain the |
| 3005 | /// user-defined conversion sequence that performs such a conversion |
| 3006 | /// and this routine will return true. Otherwise, this routine returns |
| 3007 | /// false and User is unspecified. |
| 3008 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3009 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 3010 | /// "explicit" conversion functions as well as non-explicit conversion |
| 3011 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3012 | /// |
| 3013 | /// \param AllowObjCConversionOnExplicit true if the conversion should |
| 3014 | /// allow an extra Objective-C pointer conversion on uses of explicit |
| 3015 | /// constructors. Requires \c AllowExplicit to also be set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3016 | static OverloadingResult |
| 3017 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3018 | UserDefinedConversionSequence &User, |
| 3019 | OverloadCandidateSet &CandidateSet, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3020 | bool AllowExplicit, |
| 3021 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | a1977bf | 2013-11-08 01:20:25 +0000 | [diff] [blame] | 3022 | assert(AllowExplicit || !AllowObjCConversionOnExplicit); |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3023 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3024 | // Whether we will only visit constructors. |
| 3025 | bool ConstructorsOnly = false; |
| 3026 | |
| 3027 | // If the type we are conversion to is a class type, enumerate its |
| 3028 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3029 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3030 | // C++ [over.match.ctor]p1: |
| 3031 | // When objects of class type are direct-initialized (8.5), or |
| 3032 | // copy-initialized from an expression of the same or a |
| 3033 | // derived class type (8.5), overload resolution selects the |
| 3034 | // constructor. [...] For copy-initialization, the candidate |
| 3035 | // functions are all the converting constructors (12.3.1) of |
| 3036 | // that class. The argument list is the expression-list within |
| 3037 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3038 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3039 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3040 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3041 | ConstructorsOnly = true; |
| 3042 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3043 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3044 | // RequireCompleteType may have returned true due to some invalid decl |
| 3045 | // during template instantiation, but ToType may be complete enough now |
| 3046 | // to try to recover. |
| 3047 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3048 | // We're not going to find any constructors. |
| 3049 | } else if (CXXRecordDecl *ToRecordDecl |
| 3050 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3051 | |
| 3052 | Expr **Args = &From; |
| 3053 | unsigned NumArgs = 1; |
| 3054 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3055 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3056 | // 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] | 3057 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3058 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3059 | if (Result != OR_No_Viable_Function) |
| 3060 | return Result; |
| 3061 | // Never mind. |
| 3062 | CandidateSet.clear(); |
| 3063 | |
| 3064 | // If we're list-initializing, we pass the individual elements as |
| 3065 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3066 | Args = InitList->getInits(); |
| 3067 | NumArgs = InitList->getNumInits(); |
| 3068 | ListInitializing = true; |
| 3069 | } |
| 3070 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3071 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3072 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3073 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3074 | NamedDecl *D = *Con; |
| 3075 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3076 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3077 | // Find the constructor (which may be a template). |
| 3078 | CXXConstructorDecl *Constructor = 0; |
| 3079 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3080 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3081 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3083 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3084 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3085 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3086 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3087 | bool Usable = !Constructor->isInvalidDecl(); |
| 3088 | if (ListInitializing) |
| 3089 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3090 | else |
| 3091 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3092 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3093 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3094 | if (SuppressUserConversions && ListInitializing) { |
| 3095 | SuppressUserConversions = false; |
| 3096 | if (NumArgs == 1) { |
| 3097 | // If the first argument is (a reference to) the target type, |
| 3098 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3099 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3100 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3101 | } |
| 3102 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3103 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3104 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3105 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3106 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3107 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3108 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3109 | // Allow one user-defined conversion when user specifies a |
| 3110 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3111 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3112 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3113 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3114 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3115 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3116 | } |
| 3117 | } |
| 3118 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3119 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3120 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3121 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3122 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3124 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3126 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3127 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3128 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3129 | CXXRecordDecl::conversion_iterator> |
| 3130 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3131 | for (CXXRecordDecl::conversion_iterator |
| 3132 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3133 | DeclAccessPair FoundDecl = I.getPair(); |
| 3134 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3135 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3136 | if (isa<UsingShadowDecl>(D)) |
| 3137 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3138 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3139 | CXXConversionDecl *Conv; |
| 3140 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3141 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3142 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3143 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3144 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3145 | |
| 3146 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3147 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3148 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3149 | ActingContext, From, ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3150 | CandidateSet, |
| 3151 | AllowObjCConversionOnExplicit); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3152 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3153 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3154 | From, ToType, CandidateSet, |
| 3155 | AllowObjCConversionOnExplicit); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3156 | } |
| 3157 | } |
| 3158 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3159 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3160 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3161 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3162 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3163 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3164 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3165 | case OR_Success: |
| 3166 | // Record the standard conversion we used and the conversion function. |
| 3167 | if (CXXConstructorDecl *Constructor |
| 3168 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3169 | // C++ [over.ics.user]p1: |
| 3170 | // If the user-defined conversion is specified by a |
| 3171 | // constructor (12.3.1), the initial standard conversion |
| 3172 | // sequence converts the source type to the type required by |
| 3173 | // the argument of the constructor. |
| 3174 | // |
| 3175 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3176 | if (isa<InitListExpr>(From)) { |
| 3177 | // Initializer lists don't have conversions as such. |
| 3178 | User.Before.setAsIdentityConversion(); |
| 3179 | } else { |
| 3180 | if (Best->Conversions[0].isEllipsis()) |
| 3181 | User.EllipsisConversion = true; |
| 3182 | else { |
| 3183 | User.Before = Best->Conversions[0].Standard; |
| 3184 | User.EllipsisConversion = false; |
| 3185 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3186 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3187 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3188 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3189 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3190 | User.After.setAsIdentityConversion(); |
| 3191 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3192 | User.After.setAllToTypes(ToType); |
| 3193 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3194 | } |
| 3195 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3196 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3197 | // C++ [over.ics.user]p1: |
| 3198 | // |
| 3199 | // [...] If the user-defined conversion is specified by a |
| 3200 | // conversion function (12.3.2), the initial standard |
| 3201 | // conversion sequence converts the source type to the |
| 3202 | // implicit object parameter of the conversion function. |
| 3203 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3204 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3205 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3206 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3207 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3208 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3209 | // C++ [over.ics.user]p2: |
| 3210 | // The second standard conversion sequence converts the |
| 3211 | // result of the user-defined conversion to the target type |
| 3212 | // for the sequence. Since an implicit conversion sequence |
| 3213 | // is an initialization, the special rules for |
| 3214 | // initialization by user-defined conversion apply when |
| 3215 | // selecting the best user-defined conversion for a |
| 3216 | // user-defined conversion sequence (see 13.3.3 and |
| 3217 | // 13.3.3.1). |
| 3218 | User.After = Best->FinalConversion; |
| 3219 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3220 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3221 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3222 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3223 | case OR_No_Viable_Function: |
| 3224 | return OR_No_Viable_Function; |
| 3225 | case OR_Deleted: |
| 3226 | // No conversion here! We're done. |
| 3227 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3228 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3229 | case OR_Ambiguous: |
| 3230 | return OR_Ambiguous; |
| 3231 | } |
| 3232 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3233 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3234 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3235 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3236 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3237 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3238 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3239 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3240 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3241 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 3242 | CandidateSet, false, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3243 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3244 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3245 | diag::err_typecheck_ambiguous_condition) |
| 3246 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3247 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3248 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3249 | diag::err_typecheck_nonviable_condition_incomplete, |
| 3250 | From->getType(), From->getSourceRange())) |
| 3251 | Diag(From->getLocStart(), |
| 3252 | diag::err_typecheck_nonviable_condition) |
| 3253 | << From->getType() << From->getSourceRange() << ToType; |
| 3254 | } |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3255 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3256 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3257 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3259 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3261 | /// \brief Compare the user-defined conversion functions or constructors |
| 3262 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3263 | /// is possible. |
| 3264 | static ImplicitConversionSequence::CompareKind |
| 3265 | compareConversionFunctions(Sema &S, |
| 3266 | FunctionDecl *Function1, |
| 3267 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3268 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3269 | return ImplicitConversionSequence::Indistinguishable; |
| 3270 | |
| 3271 | // Objective-C++: |
| 3272 | // If both conversion functions are implicitly-declared conversions from |
| 3273 | // a lambda closure type to a function pointer and a block pointer, |
| 3274 | // respectively, always prefer the conversion to a function pointer, |
| 3275 | // because the function pointer is more lightweight and is more likely |
| 3276 | // to keep code working. |
| 3277 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3278 | if (!Conv1) |
| 3279 | return ImplicitConversionSequence::Indistinguishable; |
| 3280 | |
| 3281 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3282 | if (!Conv2) |
| 3283 | return ImplicitConversionSequence::Indistinguishable; |
| 3284 | |
| 3285 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3286 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3287 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3288 | if (Block1 != Block2) |
| 3289 | return Block1? ImplicitConversionSequence::Worse |
| 3290 | : ImplicitConversionSequence::Better; |
| 3291 | } |
| 3292 | |
| 3293 | return ImplicitConversionSequence::Indistinguishable; |
| 3294 | } |
| 3295 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3296 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3297 | /// conversion sequences to determine whether one is better than the |
| 3298 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3299 | static ImplicitConversionSequence::CompareKind |
| 3300 | CompareImplicitConversionSequences(Sema &S, |
| 3301 | const ImplicitConversionSequence& ICS1, |
| 3302 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3303 | { |
| 3304 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3305 | // conversion sequences (as defined in 13.3.3.1) |
| 3306 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3307 | // conversion sequence than a user-defined conversion sequence or |
| 3308 | // an ellipsis conversion sequence, and |
| 3309 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3310 | // conversion sequence than an ellipsis conversion sequence |
| 3311 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3313 | // C++0x [over.best.ics]p10: |
| 3314 | // For the purpose of ranking implicit conversion sequences as |
| 3315 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3316 | // treated as a user-defined sequence that is indistinguishable |
| 3317 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3318 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3319 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3320 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3321 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3322 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3323 | // The following checks require both conversion sequences to be of |
| 3324 | // the same kind. |
| 3325 | if (ICS1.getKind() != ICS2.getKind()) |
| 3326 | return ImplicitConversionSequence::Indistinguishable; |
| 3327 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3328 | ImplicitConversionSequence::CompareKind Result = |
| 3329 | ImplicitConversionSequence::Indistinguishable; |
| 3330 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3331 | // Two implicit conversion sequences of the same form are |
| 3332 | // indistinguishable conversion sequences unless one of the |
| 3333 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3334 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3335 | Result = CompareStandardConversionSequences(S, |
| 3336 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3337 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3338 | // User-defined conversion sequence U1 is a better conversion |
| 3339 | // sequence than another user-defined conversion sequence U2 if |
| 3340 | // they contain the same user-defined conversion function or |
| 3341 | // constructor and if the second standard conversion sequence of |
| 3342 | // U1 is better than the second standard conversion sequence of |
| 3343 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3345 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3346 | Result = CompareStandardConversionSequences(S, |
| 3347 | ICS1.UserDefined.After, |
| 3348 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3349 | else |
| 3350 | Result = compareConversionFunctions(S, |
| 3351 | ICS1.UserDefined.ConversionFunction, |
| 3352 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3355 | // List-initialization sequence L1 is a better conversion sequence than |
| 3356 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3357 | // for some X and L2 does not. |
| 3358 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 3359 | !ICS1.isBad()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3360 | if (ICS1.isStdInitializerListElement() && |
| 3361 | !ICS2.isStdInitializerListElement()) |
| 3362 | return ImplicitConversionSequence::Better; |
| 3363 | if (!ICS1.isStdInitializerListElement() && |
| 3364 | ICS2.isStdInitializerListElement()) |
| 3365 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3371 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3372 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3373 | Qualifiers Quals; |
| 3374 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3375 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3376 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3377 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3378 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3379 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3380 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3381 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3382 | // determine if one is a proper subset of the other. |
| 3383 | static ImplicitConversionSequence::CompareKind |
| 3384 | compareStandardConversionSubsets(ASTContext &Context, |
| 3385 | const StandardConversionSequence& SCS1, |
| 3386 | const StandardConversionSequence& SCS2) { |
| 3387 | ImplicitConversionSequence::CompareKind Result |
| 3388 | = ImplicitConversionSequence::Indistinguishable; |
| 3389 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3391 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3392 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3393 | return ImplicitConversionSequence::Better; |
| 3394 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3395 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3397 | if (SCS1.Second != SCS2.Second) { |
| 3398 | if (SCS1.Second == ICK_Identity) |
| 3399 | Result = ImplicitConversionSequence::Better; |
| 3400 | else if (SCS2.Second == ICK_Identity) |
| 3401 | Result = ImplicitConversionSequence::Worse; |
| 3402 | else |
| 3403 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3404 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3405 | return ImplicitConversionSequence::Indistinguishable; |
| 3406 | |
| 3407 | if (SCS1.Third == SCS2.Third) { |
| 3408 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3409 | : ImplicitConversionSequence::Indistinguishable; |
| 3410 | } |
| 3411 | |
| 3412 | if (SCS1.Third == ICK_Identity) |
| 3413 | return Result == ImplicitConversionSequence::Worse |
| 3414 | ? ImplicitConversionSequence::Indistinguishable |
| 3415 | : ImplicitConversionSequence::Better; |
| 3416 | |
| 3417 | if (SCS2.Third == ICK_Identity) |
| 3418 | return Result == ImplicitConversionSequence::Better |
| 3419 | ? ImplicitConversionSequence::Indistinguishable |
| 3420 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3422 | return ImplicitConversionSequence::Indistinguishable; |
| 3423 | } |
| 3424 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3425 | /// \brief Determine whether one of the given reference bindings is better |
| 3426 | /// than the other based on what kind of bindings they are. |
| 3427 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3428 | const StandardConversionSequence &SCS2) { |
| 3429 | // C++0x [over.ics.rank]p3b4: |
| 3430 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3431 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3432 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3433 | // 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] | 3434 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3435 | // reference*. |
| 3436 | // |
| 3437 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3438 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3439 | // time of this writing) break the standard definition of std::forward |
| 3440 | // and std::reference_wrapper when dealing with references to functions. |
| 3441 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3442 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3443 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3444 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3445 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3446 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3447 | SCS2.IsLvalueReference) || |
| 3448 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3449 | !SCS2.IsLvalueReference); |
| 3450 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3452 | /// CompareStandardConversionSequences - Compare two standard |
| 3453 | /// conversion sequences to determine whether one is better than the |
| 3454 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3455 | static ImplicitConversionSequence::CompareKind |
| 3456 | CompareStandardConversionSequences(Sema &S, |
| 3457 | const StandardConversionSequence& SCS1, |
| 3458 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3459 | { |
| 3460 | // Standard conversion sequence S1 is a better conversion sequence |
| 3461 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3462 | |
| 3463 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3464 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3465 | // excluding any Lvalue Transformation; the identity conversion |
| 3466 | // sequence is considered to be a subsequence of any |
| 3467 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3468 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3469 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3470 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3471 | |
| 3472 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3473 | // defined below), or, if not that, |
| 3474 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3475 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3476 | if (Rank1 < Rank2) |
| 3477 | return ImplicitConversionSequence::Better; |
| 3478 | else if (Rank2 < Rank1) |
| 3479 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3480 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3481 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3482 | // are indistinguishable unless one of the following rules |
| 3483 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3485 | // A conversion that is not a conversion of a pointer, or |
| 3486 | // pointer to member, to bool is better than another conversion |
| 3487 | // that is such a conversion. |
| 3488 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3489 | return SCS2.isPointerConversionToBool() |
| 3490 | ? ImplicitConversionSequence::Better |
| 3491 | : ImplicitConversionSequence::Worse; |
| 3492 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3493 | // C++ [over.ics.rank]p4b2: |
| 3494 | // |
| 3495 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3496 | // conversion of B* to A* is better than conversion of B* to |
| 3497 | // void*, and conversion of A* to void* is better than conversion |
| 3498 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3500 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3502 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3503 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3504 | // Exactly one of the conversion sequences is a conversion to |
| 3505 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3506 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3507 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3508 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3509 | // Neither conversion sequence converts to a void pointer; compare |
| 3510 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3511 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3512 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3513 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3514 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3515 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3516 | // Both conversion sequences are conversions to void |
| 3517 | // pointers. Compare the source types to determine if there's an |
| 3518 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3519 | QualType FromType1 = SCS1.getFromType(); |
| 3520 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3521 | |
| 3522 | // Adjust the types we're converting from via the array-to-pointer |
| 3523 | // conversion, if we need to. |
| 3524 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3525 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3526 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3527 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3528 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3529 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3530 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3531 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3532 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3533 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3534 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3535 | return ImplicitConversionSequence::Worse; |
| 3536 | |
| 3537 | // Objective-C++: If one interface is more specific than the |
| 3538 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3539 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3540 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3541 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3542 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3543 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3544 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3545 | FromObjCPtr2); |
| 3546 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3547 | FromObjCPtr1); |
| 3548 | if (AssignLeft != AssignRight) { |
| 3549 | return AssignLeft? ImplicitConversionSequence::Better |
| 3550 | : ImplicitConversionSequence::Worse; |
| 3551 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3552 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3553 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3554 | |
| 3555 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3556 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3558 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3559 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3561 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3562 | // Check for a better reference binding based on the kind of bindings. |
| 3563 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3564 | return ImplicitConversionSequence::Better; |
| 3565 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3566 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3568 | // C++ [over.ics.rank]p3b4: |
| 3569 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3570 | // which the references refer are the same type except for |
| 3571 | // top-level cv-qualifiers, and the type to which the reference |
| 3572 | // initialized by S2 refers is more cv-qualified than the type |
| 3573 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3574 | QualType T1 = SCS1.getToType(2); |
| 3575 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3576 | T1 = S.Context.getCanonicalType(T1); |
| 3577 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3578 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3579 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3580 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3581 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3582 | // Objective-C++ ARC: If the references refer to objects with different |
| 3583 | // lifetimes, prefer bindings that don't change lifetime. |
| 3584 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3585 | SCS2.ObjCLifetimeConversionBinding) { |
| 3586 | return SCS1.ObjCLifetimeConversionBinding |
| 3587 | ? ImplicitConversionSequence::Worse |
| 3588 | : ImplicitConversionSequence::Better; |
| 3589 | } |
| 3590 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3591 | // If the type is an array type, promote the element qualifiers to the |
| 3592 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3593 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3594 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3595 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3596 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3597 | if (T2.isMoreQualifiedThan(T1)) |
| 3598 | return ImplicitConversionSequence::Better; |
| 3599 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3600 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3601 | } |
| 3602 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3603 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3604 | // In Microsoft mode, prefer an integral conversion to a |
| 3605 | // floating-to-integral conversion if the integral conversion |
| 3606 | // is between types of the same size. |
| 3607 | // For example: |
| 3608 | // void f(float); |
| 3609 | // void f(int); |
| 3610 | // int main { |
| 3611 | // long a; |
| 3612 | // f(a); |
| 3613 | // } |
| 3614 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3615 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3616 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3617 | SCS1.Second == ICK_Integral_Conversion && |
| 3618 | SCS2.Second == ICK_Floating_Integral && |
| 3619 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3620 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3621 | return ImplicitConversionSequence::Better; |
| 3622 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3623 | return ImplicitConversionSequence::Indistinguishable; |
| 3624 | } |
| 3625 | |
| 3626 | /// CompareQualificationConversions - Compares two standard conversion |
| 3627 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3629 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3630 | CompareQualificationConversions(Sema &S, |
| 3631 | const StandardConversionSequence& SCS1, |
| 3632 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3633 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3634 | // -- S1 and S2 differ only in their qualification conversion and |
| 3635 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3636 | // cv-qualification signature of type T1 is a proper subset of |
| 3637 | // the cv-qualification signature of type T2, and S1 is not the |
| 3638 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3639 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3640 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3641 | return ImplicitConversionSequence::Indistinguishable; |
| 3642 | |
| 3643 | // FIXME: the example in the standard doesn't use a qualification |
| 3644 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3645 | QualType T1 = SCS1.getToType(2); |
| 3646 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3647 | T1 = S.Context.getCanonicalType(T1); |
| 3648 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3649 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3650 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3651 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3652 | |
| 3653 | // If the types are the same, we won't learn anything by unwrapped |
| 3654 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3655 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3656 | return ImplicitConversionSequence::Indistinguishable; |
| 3657 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3658 | // If the type is an array type, promote the element qualifiers to the type |
| 3659 | // for comparison. |
| 3660 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3661 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3662 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3663 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3664 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3666 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3667 | |
| 3668 | // Objective-C++ ARC: |
| 3669 | // Prefer qualification conversions not involving a change in lifetime |
| 3670 | // to qualification conversions that do not change lifetime. |
| 3671 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3672 | SCS2.QualificationIncludesObjCLifetime) { |
| 3673 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3674 | ? ImplicitConversionSequence::Worse |
| 3675 | : ImplicitConversionSequence::Better; |
| 3676 | } |
| 3677 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3678 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3679 | // Within each iteration of the loop, we check the qualifiers to |
| 3680 | // determine if this still looks like a qualification |
| 3681 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3682 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3683 | // until there are no more pointers or pointers-to-members left |
| 3684 | // to unwrap. This essentially mimics what |
| 3685 | // IsQualificationConversion does, but here we're checking for a |
| 3686 | // strict subset of qualifiers. |
| 3687 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3688 | // The qualifiers are the same, so this doesn't tell us anything |
| 3689 | // about how the sequences rank. |
| 3690 | ; |
| 3691 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3692 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3693 | if (Result == ImplicitConversionSequence::Worse) |
| 3694 | // Neither has qualifiers that are a subset of the other's |
| 3695 | // qualifiers. |
| 3696 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3698 | Result = ImplicitConversionSequence::Better; |
| 3699 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3700 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3701 | if (Result == ImplicitConversionSequence::Better) |
| 3702 | // Neither has qualifiers that are a subset of the other's |
| 3703 | // qualifiers. |
| 3704 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3705 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3706 | Result = ImplicitConversionSequence::Worse; |
| 3707 | } else { |
| 3708 | // Qualifiers are disjoint. |
| 3709 | return ImplicitConversionSequence::Indistinguishable; |
| 3710 | } |
| 3711 | |
| 3712 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3713 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3714 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3717 | // Check that the winning standard conversion sequence isn't using |
| 3718 | // the deprecated string literal array to pointer conversion. |
| 3719 | switch (Result) { |
| 3720 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3721 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3722 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3723 | break; |
| 3724 | |
| 3725 | case ImplicitConversionSequence::Indistinguishable: |
| 3726 | break; |
| 3727 | |
| 3728 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3729 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3730 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3731 | break; |
| 3732 | } |
| 3733 | |
| 3734 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3737 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3738 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3739 | /// various kinds of derived-to-base conversions (C++ |
| 3740 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3741 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3742 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3743 | CompareDerivedToBaseConversions(Sema &S, |
| 3744 | const StandardConversionSequence& SCS1, |
| 3745 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3746 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3747 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3748 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3749 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3750 | |
| 3751 | // Adjust the types we're converting from via the array-to-pointer |
| 3752 | // conversion, if we need to. |
| 3753 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3754 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3755 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3756 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3757 | |
| 3758 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3759 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3760 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3761 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3762 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3764 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3765 | // |
| 3766 | // If class B is derived directly or indirectly from class A and |
| 3767 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3768 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3769 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3771 | SCS2.Second == ICK_Pointer_Conversion && |
| 3772 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3773 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3774 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3776 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3778 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3779 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3780 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3781 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3782 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3784 | // -- 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] | 3785 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3786 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3787 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3788 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3789 | return ImplicitConversionSequence::Worse; |
| 3790 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3791 | |
| 3792 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3793 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3794 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3795 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3796 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3797 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3798 | } |
| 3799 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3800 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3801 | const ObjCObjectPointerType *FromPtr1 |
| 3802 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3803 | const ObjCObjectPointerType *FromPtr2 |
| 3804 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3805 | const ObjCObjectPointerType *ToPtr1 |
| 3806 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3807 | const ObjCObjectPointerType *ToPtr2 |
| 3808 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3809 | |
| 3810 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3811 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3812 | // that we do for C++ pointers to class types. However, we employ the |
| 3813 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3814 | // Objective-C pointer types. |
| 3815 | bool FromAssignLeft |
| 3816 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3817 | bool FromAssignRight |
| 3818 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3819 | bool ToAssignLeft |
| 3820 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3821 | bool ToAssignRight |
| 3822 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3823 | |
| 3824 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3825 | // type is better than a conversion to 'id'. |
| 3826 | if (ToPtr1->isObjCIdType() && |
| 3827 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3828 | return ImplicitConversionSequence::Worse; |
| 3829 | if (ToPtr2->isObjCIdType() && |
| 3830 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3831 | return ImplicitConversionSequence::Better; |
| 3832 | |
| 3833 | // A conversion to a non-id object pointer type is better than a |
| 3834 | // conversion to a qualified 'id' type |
| 3835 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3836 | return ImplicitConversionSequence::Worse; |
| 3837 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3838 | return ImplicitConversionSequence::Better; |
| 3839 | |
| 3840 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3841 | // type is better than a conversion to 'Class'. |
| 3842 | if (ToPtr1->isObjCClassType() && |
| 3843 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3844 | return ImplicitConversionSequence::Worse; |
| 3845 | if (ToPtr2->isObjCClassType() && |
| 3846 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3847 | return ImplicitConversionSequence::Better; |
| 3848 | |
| 3849 | // A conversion to a non-Class object pointer type is better than a |
| 3850 | // conversion to a qualified 'Class' type. |
| 3851 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3852 | return ImplicitConversionSequence::Worse; |
| 3853 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3854 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3856 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3857 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3858 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3859 | (ToAssignLeft != ToAssignRight)) |
| 3860 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3861 | : ImplicitConversionSequence::Better; |
| 3862 | |
| 3863 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3864 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3865 | (FromAssignLeft != FromAssignRight)) |
| 3866 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3867 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3868 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3869 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3870 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3871 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3872 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3873 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3874 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3875 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3876 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3877 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3878 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3880 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3882 | ToType2->getAs<MemberPointerType>(); |
| 3883 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3884 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3885 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3886 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3887 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3888 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3889 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3890 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3891 | // 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] | 3892 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3893 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3894 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3895 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3896 | return ImplicitConversionSequence::Better; |
| 3897 | } |
| 3898 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3899 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3900 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3901 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3902 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3903 | return ImplicitConversionSequence::Worse; |
| 3904 | } |
| 3905 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3906 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3907 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3908 | // -- 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] | 3909 | // -- binding of an expression of type C to a reference of type |
| 3910 | // B& is better than binding an expression of type C to a |
| 3911 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3912 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3913 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3914 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3915 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3916 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3917 | return ImplicitConversionSequence::Worse; |
| 3918 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3920 | // -- 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] | 3921 | // -- binding of an expression of type B to a reference of type |
| 3922 | // A& is better than binding an expression of type C to a |
| 3923 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3924 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3925 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3926 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3927 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3928 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3929 | return ImplicitConversionSequence::Worse; |
| 3930 | } |
| 3931 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3933 | return ImplicitConversionSequence::Indistinguishable; |
| 3934 | } |
| 3935 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3936 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3937 | /// C++ class. |
| 3938 | static bool isTypeValid(QualType T) { |
| 3939 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3940 | return !Record->isInvalidDecl(); |
| 3941 | |
| 3942 | return true; |
| 3943 | } |
| 3944 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3945 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3946 | /// determine whether they are reference-related, |
| 3947 | /// reference-compatible, reference-compatible with added |
| 3948 | /// qualification, or incompatible, for use in C++ initialization by |
| 3949 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3950 | /// type, and the first type (T1) is the pointee type of the reference |
| 3951 | /// type being initialized. |
| 3952 | Sema::ReferenceCompareResult |
| 3953 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3954 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3955 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3956 | bool &ObjCConversion, |
| 3957 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3958 | assert(!OrigT1->isReferenceType() && |
| 3959 | "T1 must be the pointee type of the reference type"); |
| 3960 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3961 | |
| 3962 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3963 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3964 | Qualifiers T1Quals, T2Quals; |
| 3965 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3966 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3967 | |
| 3968 | // C++ [dcl.init.ref]p4: |
| 3969 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3970 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3971 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3972 | DerivedToBase = false; |
| 3973 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3974 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3975 | if (UnqualT1 == UnqualT2) { |
| 3976 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3977 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3978 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3979 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3980 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3981 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3982 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3983 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3984 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3985 | else |
| 3986 | return Ref_Incompatible; |
| 3987 | |
| 3988 | // At this point, we know that T1 and T2 are reference-related (at |
| 3989 | // least). |
| 3990 | |
| 3991 | // If the type is an array type, promote the element qualifiers to the type |
| 3992 | // for comparison. |
| 3993 | if (isa<ArrayType>(T1) && T1Quals) |
| 3994 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3995 | if (isa<ArrayType>(T2) && T2Quals) |
| 3996 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3997 | |
| 3998 | // C++ [dcl.init.ref]p4: |
| 3999 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 4000 | // reference-related to T2 and cv1 is the same cv-qualification |
| 4001 | // as, or greater cv-qualification than, cv2. For purposes of |
| 4002 | // overload resolution, cases for which cv1 is greater |
| 4003 | // cv-qualification than cv2 are identified as |
| 4004 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4005 | // |
| 4006 | // Note that we also require equivalence of Objective-C GC and address-space |
| 4007 | // qualifiers when performing these computations, so that e.g., an int in |
| 4008 | // address space 1 is not reference-compatible with an int in address |
| 4009 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4010 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 4011 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 4012 | if (isNonTrivialObjCLifetimeConversion(T2Quals, T1Quals)) |
| 4013 | ObjCLifetimeConversion = true; |
| 4014 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4015 | T1Quals.removeObjCLifetime(); |
| 4016 | T2Quals.removeObjCLifetime(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4019 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4020 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4021 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4022 | return Ref_Compatible_With_Added_Qualification; |
| 4023 | else |
| 4024 | return Ref_Related; |
| 4025 | } |
| 4026 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4027 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4028 | /// with DeclType. Return true if something definite is found. |
| 4029 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4030 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 4031 | QualType DeclType, SourceLocation DeclLoc, |
| 4032 | Expr *Init, QualType T2, bool AllowRvalues, |
| 4033 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4034 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4035 | CXXRecordDecl *T2RecordDecl |
| 4036 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4037 | |
| 4038 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4039 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4040 | CXXRecordDecl::conversion_iterator> |
| 4041 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4042 | for (CXXRecordDecl::conversion_iterator |
| 4043 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4044 | NamedDecl *D = *I; |
| 4045 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4046 | if (isa<UsingShadowDecl>(D)) |
| 4047 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4048 | |
| 4049 | FunctionTemplateDecl *ConvTemplate |
| 4050 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4051 | CXXConversionDecl *Conv; |
| 4052 | if (ConvTemplate) |
| 4053 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4054 | else |
| 4055 | Conv = cast<CXXConversionDecl>(D); |
| 4056 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | // 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] | 4058 | // explicit conversions, skip it. |
| 4059 | if (!AllowExplicit && Conv->isExplicit()) |
| 4060 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4061 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4062 | if (AllowRvalues) { |
| 4063 | bool DerivedToBase = false; |
| 4064 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4065 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4066 | |
| 4067 | // If we are initializing an rvalue reference, don't permit conversion |
| 4068 | // functions that return lvalues. |
| 4069 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4070 | const ReferenceType *RefType |
| 4071 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4072 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4073 | continue; |
| 4074 | } |
| 4075 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4076 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4077 | S.CompareReferenceRelationship( |
| 4078 | DeclLoc, |
| 4079 | Conv->getConversionType().getNonReferenceType() |
| 4080 | .getUnqualifiedType(), |
| 4081 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4082 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4083 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4084 | continue; |
| 4085 | } else { |
| 4086 | // If the conversion function doesn't return a reference type, |
| 4087 | // it can't be considered for this conversion. An rvalue reference |
| 4088 | // is only acceptable if its referencee is a function type. |
| 4089 | |
| 4090 | const ReferenceType *RefType = |
| 4091 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4092 | if (!RefType || |
| 4093 | (!RefType->isLValueReferenceType() && |
| 4094 | !RefType->getPointeeType()->isFunctionType())) |
| 4095 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4096 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4098 | if (ConvTemplate) |
| 4099 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4100 | Init, DeclType, CandidateSet, |
| 4101 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4102 | else |
| 4103 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4104 | DeclType, CandidateSet, |
| 4105 | /*AllowObjCConversionOnExplicit=*/false); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4108 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4109 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4110 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4111 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4112 | case OR_Success: |
| 4113 | // C++ [over.ics.ref]p1: |
| 4114 | // |
| 4115 | // [...] If the parameter binds directly to the result of |
| 4116 | // applying a conversion function to the argument |
| 4117 | // expression, the implicit conversion sequence is a |
| 4118 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4119 | // second standard conversion sequence either an identity |
| 4120 | // conversion or, if the conversion function returns an |
| 4121 | // entity of a type that is a derived class of the parameter |
| 4122 | // type, a derived-to-base Conversion. |
| 4123 | if (!Best->FinalConversion.DirectBinding) |
| 4124 | return false; |
| 4125 | |
| 4126 | ICS.setUserDefined(); |
| 4127 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4128 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4129 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4130 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4131 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4132 | ICS.UserDefined.EllipsisConversion = false; |
| 4133 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4134 | ICS.UserDefined.After.DirectBinding && |
| 4135 | "Expected a direct reference binding!"); |
| 4136 | return true; |
| 4137 | |
| 4138 | case OR_Ambiguous: |
| 4139 | ICS.setAmbiguous(); |
| 4140 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4141 | Cand != CandidateSet.end(); ++Cand) |
| 4142 | if (Cand->Viable) |
| 4143 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4144 | return true; |
| 4145 | |
| 4146 | case OR_No_Viable_Function: |
| 4147 | case OR_Deleted: |
| 4148 | // There was no suitable conversion, or we found a deleted |
| 4149 | // conversion; continue with other checks. |
| 4150 | return false; |
| 4151 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4152 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4153 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4156 | /// \brief Compute an implicit conversion sequence for reference |
| 4157 | /// initialization. |
| 4158 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4159 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4160 | SourceLocation DeclLoc, |
| 4161 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4162 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4163 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4164 | |
| 4165 | // Most paths end in a failed conversion. |
| 4166 | ImplicitConversionSequence ICS; |
| 4167 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4168 | |
| 4169 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4170 | QualType T2 = Init->getType(); |
| 4171 | |
| 4172 | // If the initializer is the address of an overloaded function, try |
| 4173 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4174 | // type of the resulting function. |
| 4175 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4176 | DeclAccessPair Found; |
| 4177 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4178 | false, Found)) |
| 4179 | T2 = Fn->getType(); |
| 4180 | } |
| 4181 | |
| 4182 | // Compute some basic properties of the types and the initializer. |
| 4183 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4184 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4185 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4186 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4187 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4188 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4189 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4190 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4192 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4193 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4194 | // A reference to type "cv1 T1" is initialized by an expression |
| 4195 | // of type "cv2 T2" as follows: |
| 4196 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4197 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4198 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4199 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4200 | // reference-compatible with "cv2 T2," or |
| 4201 | // |
| 4202 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4203 | if (InitCategory.isLValue() && |
| 4204 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4205 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4206 | // When a parameter of reference type binds directly (8.5.3) |
| 4207 | // to an argument expression, the implicit conversion sequence |
| 4208 | // is the identity conversion, unless the argument expression |
| 4209 | // has a type that is a derived class of the parameter type, |
| 4210 | // in which case the implicit conversion sequence is a |
| 4211 | // derived-to-base Conversion (13.3.3.1). |
| 4212 | ICS.setStandard(); |
| 4213 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4214 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4215 | : ObjCConversion? ICK_Compatible_Conversion |
| 4216 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4217 | ICS.Standard.Third = ICK_Identity; |
| 4218 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4219 | ICS.Standard.setToType(0, T2); |
| 4220 | ICS.Standard.setToType(1, T1); |
| 4221 | ICS.Standard.setToType(2, T1); |
| 4222 | ICS.Standard.ReferenceBinding = true; |
| 4223 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4224 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4225 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4226 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4227 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4228 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4229 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4230 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4231 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4232 | // derived-to-base conversions is suppressed when we're |
| 4233 | // computing the implicit conversion sequence (C++ |
| 4234 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4235 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4236 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4237 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4238 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4239 | // not reference-related to T2, and can be implicitly |
| 4240 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4241 | // is reference-compatible with "cv3 T3" 92) (this |
| 4242 | // conversion is selected by enumerating the applicable |
| 4243 | // conversion functions (13.3.1.6) and choosing the best |
| 4244 | // one through overload resolution (13.3)), |
| 4245 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4247 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4248 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4249 | Init, T2, /*AllowRvalues=*/false, |
| 4250 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4251 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4252 | } |
| 4253 | } |
| 4254 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4255 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4256 | // 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] | 4257 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4259 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4260 | // point, which is that, due to p2 (which short-circuits reference |
| 4261 | // binding by only attempting a simple conversion for non-direct |
| 4262 | // bindings) and p3's strange wording, we allow a const volatile |
| 4263 | // reference to bind to an rvalue. Hence the check for the presence |
| 4264 | // of "const" rather than checking for "const" being the only |
| 4265 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4266 | // This is also the point where rvalue references and lvalue inits no longer |
| 4267 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4268 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4269 | return ICS; |
| 4270 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4271 | // -- If the initializer expression |
| 4272 | // |
| 4273 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4274 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4275 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4276 | (InitCategory.isXValue() || |
| 4277 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4278 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4279 | ICS.setStandard(); |
| 4280 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4282 | : ObjCConversion? ICK_Compatible_Conversion |
| 4283 | : ICK_Identity; |
| 4284 | ICS.Standard.Third = ICK_Identity; |
| 4285 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4286 | ICS.Standard.setToType(0, T2); |
| 4287 | ICS.Standard.setToType(1, T1); |
| 4288 | ICS.Standard.setToType(2, T1); |
| 4289 | ICS.Standard.ReferenceBinding = true; |
| 4290 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4291 | // binding unless we're binding to a class prvalue. |
| 4292 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4293 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4294 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4296 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4297 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4298 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4299 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4301 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4302 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4303 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4304 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4305 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4307 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4308 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | // an xvalue, class prvalue, or function lvalue of type |
| 4310 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4311 | // "cv3 T3", |
| 4312 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4314 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4316 | // class subobject). |
| 4317 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4319 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4320 | Init, T2, /*AllowRvalues=*/true, |
| 4321 | AllowExplicit)) { |
| 4322 | // In the second case, if the reference is an rvalue reference |
| 4323 | // and the second standard conversion sequence of the |
| 4324 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4325 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4327 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4328 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4329 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4330 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4331 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4332 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4333 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4334 | // initialized from the initializer expression using the |
| 4335 | // rules for a non-reference copy initialization (8.5). The |
| 4336 | // reference is then bound to the temporary. If T1 is |
| 4337 | // reference-related to T2, cv1 must be the same |
| 4338 | // cv-qualification as, or greater cv-qualification than, |
| 4339 | // cv2; otherwise, the program is ill-formed. |
| 4340 | if (RefRelationship == Sema::Ref_Related) { |
| 4341 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4342 | // we would be reference-compatible or reference-compatible with |
| 4343 | // added qualification. But that wasn't the case, so the reference |
| 4344 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4345 | // |
| 4346 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4347 | // ObjC GC and lifetime qualifiers aren't important. |
| 4348 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4349 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4350 | T1Quals.removeObjCGCAttr(); |
| 4351 | T1Quals.removeObjCLifetime(); |
| 4352 | T2Quals.removeObjCGCAttr(); |
| 4353 | T2Quals.removeObjCLifetime(); |
| 4354 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4355 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | // If at least one of the types is a class type, the types are not |
| 4359 | // related, and we aren't allowed any user conversions, the |
| 4360 | // reference binding fails. This case is important for breaking |
| 4361 | // recursion, since TryImplicitConversion below will attempt to |
| 4362 | // create a temporary through the use of a copy constructor. |
| 4363 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4364 | (T1->isRecordType() || T2->isRecordType())) |
| 4365 | return ICS; |
| 4366 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4367 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4368 | // reference, the initializer expression shall not be an lvalue. |
| 4369 | if (RefRelationship >= Sema::Ref_Related && |
| 4370 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4371 | return ICS; |
| 4372 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4373 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4374 | // When a parameter of reference type is not bound directly to |
| 4375 | // an argument expression, the conversion sequence is the one |
| 4376 | // required to convert the argument expression to the |
| 4377 | // underlying type of the reference according to |
| 4378 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4379 | // to copy-initializing a temporary of the underlying type with |
| 4380 | // the argument expression. Any difference in top-level |
| 4381 | // cv-qualification is subsumed by the initialization itself |
| 4382 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4383 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4384 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4385 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4386 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4387 | /*AllowObjCWritebackConversion=*/false, |
| 4388 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4389 | |
| 4390 | // Of course, that's still a reference binding. |
| 4391 | if (ICS.isStandard()) { |
| 4392 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4393 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4394 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4395 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4396 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4397 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4398 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4399 | // Don't allow rvalue references to bind to lvalues. |
| 4400 | if (DeclType->isRValueReferenceType()) { |
| 4401 | if (const ReferenceType *RefType |
| 4402 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4403 | ->getAs<LValueReferenceType>()) { |
| 4404 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4405 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4406 | DeclType); |
| 4407 | return ICS; |
| 4408 | } |
| 4409 | } |
| 4410 | } |
| 4411 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4412 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4413 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4414 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4415 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4416 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4417 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4418 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4420 | return ICS; |
| 4421 | } |
| 4422 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4423 | static ImplicitConversionSequence |
| 4424 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4425 | bool SuppressUserConversions, |
| 4426 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4427 | bool AllowObjCWritebackConversion, |
| 4428 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4429 | |
| 4430 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4431 | /// initializer list From. |
| 4432 | static ImplicitConversionSequence |
| 4433 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4434 | bool SuppressUserConversions, |
| 4435 | bool InOverloadResolution, |
| 4436 | bool AllowObjCWritebackConversion) { |
| 4437 | // C++11 [over.ics.list]p1: |
| 4438 | // When an argument is an initializer list, it is not an expression and |
| 4439 | // special rules apply for converting it to a parameter type. |
| 4440 | |
| 4441 | ImplicitConversionSequence Result; |
| 4442 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4443 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4444 | // 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] | 4445 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4446 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4447 | return Result; |
| 4448 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4449 | // C++11 [over.ics.list]p2: |
| 4450 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4451 | // all the elements can be implicitly converted to X, the implicit |
| 4452 | // conversion sequence is the worst conversion necessary to convert an |
| 4453 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4454 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4455 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4456 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4457 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4458 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4459 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4460 | if (!X.isNull()) { |
| 4461 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4462 | Expr *Init = From->getInit(i); |
| 4463 | ImplicitConversionSequence ICS = |
| 4464 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4465 | InOverloadResolution, |
| 4466 | AllowObjCWritebackConversion); |
| 4467 | // If a single element isn't convertible, fail. |
| 4468 | if (ICS.isBad()) { |
| 4469 | Result = ICS; |
| 4470 | break; |
| 4471 | } |
| 4472 | // Otherwise, look for the worst conversion. |
| 4473 | if (Result.isBad() || |
| 4474 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4475 | ImplicitConversionSequence::Worse) |
| 4476 | Result = ICS; |
| 4477 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4478 | |
| 4479 | // For an empty list, we won't have computed any conversion sequence. |
| 4480 | // Introduce the identity conversion sequence. |
| 4481 | if (From->getNumInits() == 0) { |
| 4482 | Result.setStandard(); |
| 4483 | Result.Standard.setAsIdentityConversion(); |
| 4484 | Result.Standard.setFromType(ToType); |
| 4485 | Result.Standard.setAllToTypes(ToType); |
| 4486 | } |
| 4487 | |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4488 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4489 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4490 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4491 | |
| 4492 | // C++11 [over.ics.list]p3: |
| 4493 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4494 | // resolution chooses a single best constructor [...] the implicit |
| 4495 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4496 | // constructors are viable but none is better than the others, the |
| 4497 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4498 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4499 | // This function can deal with initializer lists. |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4500 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4501 | /*AllowExplicit=*/false, |
| 4502 | InOverloadResolution, /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4503 | AllowObjCWritebackConversion, |
| 4504 | /*AllowObjCConversionOnExplicit=*/false); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4505 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4506 | |
| 4507 | // C++11 [over.ics.list]p4: |
| 4508 | // Otherwise, if the parameter has an aggregate type which can be |
| 4509 | // initialized from the initializer list [...] the implicit conversion |
| 4510 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4511 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4512 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4513 | // down to checking whether the initialization works. |
| 4514 | // FIXME: Find out whether this parameter is consumed or not. |
| 4515 | InitializedEntity Entity = |
| 4516 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4517 | /*Consumed=*/false); |
| 4518 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4519 | Result.setUserDefined(); |
| 4520 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4521 | // Initializer lists don't have a type. |
| 4522 | Result.UserDefined.Before.setFromType(QualType()); |
| 4523 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4524 | |
| 4525 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4526 | Result.UserDefined.After.setFromType(ToType); |
| 4527 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4528 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4529 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4530 | return Result; |
| 4531 | } |
| 4532 | |
| 4533 | // C++11 [over.ics.list]p5: |
| 4534 | // 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] | 4535 | if (ToType->isReferenceType()) { |
| 4536 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4537 | // mention initializer lists in any way. So we go by what list- |
| 4538 | // initialization would do and try to extrapolate from that. |
| 4539 | |
| 4540 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4541 | |
| 4542 | // If the initializer list has a single element that is reference-related |
| 4543 | // to the parameter type, we initialize the reference from that. |
| 4544 | if (From->getNumInits() == 1) { |
| 4545 | Expr *Init = From->getInit(0); |
| 4546 | |
| 4547 | QualType T2 = Init->getType(); |
| 4548 | |
| 4549 | // If the initializer is the address of an overloaded function, try |
| 4550 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4551 | // type of the resulting function. |
| 4552 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4553 | DeclAccessPair Found; |
| 4554 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4555 | Init, ToType, false, Found)) |
| 4556 | T2 = Fn->getType(); |
| 4557 | } |
| 4558 | |
| 4559 | // Compute some basic properties of the types and the initializer. |
| 4560 | bool dummy1 = false; |
| 4561 | bool dummy2 = false; |
| 4562 | bool dummy3 = false; |
| 4563 | Sema::ReferenceCompareResult RefRelationship |
| 4564 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4565 | dummy2, dummy3); |
| 4566 | |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4567 | if (RefRelationship >= Sema::Ref_Related) { |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4568 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4569 | SuppressUserConversions, |
| 4570 | /*AllowExplicit=*/false); |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4571 | } |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | // Otherwise, we bind the reference to a temporary created from the |
| 4575 | // initializer list. |
| 4576 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4577 | InOverloadResolution, |
| 4578 | AllowObjCWritebackConversion); |
| 4579 | if (Result.isFailure()) |
| 4580 | return Result; |
| 4581 | assert(!Result.isEllipsis() && |
| 4582 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4583 | |
| 4584 | // Can we even bind to a temporary? |
| 4585 | if (ToType->isRValueReferenceType() || |
| 4586 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4587 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4588 | Result.UserDefined.After; |
| 4589 | SCS.ReferenceBinding = true; |
| 4590 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4591 | SCS.BindsToRvalue = true; |
| 4592 | SCS.BindsToFunctionLvalue = false; |
| 4593 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4594 | SCS.ObjCLifetimeConversionBinding = false; |
| 4595 | } else |
| 4596 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4597 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4598 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4599 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4600 | |
| 4601 | // C++11 [over.ics.list]p6: |
| 4602 | // Otherwise, if the parameter type is not a class: |
| 4603 | if (!ToType->isRecordType()) { |
| 4604 | // - if the initializer list has one element, the implicit conversion |
| 4605 | // sequence is the one required to convert the element to the |
| 4606 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4607 | unsigned NumInits = From->getNumInits(); |
| 4608 | if (NumInits == 1) |
| 4609 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4610 | SuppressUserConversions, |
| 4611 | InOverloadResolution, |
| 4612 | AllowObjCWritebackConversion); |
| 4613 | // - if the initializer list has no elements, the implicit conversion |
| 4614 | // sequence is the identity conversion. |
| 4615 | else if (NumInits == 0) { |
| 4616 | Result.setStandard(); |
| 4617 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4618 | Result.Standard.setFromType(ToType); |
| 4619 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4620 | } |
| 4621 | return Result; |
| 4622 | } |
| 4623 | |
| 4624 | // C++11 [over.ics.list]p7: |
| 4625 | // In all cases other than those enumerated above, no conversion is possible |
| 4626 | return Result; |
| 4627 | } |
| 4628 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4629 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4630 | /// ToType from the expression From. Return the implicit conversion |
| 4631 | /// sequence required to pass this argument, which may be a bad |
| 4632 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4633 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4634 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4635 | static ImplicitConversionSequence |
| 4636 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4637 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4638 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4639 | bool AllowObjCWritebackConversion, |
| 4640 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4641 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4642 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4643 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4644 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4645 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4646 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4647 | /*FIXME:*/From->getLocStart(), |
| 4648 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4649 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4650 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4651 | return TryImplicitConversion(S, From, ToType, |
| 4652 | SuppressUserConversions, |
| 4653 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4654 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4655 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4656 | AllowObjCWritebackConversion, |
| 4657 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4660 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4661 | const CanQualType ToQTy, |
| 4662 | Sema &S, |
| 4663 | SourceLocation Loc, |
| 4664 | ExprValueKind FromVK) { |
| 4665 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4666 | ImplicitConversionSequence ICS = |
| 4667 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4668 | |
| 4669 | return !ICS.isBad(); |
| 4670 | } |
| 4671 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4672 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4673 | /// parameter of the given member function (@c Method) from the |
| 4674 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4675 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4676 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4677 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4678 | CXXMethodDecl *Method, |
| 4679 | CXXRecordDecl *ActingContext) { |
| 4680 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4681 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4682 | // const volatile object. |
| 4683 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4684 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4685 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4686 | |
| 4687 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4688 | // to exit early. |
| 4689 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4690 | |
| 4691 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4692 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4693 | FromType = PT->getPointeeType(); |
| 4694 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4695 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4696 | // better have an lvalue. |
| 4697 | assert(FromClassification.isLValue()); |
| 4698 | } |
| 4699 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4700 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4701 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4702 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4703 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4704 | // parameter is |
| 4705 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4706 | // - "lvalue reference to cv X" for functions declared without a |
| 4707 | // ref-qualifier or with the & ref-qualifier |
| 4708 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4709 | // ref-qualifier |
| 4710 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4711 | // 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] | 4712 | // cv-qualification on the member function declaration. |
| 4713 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4715 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4716 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4717 | // reference binding here, that allows class rvalues to bind to |
| 4718 | // non-constant references. |
| 4719 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4720 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4721 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4723 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4724 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4725 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4726 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4727 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4728 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4729 | |
| 4730 | // Check that we have either the same type or a derived type. It |
| 4731 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4732 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4733 | ImplicitConversionKind SecondKind; |
| 4734 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4735 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4736 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4737 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4738 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4739 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4740 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4741 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4742 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4743 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4744 | // Check the ref-qualifier. |
| 4745 | switch (Method->getRefQualifier()) { |
| 4746 | case RQ_None: |
| 4747 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4748 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4750 | case RQ_LValue: |
| 4751 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4752 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4753 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4754 | ImplicitParamType); |
| 4755 | return ICS; |
| 4756 | } |
| 4757 | break; |
| 4758 | |
| 4759 | case RQ_RValue: |
| 4760 | if (!FromClassification.isRValue()) { |
| 4761 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4762 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4763 | ImplicitParamType); |
| 4764 | return ICS; |
| 4765 | } |
| 4766 | break; |
| 4767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4769 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4770 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4771 | ICS.Standard.setAsIdentityConversion(); |
| 4772 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4773 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4774 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4775 | ICS.Standard.ReferenceBinding = true; |
| 4776 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4778 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4779 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4780 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4781 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4782 | return ICS; |
| 4783 | } |
| 4784 | |
| 4785 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4786 | /// the implicit object parameter for the given Method with the given |
| 4787 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4788 | ExprResult |
| 4789 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4791 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4792 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4793 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4795 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4797 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4798 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4799 | FromRecordType = PT->getPointeeType(); |
| 4800 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4801 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4802 | } else { |
| 4803 | FromRecordType = From->getType(); |
| 4804 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4805 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4808 | // Note that we always use the true parent context when performing |
| 4809 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4811 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4812 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4813 | if (ICS.isBad()) { |
| 4814 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4815 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4816 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4817 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4818 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4819 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4820 | diag::err_member_function_call_bad_cvr) |
| 4821 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4822 | << From->getSourceRange(); |
| 4823 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4824 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4825 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4826 | } |
| 4827 | } |
| 4828 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4829 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4830 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4831 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4833 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4834 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4835 | ExprResult FromRes = |
| 4836 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4837 | if (FromRes.isInvalid()) |
| 4838 | return ExprError(); |
| 4839 | From = FromRes.take(); |
| 4840 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4841 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4842 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4843 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4844 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4845 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4848 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4849 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4850 | static ImplicitConversionSequence |
| 4851 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4852 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4853 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4854 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4855 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4856 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 4857 | /*AllowObjCWritebackConversion=*/false, |
| 4858 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4862 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4863 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4864 | if (checkPlaceholderForOverload(*this, From)) |
| 4865 | return ExprError(); |
| 4866 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4867 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4868 | if (!ICS.isBad()) |
| 4869 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4870 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4871 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4872 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4873 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4874 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4875 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4876 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4877 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4878 | /// Check that the specified conversion is permitted in a converted constant |
| 4879 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4880 | /// is acceptable. |
| 4881 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4882 | StandardConversionSequence &SCS) { |
| 4883 | // Since we know that the target type is an integral or unscoped enumeration |
| 4884 | // type, most conversion kinds are impossible. All possible First and Third |
| 4885 | // conversions are fine. |
| 4886 | switch (SCS.Second) { |
| 4887 | case ICK_Identity: |
| 4888 | case ICK_Integral_Promotion: |
| 4889 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4890 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4891 | return true; |
| 4892 | |
| 4893 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4894 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4895 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4896 | // conversion, so it's permitted in a converted constant expression. |
| 4897 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4898 | SCS.getToType(2)->isBooleanType(); |
| 4899 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4900 | case ICK_Floating_Integral: |
| 4901 | case ICK_Complex_Real: |
| 4902 | return false; |
| 4903 | |
| 4904 | case ICK_Lvalue_To_Rvalue: |
| 4905 | case ICK_Array_To_Pointer: |
| 4906 | case ICK_Function_To_Pointer: |
| 4907 | case ICK_NoReturn_Adjustment: |
| 4908 | case ICK_Qualification: |
| 4909 | case ICK_Compatible_Conversion: |
| 4910 | case ICK_Vector_Conversion: |
| 4911 | case ICK_Vector_Splat: |
| 4912 | case ICK_Derived_To_Base: |
| 4913 | case ICK_Pointer_Conversion: |
| 4914 | case ICK_Pointer_Member: |
| 4915 | case ICK_Block_Pointer_Conversion: |
| 4916 | case ICK_Writeback_Conversion: |
| 4917 | case ICK_Floating_Promotion: |
| 4918 | case ICK_Complex_Promotion: |
| 4919 | case ICK_Complex_Conversion: |
| 4920 | case ICK_Floating_Conversion: |
| 4921 | case ICK_TransparentUnionConversion: |
| 4922 | llvm_unreachable("unexpected second conversion kind"); |
| 4923 | |
| 4924 | case ICK_Num_Conversion_Kinds: |
| 4925 | break; |
| 4926 | } |
| 4927 | |
| 4928 | llvm_unreachable("unknown conversion kind"); |
| 4929 | } |
| 4930 | |
| 4931 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4932 | /// converted constant expression of type T, perform the conversion and produce |
| 4933 | /// the converted expression, per C++11 [expr.const]p3. |
| 4934 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4935 | llvm::APSInt &Value, |
| 4936 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4937 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4938 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4939 | |
| 4940 | if (checkPlaceholderForOverload(*this, From)) |
| 4941 | return ExprError(); |
| 4942 | |
| 4943 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4944 | // A converted constant expression of type T is a core constant expression, |
| 4945 | // implicitly converted to a prvalue of type T, where the converted |
| 4946 | // expression is a literal constant expression and the implicit conversion |
| 4947 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4948 | // conversions, integral promotions, and integral conversions other than |
| 4949 | // narrowing conversions. |
| 4950 | ImplicitConversionSequence ICS = |
| 4951 | TryImplicitConversion(From, T, |
| 4952 | /*SuppressUserConversions=*/false, |
| 4953 | /*AllowExplicit=*/false, |
| 4954 | /*InOverloadResolution=*/false, |
| 4955 | /*CStyle=*/false, |
| 4956 | /*AllowObjcWritebackConversion=*/false); |
| 4957 | StandardConversionSequence *SCS = 0; |
| 4958 | switch (ICS.getKind()) { |
| 4959 | case ImplicitConversionSequence::StandardConversion: |
| 4960 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4961 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4962 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4963 | << From->getType() << From->getSourceRange() << T; |
| 4964 | SCS = &ICS.Standard; |
| 4965 | break; |
| 4966 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4967 | // We are converting from class type to an integral or enumeration type, so |
| 4968 | // the Before sequence must be trivial. |
| 4969 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4970 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4971 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4972 | << From->getType() << From->getSourceRange() << T; |
| 4973 | SCS = &ICS.UserDefined.After; |
| 4974 | break; |
| 4975 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4976 | case ImplicitConversionSequence::BadConversion: |
| 4977 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4978 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4979 | diag::err_typecheck_converted_constant_expression) |
| 4980 | << From->getType() << From->getSourceRange() << T; |
| 4981 | return ExprError(); |
| 4982 | |
| 4983 | case ImplicitConversionSequence::EllipsisConversion: |
| 4984 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4985 | } |
| 4986 | |
| 4987 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4988 | if (Result.isInvalid()) |
| 4989 | return Result; |
| 4990 | |
| 4991 | // Check for a narrowing implicit conversion. |
| 4992 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4993 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4994 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4995 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4996 | case NK_Variable_Narrowing: |
| 4997 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4998 | // expression. We'll diagnose this in a moment. |
| 4999 | case NK_Not_Narrowing: |
| 5000 | break; |
| 5001 | |
| 5002 | case NK_Constant_Narrowing: |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 5003 | Diag(From->getLocStart(), diag::ext_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5004 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5005 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5006 | break; |
| 5007 | |
| 5008 | case NK_Type_Narrowing: |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 5009 | Diag(From->getLocStart(), diag::ext_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5010 | << CCE << /*Constant*/0 << From->getType() << T; |
| 5011 | break; |
| 5012 | } |
| 5013 | |
| 5014 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5015 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5016 | Expr::EvalResult Eval; |
| 5017 | Eval.Diag = &Notes; |
| 5018 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 5019 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5020 | // The expression can't be folded, so we can't keep it at this position in |
| 5021 | // the AST. |
| 5022 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5023 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5024 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5025 | |
| 5026 | if (Notes.empty()) { |
| 5027 | // It's a constant expression. |
| 5028 | return Result; |
| 5029 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5033 | if (Notes.size() == 1 && |
| 5034 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5035 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5036 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5037 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5038 | << CCE << From->getSourceRange(); |
| 5039 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5040 | Diag(Notes[I].first, Notes[I].second); |
| 5041 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5042 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5045 | /// dropPointerConversions - If the given standard conversion sequence |
| 5046 | /// involves any pointer conversions, remove them. This may change |
| 5047 | /// the result type of the conversion sequence. |
| 5048 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5049 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5050 | SCS.Second = ICK_Identity; |
| 5051 | SCS.Third = ICK_Identity; |
| 5052 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5053 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5054 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5055 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5056 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5057 | /// convert the expression From to an Objective-C pointer type. |
| 5058 | static ImplicitConversionSequence |
| 5059 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5060 | // Do an implicit conversion to 'id'. |
| 5061 | QualType Ty = S.Context.getObjCIdType(); |
| 5062 | ImplicitConversionSequence ICS |
| 5063 | = TryImplicitConversion(S, From, Ty, |
| 5064 | // FIXME: Are these flags correct? |
| 5065 | /*SuppressUserConversions=*/false, |
| 5066 | /*AllowExplicit=*/true, |
| 5067 | /*InOverloadResolution=*/false, |
| 5068 | /*CStyle=*/false, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5069 | /*AllowObjCWritebackConversion=*/false, |
| 5070 | /*AllowObjCConversionOnExplicit=*/true); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5071 | |
| 5072 | // Strip off any final conversions to 'id'. |
| 5073 | switch (ICS.getKind()) { |
| 5074 | case ImplicitConversionSequence::BadConversion: |
| 5075 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5076 | case ImplicitConversionSequence::EllipsisConversion: |
| 5077 | break; |
| 5078 | |
| 5079 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5080 | dropPointerConversion(ICS.UserDefined.After); |
| 5081 | break; |
| 5082 | |
| 5083 | case ImplicitConversionSequence::StandardConversion: |
| 5084 | dropPointerConversion(ICS.Standard); |
| 5085 | break; |
| 5086 | } |
| 5087 | |
| 5088 | return ICS; |
| 5089 | } |
| 5090 | |
| 5091 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5092 | /// conversion of the expression From to an Objective-C pointer type. |
| 5093 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5094 | if (checkPlaceholderForOverload(*this, From)) |
| 5095 | return ExprError(); |
| 5096 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5097 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5098 | ImplicitConversionSequence ICS = |
| 5099 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5100 | if (!ICS.isBad()) |
| 5101 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5102 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5103 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5104 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5105 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5106 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5107 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5108 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5109 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5112 | static ExprResult |
| 5113 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5114 | Sema::ContextualImplicitConverter &Converter, |
| 5115 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5116 | |
| 5117 | if (Converter.Suppress) |
| 5118 | return ExprError(); |
| 5119 | |
| 5120 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5121 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5122 | CXXConversionDecl *Conv = |
| 5123 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5124 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5125 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5126 | } |
| 5127 | return SemaRef.Owned(From); |
| 5128 | } |
| 5129 | |
| 5130 | static bool |
| 5131 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5132 | Sema::ContextualImplicitConverter &Converter, |
| 5133 | QualType T, bool HadMultipleCandidates, |
| 5134 | UnresolvedSetImpl &ExplicitConversions) { |
| 5135 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5136 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5137 | CXXConversionDecl *Conversion = |
| 5138 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5139 | |
| 5140 | // The user probably meant to invoke the given explicit |
| 5141 | // conversion; use it. |
| 5142 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5143 | std::string TypeStr; |
| 5144 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5145 | |
| 5146 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5147 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5148 | "static_cast<" + TypeStr + ">(") |
| 5149 | << FixItHint::CreateInsertion( |
| 5150 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5151 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5152 | |
| 5153 | // If we aren't in a SFINAE context, build a call to the |
| 5154 | // explicit conversion function. |
| 5155 | if (SemaRef.isSFINAEContext()) |
| 5156 | return true; |
| 5157 | |
| 5158 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5159 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5160 | HadMultipleCandidates); |
| 5161 | if (Result.isInvalid()) |
| 5162 | return true; |
| 5163 | // Record usage of conversion in an implicit cast. |
| 5164 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5165 | CK_UserDefinedConversion, Result.get(), 0, |
| 5166 | Result.get()->getValueKind()); |
| 5167 | } |
| 5168 | return false; |
| 5169 | } |
| 5170 | |
| 5171 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5172 | Sema::ContextualImplicitConverter &Converter, |
| 5173 | QualType T, bool HadMultipleCandidates, |
| 5174 | DeclAccessPair &Found) { |
| 5175 | CXXConversionDecl *Conversion = |
| 5176 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5177 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5178 | |
| 5179 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5180 | if (!Converter.SuppressConversion) { |
| 5181 | if (SemaRef.isSFINAEContext()) |
| 5182 | return true; |
| 5183 | |
| 5184 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5185 | << From->getSourceRange(); |
| 5186 | } |
| 5187 | |
| 5188 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5189 | HadMultipleCandidates); |
| 5190 | if (Result.isInvalid()) |
| 5191 | return true; |
| 5192 | // Record usage of conversion in an implicit cast. |
| 5193 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5194 | CK_UserDefinedConversion, Result.get(), 0, |
| 5195 | Result.get()->getValueKind()); |
| 5196 | return false; |
| 5197 | } |
| 5198 | |
| 5199 | static ExprResult finishContextualImplicitConversion( |
| 5200 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5201 | Sema::ContextualImplicitConverter &Converter) { |
| 5202 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5203 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5204 | << From->getSourceRange(); |
| 5205 | |
| 5206 | return SemaRef.DefaultLvalueConversion(From); |
| 5207 | } |
| 5208 | |
| 5209 | static void |
| 5210 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5211 | UnresolvedSetImpl &ViableConversions, |
| 5212 | OverloadCandidateSet &CandidateSet) { |
| 5213 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5214 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5215 | NamedDecl *D = FoundDecl.getDecl(); |
| 5216 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5217 | if (isa<UsingShadowDecl>(D)) |
| 5218 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5219 | |
| 5220 | CXXConversionDecl *Conv; |
| 5221 | FunctionTemplateDecl *ConvTemplate; |
| 5222 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5223 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5224 | else |
| 5225 | Conv = cast<CXXConversionDecl>(D); |
| 5226 | |
| 5227 | if (ConvTemplate) |
| 5228 | SemaRef.AddTemplateConversionCandidate( |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5229 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet, |
| 5230 | /*AllowObjCConversionOnExplicit=*/false); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5231 | else |
| 5232 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5233 | ToType, CandidateSet, |
| 5234 | /*AllowObjCConversionOnExplicit=*/false); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5235 | } |
| 5236 | } |
| 5237 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5238 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5239 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5240 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5241 | /// This routine will attempt to convert an expression of class type to a |
| 5242 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5243 | /// must have a single non-explicit conversion function converting to a matching |
| 5244 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5245 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5246 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5247 | /// \param Loc The source location of the construct that requires the |
| 5248 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5249 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5250 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5251 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5252 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5253 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5254 | /// \returns The expression, converted to an integral or enumeration type if |
| 5255 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5256 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5257 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5258 | // We can't perform any more checking for type-dependent expressions. |
| 5259 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5260 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5261 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5262 | // Process placeholders immediately. |
| 5263 | if (From->hasPlaceholderType()) { |
| 5264 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5265 | if (result.isInvalid()) |
| 5266 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5267 | From = result.take(); |
| 5268 | } |
| 5269 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5270 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5271 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5272 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5273 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5274 | |
| 5275 | // FIXME: Check for missing '()' if T is a function type? |
| 5276 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5277 | // We can only perform contextual implicit conversions on objects of class |
| 5278 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5279 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5280 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5281 | if (!Converter.Suppress) |
| 5282 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5283 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5284 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5285 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5286 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5287 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5288 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5289 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5290 | |
| 5291 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5292 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5293 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5294 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5295 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5296 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5297 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5298 | |
| 5299 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5300 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5302 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5303 | UnresolvedSet<4> |
| 5304 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5305 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5306 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5307 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5308 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5309 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5310 | bool HadMultipleCandidates = |
| 5311 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5312 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5313 | // To check that there is only one target type, in C++1y: |
| 5314 | QualType ToType; |
| 5315 | bool HasUniqueTargetType = true; |
| 5316 | |
| 5317 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5318 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5319 | E = Conversions.second; |
| 5320 | I != E; ++I) { |
| 5321 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5322 | CXXConversionDecl *Conversion; |
| 5323 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5324 | if (ConvTemplate) { |
| 5325 | if (getLangOpts().CPlusPlus1y) |
| 5326 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5327 | else |
| 5328 | continue; // C++11 does not consider conversion operator templates(?). |
| 5329 | } else |
| 5330 | Conversion = cast<CXXConversionDecl>(D); |
| 5331 | |
| 5332 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5333 | "Conversion operator templates are considered potentially " |
| 5334 | "viable in C++1y"); |
| 5335 | |
| 5336 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5337 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5338 | |
| 5339 | if (Conversion->isExplicit()) { |
| 5340 | // FIXME: For C++1y, do we need this restriction? |
| 5341 | // cf. diagnoseNoViableConversion() |
| 5342 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5343 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5344 | } else { |
| 5345 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5346 | if (ToType.isNull()) |
| 5347 | ToType = CurToType.getUnqualifiedType(); |
| 5348 | else if (HasUniqueTargetType && |
| 5349 | (CurToType.getUnqualifiedType() != ToType)) |
| 5350 | HasUniqueTargetType = false; |
| 5351 | } |
| 5352 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5353 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5354 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5355 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5357 | if (getLangOpts().CPlusPlus1y) { |
| 5358 | // C++1y [conv]p6: |
| 5359 | // ... An expression e of class type E appearing in such a context |
| 5360 | // is said to be contextually implicitly converted to a specified |
| 5361 | // type T and is well-formed if and only if e can be implicitly |
| 5362 | // 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] | 5363 | // for conversion functions whose return type is cv T or reference to |
| 5364 | // 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] | 5365 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5366 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5367 | // If no unique T is found: |
| 5368 | if (ToType.isNull()) { |
| 5369 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5370 | HadMultipleCandidates, |
| 5371 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5372 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5373 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5374 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5376 | // If more than one unique Ts are found: |
| 5377 | if (!HasUniqueTargetType) |
| 5378 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5379 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5380 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5381 | // If one unique T is found: |
| 5382 | // First, build a candidate set from the previously recorded |
| 5383 | // potentially viable conversions. |
| 5384 | OverloadCandidateSet CandidateSet(Loc); |
| 5385 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5386 | CandidateSet); |
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 | // Then, perform overload resolution over the candidate set. |
| 5389 | OverloadCandidateSet::iterator Best; |
| 5390 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5391 | case OR_Success: { |
| 5392 | // Apply this conversion. |
| 5393 | DeclAccessPair Found = |
| 5394 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5395 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5396 | HadMultipleCandidates, Found)) |
| 5397 | return ExprError(); |
| 5398 | break; |
| 5399 | } |
| 5400 | case OR_Ambiguous: |
| 5401 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5402 | ViableConversions); |
| 5403 | case OR_No_Viable_Function: |
| 5404 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5405 | HadMultipleCandidates, |
| 5406 | ExplicitConversions)) |
| 5407 | return ExprError(); |
| 5408 | // fall through 'OR_Deleted' case. |
| 5409 | case OR_Deleted: |
| 5410 | // We'll complain below about a non-integral condition type. |
| 5411 | break; |
| 5412 | } |
| 5413 | } else { |
| 5414 | switch (ViableConversions.size()) { |
| 5415 | case 0: { |
| 5416 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5417 | HadMultipleCandidates, |
| 5418 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5419 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5421 | // We'll complain below about a non-integral condition type. |
| 5422 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5423 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5424 | case 1: { |
| 5425 | // Apply this conversion. |
| 5426 | DeclAccessPair Found = ViableConversions[0]; |
| 5427 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5428 | HadMultipleCandidates, Found)) |
| 5429 | return ExprError(); |
| 5430 | break; |
| 5431 | } |
| 5432 | default: |
| 5433 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5434 | ViableConversions); |
| 5435 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5436 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5437 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5438 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5441 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5442 | /// candidate functions, using the given function call arguments. If |
| 5443 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5444 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5445 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5446 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5447 | /// based on an incomplete set of function arguments. This feature is used by |
| 5448 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5449 | void |
| 5450 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5451 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5452 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5453 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5454 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5455 | bool PartialOverloading, |
| 5456 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5457 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5458 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5459 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5460 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5461 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5463 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5464 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5465 | // If we get here, it's because we're calling a member function |
| 5466 | // that is named without a member access expression (e.g., |
| 5467 | // "this->f") that was either written explicitly or created |
| 5468 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5469 | // function, e.g., X::f(). We use an empty type for the implied |
| 5470 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5471 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5472 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5473 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5474 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5475 | return; |
| 5476 | } |
| 5477 | // We treat a constructor like a non-member function, since its object |
| 5478 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5481 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5482 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5483 | |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5484 | // C++11 [class.copy]p11: [DR1402] |
| 5485 | // A defaulted move constructor that is defined as deleted is ignored by |
| 5486 | // overload resolution. |
| 5487 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function); |
| 5488 | if (Constructor && Constructor->isDefaulted() && Constructor->isDeleted() && |
| 5489 | Constructor->isMoveConstructor()) |
| 5490 | return; |
| 5491 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5492 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5493 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5494 | |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5495 | if (Constructor) { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5496 | // C++ [class.copy]p3: |
| 5497 | // A member function template is never instantiated to perform the copy |
| 5498 | // of a class object to an object of its class type. |
| 5499 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5500 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5501 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5502 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5503 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5504 | return; |
| 5505 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5507 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5508 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5509 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5510 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5511 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5512 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5513 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5514 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5516 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5517 | |
| 5518 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5519 | // parameters is viable only if it has an ellipsis in its parameter |
| 5520 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5521 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5522 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5523 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5524 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5525 | return; |
| 5526 | } |
| 5527 | |
| 5528 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5529 | // is viable only if the (m+1)st parameter has a default argument |
| 5530 | // (8.3.6). For the purposes of overload resolution, the |
| 5531 | // parameter list is truncated on the right, so that there are |
| 5532 | // exactly m parameters. |
| 5533 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5534 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5535 | // Not enough arguments. |
| 5536 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5537 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5538 | return; |
| 5539 | } |
| 5540 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5541 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5542 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5543 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5544 | if (CheckCUDATarget(Caller, Function)) { |
| 5545 | Candidate.Viable = false; |
| 5546 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5547 | return; |
| 5548 | } |
| 5549 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5550 | // Determine the implicit conversion sequences for each of the |
| 5551 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5552 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5553 | if (ArgIdx < NumArgsInProto) { |
| 5554 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5555 | // exist for each argument an implicit conversion sequence |
| 5556 | // (13.3.3.1) that converts that argument to the corresponding |
| 5557 | // parameter of F. |
| 5558 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5559 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5560 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5561 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5562 | /*InOverloadResolution=*/true, |
| 5563 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5564 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5565 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5566 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5567 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5568 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5569 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5570 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5571 | } else { |
| 5572 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5573 | // argument for which there is no corresponding parameter is |
| 5574 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5575 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5576 | } |
| 5577 | } |
| 5578 | } |
| 5579 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5580 | /// \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] | 5581 | /// the overload candidate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5582 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5583 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5584 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5585 | bool SuppressUserConversions, |
| 5586 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5587 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5588 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5589 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5590 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5591 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5592 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5593 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5594 | Args.slice(1), CandidateSet, |
| 5595 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5596 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5597 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5598 | SuppressUserConversions); |
| 5599 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5600 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5601 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5602 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5603 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5604 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5605 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5607 | Args[0]->Classify(Context), Args.slice(1), |
| 5608 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5609 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5610 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5611 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5612 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5613 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5614 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5617 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5618 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5619 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5620 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5621 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5622 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5623 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5624 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5625 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5626 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5627 | |
| 5628 | if (isa<UsingShadowDecl>(Decl)) |
| 5629 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5630 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5631 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5632 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5633 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5634 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5635 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5636 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5637 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5638 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5639 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5640 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5641 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5642 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5643 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5644 | } |
| 5645 | } |
| 5646 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5647 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5648 | /// of candidate functions, using the given function call arguments |
| 5649 | /// and the object argument (@c Object). For example, in a call |
| 5650 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5651 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5652 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5653 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5654 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5655 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5656 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5657 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5658 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5659 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5660 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5661 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5662 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5663 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5664 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5665 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5667 | if (!CandidateSet.isNewCandidate(Method)) |
| 5668 | return; |
| 5669 | |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 5670 | // C++11 [class.copy]p23: [DR1402] |
| 5671 | // A defaulted move assignment operator that is defined as deleted is |
| 5672 | // ignored by overload resolution. |
| 5673 | if (Method->isDefaulted() && Method->isDeleted() && |
| 5674 | Method->isMoveAssignmentOperator()) |
| 5675 | return; |
| 5676 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5677 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5678 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5679 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5680 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5681 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5682 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5683 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5684 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5685 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5686 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5687 | |
| 5688 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5689 | |
| 5690 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5691 | // parameters is viable only if it has an ellipsis in its parameter |
| 5692 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5693 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5694 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5695 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5696 | return; |
| 5697 | } |
| 5698 | |
| 5699 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5700 | // is viable only if the (m+1)st parameter has a default argument |
| 5701 | // (8.3.6). For the purposes of overload resolution, the |
| 5702 | // parameter list is truncated on the right, so that there are |
| 5703 | // exactly m parameters. |
| 5704 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5705 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5706 | // Not enough arguments. |
| 5707 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5708 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5709 | return; |
| 5710 | } |
| 5711 | |
| 5712 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5713 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5714 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5715 | // The implicit object argument is ignored. |
| 5716 | Candidate.IgnoreObjectArgument = true; |
| 5717 | else { |
| 5718 | // Determine the implicit conversion sequence for the object |
| 5719 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5720 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5721 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5722 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5723 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5724 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5725 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5726 | return; |
| 5727 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | // Determine the implicit conversion sequences for each of the |
| 5731 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5732 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5733 | if (ArgIdx < NumArgsInProto) { |
| 5734 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5735 | // exist for each argument an implicit conversion sequence |
| 5736 | // (13.3.3.1) that converts that argument to the corresponding |
| 5737 | // parameter of F. |
| 5738 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5739 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5740 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5741 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5742 | /*InOverloadResolution=*/true, |
| 5743 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5744 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5745 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5746 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5747 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5748 | break; |
| 5749 | } |
| 5750 | } else { |
| 5751 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5752 | // argument for which there is no corresponding parameter is |
| 5753 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5754 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5755 | } |
| 5756 | } |
| 5757 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5758 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5759 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5760 | /// set, using template argument deduction to produce an appropriate member |
| 5761 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5762 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5763 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5764 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5765 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5766 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5767 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5768 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5769 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5770 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5771 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5772 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5773 | return; |
| 5774 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5775 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5776 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5777 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5778 | // 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] | 5779 | // candidate functions in the usual way.113) A given name can refer to one |
| 5780 | // or more function templates and also to a set of overloaded non-template |
| 5781 | // functions. In such a case, the candidate functions generated from each |
| 5782 | // function template are combined with the set of non-template candidate |
| 5783 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5784 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5785 | FunctionDecl *Specialization = 0; |
| 5786 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5787 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5788 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5789 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5790 | Candidate.FoundDecl = FoundDecl; |
| 5791 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5792 | Candidate.Viable = false; |
| 5793 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5794 | Candidate.IsSurrogate = false; |
| 5795 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5796 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5798 | Info); |
| 5799 | return; |
| 5800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5801 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5802 | // Add the function template specialization produced by template argument |
| 5803 | // deduction as a candidate. |
| 5804 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5805 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5806 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5807 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5808 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5809 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5812 | /// \brief Add a C++ function template specialization as a candidate |
| 5813 | /// in the candidate set, using template argument deduction to produce |
| 5814 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5815 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5816 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5817 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5818 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5819 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5820 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5821 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5822 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5823 | return; |
| 5824 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5825 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5826 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5827 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5828 | // 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] | 5829 | // candidate functions in the usual way.113) A given name can refer to one |
| 5830 | // or more function templates and also to a set of overloaded non-template |
| 5831 | // functions. In such a case, the candidate functions generated from each |
| 5832 | // function template are combined with the set of non-template candidate |
| 5833 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5834 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5835 | FunctionDecl *Specialization = 0; |
| 5836 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5837 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5838 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5839 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5840 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5841 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5842 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5843 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5844 | Candidate.IsSurrogate = false; |
| 5845 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5846 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5847 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5848 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5849 | return; |
| 5850 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5851 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5852 | // Add the function template specialization produced by template argument |
| 5853 | // deduction as a candidate. |
| 5854 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5855 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5856 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5857 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5858 | |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5859 | /// Determine whether this is an allowable conversion from the result |
| 5860 | /// of an explicit conversion operator to the expected type, per C++ |
| 5861 | /// [over.match.conv]p1 and [over.match.ref]p1. |
| 5862 | /// |
| 5863 | /// \param ConvType The return type of the conversion function. |
| 5864 | /// |
| 5865 | /// \param ToType The type we are converting to. |
| 5866 | /// |
| 5867 | /// \param AllowObjCPointerConversion Allow a conversion from one |
| 5868 | /// Objective-C pointer to another. |
| 5869 | /// |
| 5870 | /// \returns true if the conversion is allowable, false otherwise. |
| 5871 | static bool isAllowableExplicitConversion(Sema &S, |
| 5872 | QualType ConvType, QualType ToType, |
| 5873 | bool AllowObjCPointerConversion) { |
| 5874 | QualType ToNonRefType = ToType.getNonReferenceType(); |
| 5875 | |
| 5876 | // Easy case: the types are the same. |
| 5877 | if (S.Context.hasSameUnqualifiedType(ConvType, ToNonRefType)) |
| 5878 | return true; |
| 5879 | |
| 5880 | // Allow qualification conversions. |
| 5881 | bool ObjCLifetimeConversion; |
| 5882 | if (S.IsQualificationConversion(ConvType, ToNonRefType, /*CStyle*/false, |
| 5883 | ObjCLifetimeConversion)) |
| 5884 | return true; |
| 5885 | |
| 5886 | // If we're not allowed to consider Objective-C pointer conversions, |
| 5887 | // we're done. |
| 5888 | if (!AllowObjCPointerConversion) |
| 5889 | return false; |
| 5890 | |
| 5891 | // Is this an Objective-C pointer conversion? |
| 5892 | bool IncompatibleObjC = false; |
| 5893 | QualType ConvertedType; |
| 5894 | return S.isObjCPointerConversion(ConvType, ToNonRefType, ConvertedType, |
| 5895 | IncompatibleObjC); |
| 5896 | } |
| 5897 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5898 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5899 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5900 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5901 | /// 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] | 5902 | /// (which may or may not be the same type as the type that the |
| 5903 | /// conversion function produces). |
| 5904 | void |
| 5905 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5906 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5907 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5908 | Expr *From, QualType ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5909 | OverloadCandidateSet& CandidateSet, |
| 5910 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5911 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5912 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5913 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5914 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5915 | return; |
| 5916 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5917 | // If the conversion function has an undeduced return type, trigger its |
| 5918 | // deduction now. |
| 5919 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5920 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5921 | return; |
| 5922 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5923 | } |
| 5924 | |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5925 | // Per C++ [over.match.conv]p1, [over.match.ref]p1, an explicit conversion |
| 5926 | // operator is only a candidate if its return type is the target type or |
| 5927 | // can be converted to the target type with a qualification conversion. |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 5928 | if (Conversion->isExplicit() && |
| 5929 | !isAllowableExplicitConversion(*this, ConvType, ToType, |
| 5930 | AllowObjCConversionOnExplicit)) |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 5931 | return; |
| 5932 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5933 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5934 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5935 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5936 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5937 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5938 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5939 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5940 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5941 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5942 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5943 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5944 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5945 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5946 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5947 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5948 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | // For conversion functions, the function is considered to be a member of |
| 5950 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5951 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5952 | // |
| 5953 | // Determine the implicit conversion sequence for the implicit |
| 5954 | // object parameter. |
| 5955 | QualType ImplicitParamType = From->getType(); |
| 5956 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5957 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5958 | CXXRecordDecl *ConversionContext |
| 5959 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5960 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5961 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5962 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5963 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5964 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5965 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5966 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5967 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5968 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5969 | return; |
| 5970 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5971 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5972 | // 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] | 5973 | // derived to base as such conversions are given Conversion Rank. They only |
| 5974 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5975 | QualType FromCanon |
| 5976 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5977 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5978 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5979 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5980 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5981 | return; |
| 5982 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5983 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5984 | // To determine what the conversion from the result of calling the |
| 5985 | // conversion function to the type we're eventually trying to |
| 5986 | // convert to (ToType), we need to synthesize a call to the |
| 5987 | // conversion function and attempt copy initialization from it. This |
| 5988 | // makes sure that we get the right semantics with respect to |
| 5989 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5990 | // call on the stack and we don't need its arguments to be |
| 5991 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5992 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5993 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5994 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5995 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5996 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5997 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5998 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5999 | QualType ConversionType = Conversion->getConversionType(); |
| 6000 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 6001 | Candidate.Viable = false; |
| 6002 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 6003 | return; |
| 6004 | } |
| 6005 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6006 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6007 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6008 | // 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] | 6009 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 6010 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 6011 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 6012 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 6013 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6014 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6015 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6016 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6017 | /*InOverloadResolution=*/false, |
| 6018 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6019 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6020 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6021 | case ImplicitConversionSequence::StandardConversion: |
| 6022 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6023 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6024 | // C++ [over.ics.user]p3: |
| 6025 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6026 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6027 | // shall have exact match rank. |
| 6028 | if (Conversion->getPrimaryTemplate() && |
| 6029 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 6030 | Candidate.Viable = false; |
| 6031 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 6032 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6033 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6034 | // C++0x [dcl.init.ref]p5: |
| 6035 | // In the second case, if the reference is an rvalue reference and |
| 6036 | // the second standard conversion sequence of the user-defined |
| 6037 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 6038 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6039 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 6040 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 6041 | Candidate.Viable = false; |
| 6042 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 6043 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6044 | break; |
| 6045 | |
| 6046 | case ImplicitConversionSequence::BadConversion: |
| 6047 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6048 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6049 | break; |
| 6050 | |
| 6051 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6052 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6053 | "Can only end up with a standard conversion sequence or failure"); |
| 6054 | } |
| 6055 | } |
| 6056 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6057 | /// \brief Adds a conversion function template specialization |
| 6058 | /// candidate to the overload set, using template argument deduction |
| 6059 | /// to deduce the template arguments of the conversion function |
| 6060 | /// template from the type that we are converting to (C++ |
| 6061 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6062 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6063 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6064 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6065 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6066 | Expr *From, QualType ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6067 | OverloadCandidateSet &CandidateSet, |
| 6068 | bool AllowObjCConversionOnExplicit) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6069 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 6070 | "Only conversion function templates permitted here"); |
| 6071 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6072 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 6073 | return; |
| 6074 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6075 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6076 | CXXConversionDecl *Specialization = 0; |
| 6077 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6078 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6079 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6080 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6081 | Candidate.FoundDecl = FoundDecl; |
| 6082 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 6083 | Candidate.Viable = false; |
| 6084 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6085 | Candidate.IsSurrogate = false; |
| 6086 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6087 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6088 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6089 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6090 | return; |
| 6091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6092 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6093 | // Add the conversion function template specialization produced by |
| 6094 | // template argument deduction as a candidate. |
| 6095 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6096 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
Douglas Gregor | 1ce5509 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 6097 | CandidateSet, AllowObjCConversionOnExplicit); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6100 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6101 | /// converts the given @c Object to a function pointer via the |
| 6102 | /// conversion function @c Conversion, and then attempts to call it |
| 6103 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6104 | /// the type of function that we'll eventually be calling. |
| 6105 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6106 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6107 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6108 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6109 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6110 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6111 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6112 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6113 | return; |
| 6114 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6115 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6116 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6117 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6118 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6119 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6120 | Candidate.Function = 0; |
| 6121 | Candidate.Surrogate = Conversion; |
| 6122 | Candidate.Viable = true; |
| 6123 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6124 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6125 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6126 | |
| 6127 | // Determine the implicit conversion sequence for the implicit |
| 6128 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6129 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6130 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6131 | Object->Classify(Context), |
| 6132 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6133 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6134 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6135 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6136 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6137 | return; |
| 6138 | } |
| 6139 | |
| 6140 | // The first conversion is actually a user-defined conversion whose |
| 6141 | // first conversion is ObjectInit's standard conversion (which is |
| 6142 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6143 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6144 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6145 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6146 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6147 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6148 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6149 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6150 | = Candidate.Conversions[0].UserDefined.Before; |
| 6151 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6152 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6153 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6154 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6155 | |
| 6156 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6157 | // parameters is viable only if it has an ellipsis in its parameter |
| 6158 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6159 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6160 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6161 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6162 | return; |
| 6163 | } |
| 6164 | |
| 6165 | // Function types don't have any default arguments, so just check if |
| 6166 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6167 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6168 | // Not enough arguments. |
| 6169 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6170 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6171 | return; |
| 6172 | } |
| 6173 | |
| 6174 | // Determine the implicit conversion sequences for each of the |
| 6175 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6176 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6177 | if (ArgIdx < NumArgsInProto) { |
| 6178 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6179 | // exist for each argument an implicit conversion sequence |
| 6180 | // (13.3.3.1) that converts that argument to the corresponding |
| 6181 | // parameter of F. |
| 6182 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6183 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6184 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6185 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6186 | /*InOverloadResolution=*/false, |
| 6187 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6188 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6189 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6190 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6191 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6192 | break; |
| 6193 | } |
| 6194 | } else { |
| 6195 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6196 | // argument for which there is no corresponding parameter is |
| 6197 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6198 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6199 | } |
| 6200 | } |
| 6201 | } |
| 6202 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6203 | /// \brief Add overload candidates for overloaded operators that are |
| 6204 | /// member functions. |
| 6205 | /// |
| 6206 | /// Add the overloaded operator candidates that are member functions |
| 6207 | /// for the operator Op that was used in an operator expression such |
| 6208 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6209 | /// CandidateSet will store the added overload candidates. (C++ |
| 6210 | /// [over.match.oper]). |
| 6211 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6212 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6213 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6214 | OverloadCandidateSet& CandidateSet, |
| 6215 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6216 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6217 | |
| 6218 | // C++ [over.match.oper]p3: |
| 6219 | // For a unary operator @ with an operand of a type whose |
| 6220 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6221 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6222 | // a right operand of a type whose cv-unqualified version is T2, |
| 6223 | // three sets of candidate functions, designated member |
| 6224 | // candidates, non-member candidates and built-in candidates, are |
| 6225 | // constructed as follows: |
| 6226 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6227 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6228 | // -- If T1 is a complete class type or a class currently being |
| 6229 | // defined, the set of member candidates is the result of the |
| 6230 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6231 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6232 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6233 | // Complete the type if it can be completed. |
| 6234 | RequireCompleteType(OpLoc, T1, 0); |
| 6235 | // If the type is neither complete nor being defined, bail out now. |
| 6236 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6237 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6238 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6239 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6240 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6241 | Operators.suppressDiagnostics(); |
| 6242 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6244 | OperEnd = Operators.end(); |
| 6245 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6246 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6247 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6248 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6249 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6250 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6251 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6252 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6255 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6256 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6257 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6258 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6259 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6260 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6261 | /// (at the beginning of the argument list) that will be contextually |
| 6262 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6263 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6264 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6265 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6266 | bool IsAssignmentOperator, |
| 6267 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6268 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6269 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6270 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6271 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6272 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6273 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6274 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6275 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6276 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6277 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6278 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6279 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6280 | |
| 6281 | // Determine the implicit conversion sequences for each of the |
| 6282 | // arguments. |
| 6283 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6284 | Candidate.ExplicitCallArguments = Args.size(); |
| 6285 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6286 | // C++ [over.match.oper]p4: |
| 6287 | // For the built-in assignment operators, conversions of the |
| 6288 | // left operand are restricted as follows: |
| 6289 | // -- no temporaries are introduced to hold the left operand, and |
| 6290 | // -- no user-defined conversions are applied to the left |
| 6291 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6292 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6293 | // |
| 6294 | // We block these conversions by turning off user-defined |
| 6295 | // conversions, since that is the only way that initialization of |
| 6296 | // a reference to a non-class type can occur from something that |
| 6297 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6298 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6299 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6300 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6301 | Candidate.Conversions[ArgIdx] |
| 6302 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6303 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6304 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6305 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6306 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6307 | /*InOverloadResolution=*/false, |
| 6308 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6309 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6310 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6311 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6312 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6313 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6314 | break; |
| 6315 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6316 | } |
| 6317 | } |
| 6318 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6319 | namespace { |
| 6320 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6321 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6322 | /// candidate operator functions for built-in operators (C++ |
| 6323 | /// [over.built]). The types are separated into pointer types and |
| 6324 | /// enumeration types. |
| 6325 | class BuiltinCandidateTypeSet { |
| 6326 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6327 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6328 | |
| 6329 | /// PointerTypes - The set of pointer types that will be used in the |
| 6330 | /// built-in candidates. |
| 6331 | TypeSet PointerTypes; |
| 6332 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6333 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6334 | /// used in the built-in candidates. |
| 6335 | TypeSet MemberPointerTypes; |
| 6336 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6337 | /// EnumerationTypes - The set of enumeration types that will be |
| 6338 | /// used in the built-in candidates. |
| 6339 | TypeSet EnumerationTypes; |
| 6340 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6341 | /// \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] | 6342 | /// candidates. |
| 6343 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6344 | |
| 6345 | /// \brief A flag indicating non-record types are viable candidates |
| 6346 | bool HasNonRecordTypes; |
| 6347 | |
| 6348 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6349 | /// were present in the candidate set. |
| 6350 | bool HasArithmeticOrEnumeralTypes; |
| 6351 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6352 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6353 | /// candidate set. |
| 6354 | bool HasNullPtrType; |
| 6355 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6356 | /// Sema - The semantic analysis instance where we are building the |
| 6357 | /// candidate type set. |
| 6358 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6359 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6360 | /// Context - The AST context in which we will build the type sets. |
| 6361 | ASTContext &Context; |
| 6362 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6363 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6364 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6365 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6366 | |
| 6367 | public: |
| 6368 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6369 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6370 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6371 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6372 | : HasNonRecordTypes(false), |
| 6373 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6374 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6375 | SemaRef(SemaRef), |
| 6376 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6377 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6378 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6379 | SourceLocation Loc, |
| 6380 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6381 | bool AllowExplicitConversions, |
| 6382 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6383 | |
| 6384 | /// pointer_begin - First pointer type found; |
| 6385 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6386 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6387 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6388 | iterator pointer_end() { return PointerTypes.end(); } |
| 6389 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6390 | /// member_pointer_begin - First member pointer type found; |
| 6391 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6392 | |
| 6393 | /// member_pointer_end - Past the last member pointer type found; |
| 6394 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6395 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6396 | /// enumeration_begin - First enumeration type found; |
| 6397 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6398 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6399 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6400 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6401 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6402 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6403 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6404 | |
| 6405 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6406 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6407 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6408 | }; |
| 6409 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6410 | } // end anonymous namespace |
| 6411 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6412 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6413 | /// the set of pointer types along with any more-qualified variants of |
| 6414 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6415 | /// will add "int const *", "int const volatile *", "int const |
| 6416 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6417 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6418 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6419 | /// |
| 6420 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6421 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6422 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6423 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6424 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6425 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6426 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6427 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6429 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6430 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6431 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6432 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6433 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6434 | PointeeTy = PTy->getPointeeType(); |
| 6435 | buildObjCPtr = true; |
| 6436 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6437 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6440 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6441 | // (the qualifier would sink to the element type), and for another, the |
| 6442 | // only overload situation where it matters is subscript or pointer +- int, |
| 6443 | // and those shouldn't have qualifier variants anyway. |
| 6444 | if (PointeeTy->isArrayType()) |
| 6445 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6446 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6447 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6448 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6449 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6450 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6451 | // Iterate through all strict supersets of BaseCVR. |
| 6452 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6453 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6454 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6455 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6456 | |
| 6457 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6458 | // the type cannot be restrict-qualified. |
| 6459 | if ((CVR & Qualifiers::Restrict) && |
| 6460 | (!hasRestrict || |
| 6461 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6462 | continue; |
| 6463 | |
| 6464 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6465 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6466 | |
| 6467 | // Build qualified pointer type. |
| 6468 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6469 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6470 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6471 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6472 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6473 | |
| 6474 | // Insert qualified pointer type. |
| 6475 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | return true; |
| 6479 | } |
| 6480 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6481 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6482 | /// to the set of pointer types along with any more-qualified variants of |
| 6483 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6484 | /// will add "int const *", "int const volatile *", "int const |
| 6485 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6486 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6487 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6488 | /// |
| 6489 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6490 | bool |
| 6491 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6492 | QualType Ty) { |
| 6493 | // Insert this type. |
| 6494 | if (!MemberPointerTypes.insert(Ty)) |
| 6495 | return false; |
| 6496 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6497 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6498 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6499 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6500 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6501 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6502 | // (the qualifier would sink to the element type), and for another, the |
| 6503 | // only overload situation where it matters is subscript or pointer +- int, |
| 6504 | // and those shouldn't have qualifier variants anyway. |
| 6505 | if (PointeeTy->isArrayType()) |
| 6506 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6507 | const Type *ClassTy = PointerTy->getClass(); |
| 6508 | |
| 6509 | // Iterate through all strict supersets of the pointee type's CVR |
| 6510 | // qualifiers. |
| 6511 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6512 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6513 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6514 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6515 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6516 | MemberPointerTypes.insert( |
| 6517 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | return true; |
| 6521 | } |
| 6522 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6523 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6524 | /// 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] | 6525 | /// primarily interested in pointer types and enumeration types. We also |
| 6526 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6527 | /// AllowUserConversions is true if we should look at the conversion |
| 6528 | /// functions of a class type, and AllowExplicitConversions if we |
| 6529 | /// should also include the explicit conversion functions of a class |
| 6530 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6531 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6532 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6533 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6534 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6535 | bool AllowExplicitConversions, |
| 6536 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6537 | // Only deal with canonical types. |
| 6538 | Ty = Context.getCanonicalType(Ty); |
| 6539 | |
| 6540 | // Look through reference types; they aren't part of the type of an |
| 6541 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6542 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6543 | Ty = RefTy->getPointeeType(); |
| 6544 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6545 | // If we're dealing with an array type, decay to the pointer. |
| 6546 | if (Ty->isArrayType()) |
| 6547 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6548 | |
| 6549 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6550 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6551 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6552 | // Flag if we ever add a non-record type. |
| 6553 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6554 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6555 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6556 | // Flag if we encounter an arithmetic type. |
| 6557 | HasArithmeticOrEnumeralTypes = |
| 6558 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6559 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6560 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6561 | PointerTypes.insert(Ty); |
| 6562 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6563 | // Insert our type, and its more-qualified variants, into the set |
| 6564 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6565 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6566 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6567 | } else if (Ty->isMemberPointerType()) { |
| 6568 | // Member pointers are far easier, since the pointee can't be converted. |
| 6569 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6570 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6571 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6572 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6573 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6574 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6575 | // We treat vector types as arithmetic types in many contexts as an |
| 6576 | // extension. |
| 6577 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6578 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6579 | } else if (Ty->isNullPtrType()) { |
| 6580 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6581 | } else if (AllowUserConversions && TyRec) { |
| 6582 | // No conversion functions in incomplete types. |
| 6583 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6584 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6585 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6586 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6587 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6588 | CXXRecordDecl::conversion_iterator> |
| 6589 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6590 | for (CXXRecordDecl::conversion_iterator |
| 6591 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6592 | NamedDecl *D = I.getDecl(); |
| 6593 | if (isa<UsingShadowDecl>(D)) |
| 6594 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6595 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6596 | // Skip conversion function templates; they don't tell us anything |
| 6597 | // about which builtin types we can convert to. |
| 6598 | if (isa<FunctionTemplateDecl>(D)) |
| 6599 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6600 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6601 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6602 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6603 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6604 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6605 | } |
| 6606 | } |
| 6607 | } |
| 6608 | } |
| 6609 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6610 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6611 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6612 | /// given type to the candidate set. |
| 6613 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6614 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6615 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6616 | OverloadCandidateSet &CandidateSet) { |
| 6617 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6618 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6619 | // T& operator=(T&, T) |
| 6620 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6621 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6622 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6623 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6624 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6625 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6626 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6627 | ParamTypes[0] |
| 6628 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6629 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6630 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6631 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6632 | } |
| 6633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6634 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6635 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6636 | /// 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] | 6637 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6638 | Qualifiers VRQuals; |
| 6639 | const RecordType *TyRec; |
| 6640 | if (const MemberPointerType *RHSMPType = |
| 6641 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6642 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6643 | else |
| 6644 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6645 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6646 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6647 | VRQuals.addVolatile(); |
| 6648 | VRQuals.addRestrict(); |
| 6649 | return VRQuals; |
| 6650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6651 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6652 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6653 | if (!ClassDecl->hasDefinition()) |
| 6654 | return VRQuals; |
| 6655 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6656 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6657 | CXXRecordDecl::conversion_iterator> |
| 6658 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6660 | for (CXXRecordDecl::conversion_iterator |
| 6661 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6662 | NamedDecl *D = I.getDecl(); |
| 6663 | if (isa<UsingShadowDecl>(D)) |
| 6664 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6665 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6666 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6667 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6668 | CanTy = ResTypeRef->getPointeeType(); |
| 6669 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6670 | // as see them. |
| 6671 | bool done = false; |
| 6672 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6673 | if (CanTy.isRestrictQualified()) |
| 6674 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6675 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6676 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6677 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6678 | CanTy->getAs<MemberPointerType>()) |
| 6679 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6680 | else |
| 6681 | done = true; |
| 6682 | if (CanTy.isVolatileQualified()) |
| 6683 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6684 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6685 | return VRQuals; |
| 6686 | } |
| 6687 | } |
| 6688 | } |
| 6689 | return VRQuals; |
| 6690 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6691 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6692 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6693 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6694 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6695 | /// candidates. It provides shared state and utility methods used throughout |
| 6696 | /// the process, as well as a helper method to add each group of builtin |
| 6697 | /// operator overloads from the standard to a candidate set. |
| 6698 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6699 | // Common instance state available to all overload candidate addition methods. |
| 6700 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6701 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6702 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6703 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6704 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6705 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6706 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6707 | // Define some constants used to index and iterate over the arithemetic types |
| 6708 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6709 | // The "promoted arithmetic types" are the arithmetic |
| 6710 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6711 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6712 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6713 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6714 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6715 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6716 | LastPromotedArithmeticType = 11; |
| 6717 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6718 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6719 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6720 | CanQualType getArithmeticType(unsigned index) { |
| 6721 | assert(index < NumArithmeticTypes); |
| 6722 | static CanQualType ASTContext::* const |
| 6723 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6724 | // Start of promoted types. |
| 6725 | &ASTContext::FloatTy, |
| 6726 | &ASTContext::DoubleTy, |
| 6727 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6728 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6729 | // Start of integral types. |
| 6730 | &ASTContext::IntTy, |
| 6731 | &ASTContext::LongTy, |
| 6732 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6733 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6734 | &ASTContext::UnsignedIntTy, |
| 6735 | &ASTContext::UnsignedLongTy, |
| 6736 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6737 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6738 | // End of promoted types. |
| 6739 | |
| 6740 | &ASTContext::BoolTy, |
| 6741 | &ASTContext::CharTy, |
| 6742 | &ASTContext::WCharTy, |
| 6743 | &ASTContext::Char16Ty, |
| 6744 | &ASTContext::Char32Ty, |
| 6745 | &ASTContext::SignedCharTy, |
| 6746 | &ASTContext::ShortTy, |
| 6747 | &ASTContext::UnsignedCharTy, |
| 6748 | &ASTContext::UnsignedShortTy, |
| 6749 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6750 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6751 | }; |
| 6752 | return S.Context.*ArithmeticTypes[index]; |
| 6753 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6754 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6755 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6756 | /// converions for the given arithmetic types. |
| 6757 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6758 | // Accelerator table for performing the usual arithmetic conversions. |
| 6759 | // The rules are basically: |
| 6760 | // - if either is floating-point, use the wider floating-point |
| 6761 | // - if same signedness, use the higher rank |
| 6762 | // - if same size, use unsigned of the higher rank |
| 6763 | // - use the larger type |
| 6764 | // These rules, together with the axiom that higher ranks are |
| 6765 | // never smaller, are sufficient to precompute all of these results |
| 6766 | // *except* when dealing with signed types of higher rank. |
| 6767 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6768 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6769 | // 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] | 6770 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6771 | Dep=-1, |
| 6772 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6773 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6774 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6775 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6776 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6777 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6778 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6779 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6780 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6781 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6782 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6783 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6784 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6785 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6786 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6787 | }; |
| 6788 | |
| 6789 | assert(L < LastPromotedArithmeticType); |
| 6790 | assert(R < LastPromotedArithmeticType); |
| 6791 | int Idx = ConversionsTable[L][R]; |
| 6792 | |
| 6793 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6794 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6795 | |
| 6796 | // Slow path: we need to compare widths. |
| 6797 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6798 | CanQualType LT = getArithmeticType(L), |
| 6799 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6800 | unsigned LW = S.Context.getIntWidth(LT), |
| 6801 | RW = S.Context.getIntWidth(RT); |
| 6802 | |
| 6803 | // If they're different widths, use the signed type. |
| 6804 | if (LW > RW) return LT; |
| 6805 | else if (LW < RW) return RT; |
| 6806 | |
| 6807 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6808 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6809 | assert(L == SLL || R == SLL); |
| 6810 | return S.Context.UnsignedLongLongTy; |
| 6811 | } |
| 6812 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6813 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6814 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6815 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6816 | bool HasVolatile, |
| 6817 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6818 | QualType ParamTypes[2] = { |
| 6819 | S.Context.getLValueReferenceType(CandidateTy), |
| 6820 | S.Context.IntTy |
| 6821 | }; |
| 6822 | |
| 6823 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6824 | if (Args.size() == 1) |
| 6825 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6826 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6827 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6828 | |
| 6829 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6830 | // add volatile version only if there are conversions to a volatile type. |
| 6831 | if (HasVolatile) { |
| 6832 | ParamTypes[0] = |
| 6833 | S.Context.getLValueReferenceType( |
| 6834 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6835 | if (Args.size() == 1) |
| 6836 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6837 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6838 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6839 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6840 | |
| 6841 | // Add restrict version only if there are conversions to a restrict type |
| 6842 | // and our candidate type is a non-restrict-qualified pointer. |
| 6843 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6844 | !CandidateTy.isRestrictQualified()) { |
| 6845 | ParamTypes[0] |
| 6846 | = S.Context.getLValueReferenceType( |
| 6847 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6848 | if (Args.size() == 1) |
| 6849 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6850 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6851 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6852 | |
| 6853 | if (HasVolatile) { |
| 6854 | ParamTypes[0] |
| 6855 | = S.Context.getLValueReferenceType( |
| 6856 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6857 | (Qualifiers::Volatile | |
| 6858 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6859 | if (Args.size() == 1) |
| 6860 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6861 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6862 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6863 | } |
| 6864 | } |
| 6865 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | public: |
| 6869 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6870 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6871 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6872 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6873 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6874 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6875 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6876 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6877 | HasArithmeticOrEnumeralCandidateType( |
| 6878 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6879 | CandidateTypes(CandidateTypes), |
| 6880 | CandidateSet(CandidateSet) { |
| 6881 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6882 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6883 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6884 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6885 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6886 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6887 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6888 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6889 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6890 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6891 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6892 | "Invalid last promoted arithmetic type"); |
| 6893 | } |
| 6894 | |
| 6895 | // C++ [over.built]p3: |
| 6896 | // |
| 6897 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6898 | // is either volatile or empty, there exist candidate operator |
| 6899 | // functions of the form |
| 6900 | // |
| 6901 | // VQ T& operator++(VQ T&); |
| 6902 | // T operator++(VQ T&, int); |
| 6903 | // |
| 6904 | // C++ [over.built]p4: |
| 6905 | // |
| 6906 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6907 | // than bool, and VQ is either volatile or empty, there exist |
| 6908 | // candidate operator functions of the form |
| 6909 | // |
| 6910 | // VQ T& operator--(VQ T&); |
| 6911 | // T operator--(VQ T&, int); |
| 6912 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6913 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6914 | return; |
| 6915 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6916 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6917 | Arith < NumArithmeticTypes; ++Arith) { |
| 6918 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6919 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6920 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6921 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6922 | } |
| 6923 | } |
| 6924 | |
| 6925 | // C++ [over.built]p5: |
| 6926 | // |
| 6927 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6928 | // cv-unqualified object type, and VQ is either volatile or |
| 6929 | // empty, there exist candidate operator functions of the form |
| 6930 | // |
| 6931 | // T*VQ& operator++(T*VQ&); |
| 6932 | // T*VQ& operator--(T*VQ&); |
| 6933 | // T* operator++(T*VQ&, int); |
| 6934 | // T* operator--(T*VQ&, int); |
| 6935 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6936 | for (BuiltinCandidateTypeSet::iterator |
| 6937 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6938 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6939 | Ptr != PtrEnd; ++Ptr) { |
| 6940 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6941 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6942 | continue; |
| 6943 | |
| 6944 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6945 | (!(*Ptr).isVolatileQualified() && |
| 6946 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6947 | (!(*Ptr).isRestrictQualified() && |
| 6948 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6949 | } |
| 6950 | } |
| 6951 | |
| 6952 | // C++ [over.built]p6: |
| 6953 | // For every cv-qualified or cv-unqualified object type T, there |
| 6954 | // exist candidate operator functions of the form |
| 6955 | // |
| 6956 | // T& operator*(T*); |
| 6957 | // |
| 6958 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6959 | // 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] | 6960 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6961 | // T& operator*(T*); |
| 6962 | void addUnaryStarPointerOverloads() { |
| 6963 | for (BuiltinCandidateTypeSet::iterator |
| 6964 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6965 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6966 | Ptr != PtrEnd; ++Ptr) { |
| 6967 | QualType ParamTy = *Ptr; |
| 6968 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6969 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6970 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6971 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6972 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6973 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6974 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6975 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6976 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6977 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6978 | } |
| 6979 | } |
| 6980 | |
| 6981 | // C++ [over.built]p9: |
| 6982 | // For every promoted arithmetic type T, there exist candidate |
| 6983 | // operator functions of the form |
| 6984 | // |
| 6985 | // T operator+(T); |
| 6986 | // T operator-(T); |
| 6987 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6988 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6989 | return; |
| 6990 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6991 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6992 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6993 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6994 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | // Extension: We also add these operators for vector types. |
| 6998 | for (BuiltinCandidateTypeSet::iterator |
| 6999 | Vec = CandidateTypes[0].vector_begin(), |
| 7000 | VecEnd = CandidateTypes[0].vector_end(); |
| 7001 | Vec != VecEnd; ++Vec) { |
| 7002 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7003 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7004 | } |
| 7005 | } |
| 7006 | |
| 7007 | // C++ [over.built]p8: |
| 7008 | // For every type T, there exist candidate operator functions of |
| 7009 | // the form |
| 7010 | // |
| 7011 | // T* operator+(T*); |
| 7012 | void addUnaryPlusPointerOverloads() { |
| 7013 | for (BuiltinCandidateTypeSet::iterator |
| 7014 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7015 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7016 | Ptr != PtrEnd; ++Ptr) { |
| 7017 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7018 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7019 | } |
| 7020 | } |
| 7021 | |
| 7022 | // C++ [over.built]p10: |
| 7023 | // For every promoted integral type T, there exist candidate |
| 7024 | // operator functions of the form |
| 7025 | // |
| 7026 | // T operator~(T); |
| 7027 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7028 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7029 | return; |
| 7030 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7031 | for (unsigned Int = FirstPromotedIntegralType; |
| 7032 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7033 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7034 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7035 | } |
| 7036 | |
| 7037 | // Extension: We also add this operator for vector types. |
| 7038 | for (BuiltinCandidateTypeSet::iterator |
| 7039 | Vec = CandidateTypes[0].vector_begin(), |
| 7040 | VecEnd = CandidateTypes[0].vector_end(); |
| 7041 | Vec != VecEnd; ++Vec) { |
| 7042 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7043 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7044 | } |
| 7045 | } |
| 7046 | |
| 7047 | // C++ [over.match.oper]p16: |
| 7048 | // For every pointer to member type T, there exist candidate operator |
| 7049 | // functions of the form |
| 7050 | // |
| 7051 | // bool operator==(T,T); |
| 7052 | // bool operator!=(T,T); |
| 7053 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 7054 | /// Set of (canonical) types that we've already handled. |
| 7055 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7056 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7057 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7058 | for (BuiltinCandidateTypeSet::iterator |
| 7059 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7060 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7061 | MemPtr != MemPtrEnd; |
| 7062 | ++MemPtr) { |
| 7063 | // Don't add the same builtin candidate twice. |
| 7064 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7065 | continue; |
| 7066 | |
| 7067 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7068 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7069 | } |
| 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | // C++ [over.built]p15: |
| 7074 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7075 | // For every T, where T is an enumeration type, a pointer type, or |
| 7076 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7077 | // |
| 7078 | // bool operator<(T, T); |
| 7079 | // bool operator>(T, T); |
| 7080 | // bool operator<=(T, T); |
| 7081 | // bool operator>=(T, T); |
| 7082 | // bool operator==(T, T); |
| 7083 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7084 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7085 | // C++ [over.match.oper]p3: |
| 7086 | // [...]the built-in candidates include all of the candidate operator |
| 7087 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 7088 | // do not have the same parameter-type-list as any non-template non-member |
| 7089 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7090 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7091 | // Note that in practice, this only affects enumeration types because there |
| 7092 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7093 | // must have an operand of record or enumeration type. Also, the only other |
| 7094 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7095 | // cannot be overloaded for enumeration types, so this is the only place |
| 7096 | // where we must suppress candidates like this. |
| 7097 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7098 | UserDefinedBinaryOperators; |
| 7099 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7100 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7101 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7102 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7103 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7104 | CEnd = CandidateSet.end(); |
| 7105 | C != CEnd; ++C) { |
| 7106 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7107 | continue; |
| 7108 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7109 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7110 | continue; |
| 7111 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7112 | QualType FirstParamType = |
| 7113 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7114 | QualType SecondParamType = |
| 7115 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7116 | |
| 7117 | // Skip if either parameter isn't of enumeral type. |
| 7118 | if (!FirstParamType->isEnumeralType() || |
| 7119 | !SecondParamType->isEnumeralType()) |
| 7120 | continue; |
| 7121 | |
| 7122 | // Add this operator to the set of known user-defined operators. |
| 7123 | UserDefinedBinaryOperators.insert( |
| 7124 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7125 | S.Context.getCanonicalType(SecondParamType))); |
| 7126 | } |
| 7127 | } |
| 7128 | } |
| 7129 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7130 | /// Set of (canonical) types that we've already handled. |
| 7131 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7132 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7133 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7134 | for (BuiltinCandidateTypeSet::iterator |
| 7135 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7136 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7137 | Ptr != PtrEnd; ++Ptr) { |
| 7138 | // Don't add the same builtin candidate twice. |
| 7139 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7140 | continue; |
| 7141 | |
| 7142 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7143 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7144 | } |
| 7145 | for (BuiltinCandidateTypeSet::iterator |
| 7146 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7147 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7148 | Enum != EnumEnd; ++Enum) { |
| 7149 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7150 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7151 | // Don't add the same builtin candidate twice, or if a user defined |
| 7152 | // candidate exists. |
| 7153 | if (!AddedTypes.insert(CanonType) || |
| 7154 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7155 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7156 | continue; |
| 7157 | |
| 7158 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7159 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7160 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7161 | |
| 7162 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7163 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7164 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7165 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7166 | NullPtrTy))) { |
| 7167 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7168 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7169 | CandidateSet); |
| 7170 | } |
| 7171 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7172 | } |
| 7173 | } |
| 7174 | |
| 7175 | // C++ [over.built]p13: |
| 7176 | // |
| 7177 | // For every cv-qualified or cv-unqualified object type T |
| 7178 | // there exist candidate operator functions of the form |
| 7179 | // |
| 7180 | // T* operator+(T*, ptrdiff_t); |
| 7181 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7182 | // T* operator-(T*, ptrdiff_t); |
| 7183 | // T* operator+(ptrdiff_t, T*); |
| 7184 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7185 | // |
| 7186 | // C++ [over.built]p14: |
| 7187 | // |
| 7188 | // For every T, where T is a pointer to object type, there |
| 7189 | // exist candidate operator functions of the form |
| 7190 | // |
| 7191 | // ptrdiff_t operator-(T, T); |
| 7192 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7193 | /// Set of (canonical) types that we've already handled. |
| 7194 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7195 | |
| 7196 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7197 | QualType AsymetricParamTypes[2] = { |
| 7198 | S.Context.getPointerDiffType(), |
| 7199 | S.Context.getPointerDiffType(), |
| 7200 | }; |
| 7201 | for (BuiltinCandidateTypeSet::iterator |
| 7202 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7203 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7204 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7205 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7206 | if (!PointeeTy->isObjectType()) |
| 7207 | continue; |
| 7208 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7209 | AsymetricParamTypes[Arg] = *Ptr; |
| 7210 | if (Arg == 0 || Op == OO_Plus) { |
| 7211 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7212 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7213 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7214 | } |
| 7215 | if (Op == OO_Minus) { |
| 7216 | // ptrdiff_t operator-(T, T); |
| 7217 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7218 | continue; |
| 7219 | |
| 7220 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7221 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7222 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7223 | } |
| 7224 | } |
| 7225 | } |
| 7226 | } |
| 7227 | |
| 7228 | // C++ [over.built]p12: |
| 7229 | // |
| 7230 | // For every pair of promoted arithmetic types L and R, there |
| 7231 | // exist candidate operator functions of the form |
| 7232 | // |
| 7233 | // LR operator*(L, R); |
| 7234 | // LR operator/(L, R); |
| 7235 | // LR operator+(L, R); |
| 7236 | // LR operator-(L, R); |
| 7237 | // bool operator<(L, R); |
| 7238 | // bool operator>(L, R); |
| 7239 | // bool operator<=(L, R); |
| 7240 | // bool operator>=(L, R); |
| 7241 | // bool operator==(L, R); |
| 7242 | // bool operator!=(L, R); |
| 7243 | // |
| 7244 | // where LR is the result of the usual arithmetic conversions |
| 7245 | // between types L and R. |
| 7246 | // |
| 7247 | // C++ [over.built]p24: |
| 7248 | // |
| 7249 | // For every pair of promoted arithmetic types L and R, there exist |
| 7250 | // candidate operator functions of the form |
| 7251 | // |
| 7252 | // LR operator?(bool, L, R); |
| 7253 | // |
| 7254 | // where LR is the result of the usual arithmetic conversions |
| 7255 | // between types L and R. |
| 7256 | // Our candidates ignore the first parameter. |
| 7257 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7258 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7259 | return; |
| 7260 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7261 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7262 | Left < LastPromotedArithmeticType; ++Left) { |
| 7263 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7264 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7265 | QualType LandR[2] = { getArithmeticType(Left), |
| 7266 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7267 | QualType Result = |
| 7268 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7269 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7270 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7271 | } |
| 7272 | } |
| 7273 | |
| 7274 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7275 | // conditional operator for vector types. |
| 7276 | for (BuiltinCandidateTypeSet::iterator |
| 7277 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7278 | Vec1End = CandidateTypes[0].vector_end(); |
| 7279 | Vec1 != Vec1End; ++Vec1) { |
| 7280 | for (BuiltinCandidateTypeSet::iterator |
| 7281 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7282 | Vec2End = CandidateTypes[1].vector_end(); |
| 7283 | Vec2 != Vec2End; ++Vec2) { |
| 7284 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7285 | QualType Result = S.Context.BoolTy; |
| 7286 | if (!isComparison) { |
| 7287 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7288 | Result = *Vec1; |
| 7289 | else |
| 7290 | Result = *Vec2; |
| 7291 | } |
| 7292 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7293 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | // C++ [over.built]p17: |
| 7299 | // |
| 7300 | // For every pair of promoted integral types L and R, there |
| 7301 | // exist candidate operator functions of the form |
| 7302 | // |
| 7303 | // LR operator%(L, R); |
| 7304 | // LR operator&(L, R); |
| 7305 | // LR operator^(L, R); |
| 7306 | // LR operator|(L, R); |
| 7307 | // L operator<<(L, R); |
| 7308 | // L operator>>(L, R); |
| 7309 | // |
| 7310 | // where LR is the result of the usual arithmetic conversions |
| 7311 | // between types L and R. |
| 7312 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7313 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7314 | return; |
| 7315 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7316 | for (unsigned Left = FirstPromotedIntegralType; |
| 7317 | Left < LastPromotedIntegralType; ++Left) { |
| 7318 | for (unsigned Right = FirstPromotedIntegralType; |
| 7319 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7320 | QualType LandR[2] = { getArithmeticType(Left), |
| 7321 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7322 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7323 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7324 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7325 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7326 | } |
| 7327 | } |
| 7328 | } |
| 7329 | |
| 7330 | // C++ [over.built]p20: |
| 7331 | // |
| 7332 | // For every pair (T, VQ), where T is an enumeration or |
| 7333 | // pointer to member type and VQ is either volatile or |
| 7334 | // empty, there exist candidate operator functions of the form |
| 7335 | // |
| 7336 | // VQ T& operator=(VQ T&, T); |
| 7337 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7338 | /// Set of (canonical) types that we've already handled. |
| 7339 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7340 | |
| 7341 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7342 | for (BuiltinCandidateTypeSet::iterator |
| 7343 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7344 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7345 | Enum != EnumEnd; ++Enum) { |
| 7346 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7347 | continue; |
| 7348 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7349 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | for (BuiltinCandidateTypeSet::iterator |
| 7353 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7354 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7355 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7356 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7357 | continue; |
| 7358 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7359 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7360 | } |
| 7361 | } |
| 7362 | } |
| 7363 | |
| 7364 | // C++ [over.built]p19: |
| 7365 | // |
| 7366 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7367 | // volatile or empty, there exist candidate operator functions |
| 7368 | // of the form |
| 7369 | // |
| 7370 | // T*VQ& operator=(T*VQ&, T*); |
| 7371 | // |
| 7372 | // C++ [over.built]p21: |
| 7373 | // |
| 7374 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7375 | // cv-unqualified object type and VQ is either volatile or |
| 7376 | // empty, there exist candidate operator functions of the form |
| 7377 | // |
| 7378 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7379 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7380 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7381 | /// Set of (canonical) types that we've already handled. |
| 7382 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7383 | |
| 7384 | for (BuiltinCandidateTypeSet::iterator |
| 7385 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7386 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7387 | Ptr != PtrEnd; ++Ptr) { |
| 7388 | // If this is operator=, keep track of the builtin candidates we added. |
| 7389 | if (isEqualOp) |
| 7390 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7391 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7392 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7393 | |
| 7394 | // non-volatile version |
| 7395 | QualType ParamTypes[2] = { |
| 7396 | S.Context.getLValueReferenceType(*Ptr), |
| 7397 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7398 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7399 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7400 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7401 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7402 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7403 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7404 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7405 | // volatile version |
| 7406 | ParamTypes[0] = |
| 7407 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7408 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7409 | /*IsAssigmentOperator=*/isEqualOp); |
| 7410 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7411 | |
| 7412 | if (!(*Ptr).isRestrictQualified() && |
| 7413 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7414 | // restrict version |
| 7415 | ParamTypes[0] |
| 7416 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7417 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7418 | /*IsAssigmentOperator=*/isEqualOp); |
| 7419 | |
| 7420 | if (NeedVolatile) { |
| 7421 | // volatile restrict version |
| 7422 | ParamTypes[0] |
| 7423 | = S.Context.getLValueReferenceType( |
| 7424 | S.Context.getCVRQualifiedType(*Ptr, |
| 7425 | (Qualifiers::Volatile | |
| 7426 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7427 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7428 | /*IsAssigmentOperator=*/isEqualOp); |
| 7429 | } |
| 7430 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
| 7433 | if (isEqualOp) { |
| 7434 | for (BuiltinCandidateTypeSet::iterator |
| 7435 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7436 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7437 | Ptr != PtrEnd; ++Ptr) { |
| 7438 | // Make sure we don't add the same candidate twice. |
| 7439 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7440 | continue; |
| 7441 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7442 | QualType ParamTypes[2] = { |
| 7443 | S.Context.getLValueReferenceType(*Ptr), |
| 7444 | *Ptr, |
| 7445 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7446 | |
| 7447 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7448 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7449 | /*IsAssigmentOperator=*/true); |
| 7450 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7451 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7452 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7453 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7454 | // volatile version |
| 7455 | ParamTypes[0] = |
| 7456 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7457 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7458 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7459 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7460 | |
| 7461 | if (!(*Ptr).isRestrictQualified() && |
| 7462 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7463 | // restrict version |
| 7464 | ParamTypes[0] |
| 7465 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7466 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7467 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7468 | |
| 7469 | if (NeedVolatile) { |
| 7470 | // volatile restrict version |
| 7471 | ParamTypes[0] |
| 7472 | = S.Context.getLValueReferenceType( |
| 7473 | S.Context.getCVRQualifiedType(*Ptr, |
| 7474 | (Qualifiers::Volatile | |
| 7475 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7476 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7477 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7478 | } |
| 7479 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7480 | } |
| 7481 | } |
| 7482 | } |
| 7483 | |
| 7484 | // C++ [over.built]p18: |
| 7485 | // |
| 7486 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7487 | // VQ is either volatile or empty, and R is a promoted |
| 7488 | // arithmetic type, there exist candidate operator functions of |
| 7489 | // the form |
| 7490 | // |
| 7491 | // VQ L& operator=(VQ L&, R); |
| 7492 | // VQ L& operator*=(VQ L&, R); |
| 7493 | // VQ L& operator/=(VQ L&, R); |
| 7494 | // VQ L& operator+=(VQ L&, R); |
| 7495 | // VQ L& operator-=(VQ L&, R); |
| 7496 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7497 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7498 | return; |
| 7499 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7500 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7501 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7502 | Right < LastPromotedArithmeticType; ++Right) { |
| 7503 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7504 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7505 | |
| 7506 | // Add this built-in operator as a candidate (VQ is empty). |
| 7507 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7508 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7509 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7510 | /*IsAssigmentOperator=*/isEqualOp); |
| 7511 | |
| 7512 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7513 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7514 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7515 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7516 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7517 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7518 | /*IsAssigmentOperator=*/isEqualOp); |
| 7519 | } |
| 7520 | } |
| 7521 | } |
| 7522 | |
| 7523 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7524 | for (BuiltinCandidateTypeSet::iterator |
| 7525 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7526 | Vec1End = CandidateTypes[0].vector_end(); |
| 7527 | Vec1 != Vec1End; ++Vec1) { |
| 7528 | for (BuiltinCandidateTypeSet::iterator |
| 7529 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7530 | Vec2End = CandidateTypes[1].vector_end(); |
| 7531 | Vec2 != Vec2End; ++Vec2) { |
| 7532 | QualType ParamTypes[2]; |
| 7533 | ParamTypes[1] = *Vec2; |
| 7534 | // Add this built-in operator as a candidate (VQ is empty). |
| 7535 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7536 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7537 | /*IsAssigmentOperator=*/isEqualOp); |
| 7538 | |
| 7539 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7540 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7541 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7542 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7543 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7544 | /*IsAssigmentOperator=*/isEqualOp); |
| 7545 | } |
| 7546 | } |
| 7547 | } |
| 7548 | } |
| 7549 | |
| 7550 | // C++ [over.built]p22: |
| 7551 | // |
| 7552 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7553 | // is either volatile or empty, and R is a promoted integral |
| 7554 | // type, there exist candidate operator functions of the form |
| 7555 | // |
| 7556 | // VQ L& operator%=(VQ L&, R); |
| 7557 | // VQ L& operator<<=(VQ L&, R); |
| 7558 | // VQ L& operator>>=(VQ L&, R); |
| 7559 | // VQ L& operator&=(VQ L&, R); |
| 7560 | // VQ L& operator^=(VQ L&, R); |
| 7561 | // VQ L& operator|=(VQ L&, R); |
| 7562 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7563 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7564 | return; |
| 7565 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7566 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7567 | for (unsigned Right = FirstPromotedIntegralType; |
| 7568 | Right < LastPromotedIntegralType; ++Right) { |
| 7569 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7570 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7571 | |
| 7572 | // Add this built-in operator as a candidate (VQ is empty). |
| 7573 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7574 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7575 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7576 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7577 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7578 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7579 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7580 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7581 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7582 | } |
| 7583 | } |
| 7584 | } |
| 7585 | } |
| 7586 | |
| 7587 | // C++ [over.operator]p23: |
| 7588 | // |
| 7589 | // There also exist candidate operator functions of the form |
| 7590 | // |
| 7591 | // bool operator!(bool); |
| 7592 | // bool operator&&(bool, bool); |
| 7593 | // bool operator||(bool, bool); |
| 7594 | void addExclaimOverload() { |
| 7595 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7596 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7597 | /*IsAssignmentOperator=*/false, |
| 7598 | /*NumContextualBoolArguments=*/1); |
| 7599 | } |
| 7600 | void addAmpAmpOrPipePipeOverload() { |
| 7601 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7602 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7603 | /*IsAssignmentOperator=*/false, |
| 7604 | /*NumContextualBoolArguments=*/2); |
| 7605 | } |
| 7606 | |
| 7607 | // C++ [over.built]p13: |
| 7608 | // |
| 7609 | // For every cv-qualified or cv-unqualified object type T there |
| 7610 | // exist candidate operator functions of the form |
| 7611 | // |
| 7612 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7613 | // T& operator[](T*, ptrdiff_t); |
| 7614 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7615 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7616 | // T& operator[](ptrdiff_t, T*); |
| 7617 | void addSubscriptOverloads() { |
| 7618 | for (BuiltinCandidateTypeSet::iterator |
| 7619 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7620 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7621 | Ptr != PtrEnd; ++Ptr) { |
| 7622 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7623 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7624 | if (!PointeeType->isObjectType()) |
| 7625 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7626 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7627 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7628 | |
| 7629 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7630 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7631 | } |
| 7632 | |
| 7633 | for (BuiltinCandidateTypeSet::iterator |
| 7634 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7635 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7636 | Ptr != PtrEnd; ++Ptr) { |
| 7637 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7638 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7639 | if (!PointeeType->isObjectType()) |
| 7640 | continue; |
| 7641 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7642 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7643 | |
| 7644 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7645 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7646 | } |
| 7647 | } |
| 7648 | |
| 7649 | // C++ [over.built]p11: |
| 7650 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7651 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7652 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7653 | // there exist candidate operator functions of the form |
| 7654 | // |
| 7655 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7656 | // |
| 7657 | // where CV12 is the union of CV1 and CV2. |
| 7658 | void addArrowStarOverloads() { |
| 7659 | for (BuiltinCandidateTypeSet::iterator |
| 7660 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7661 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7662 | Ptr != PtrEnd; ++Ptr) { |
| 7663 | QualType C1Ty = (*Ptr); |
| 7664 | QualType C1; |
| 7665 | QualifierCollector Q1; |
| 7666 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7667 | if (!isa<RecordType>(C1)) |
| 7668 | continue; |
| 7669 | // heuristic to reduce number of builtin candidates in the set. |
| 7670 | // Add volatile/restrict version only if there are conversions to a |
| 7671 | // volatile/restrict type. |
| 7672 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7673 | continue; |
| 7674 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7675 | continue; |
| 7676 | for (BuiltinCandidateTypeSet::iterator |
| 7677 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7678 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7679 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7680 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7681 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7682 | C2 = C2.getUnqualifiedType(); |
| 7683 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7684 | break; |
| 7685 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7686 | // build CV12 T& |
| 7687 | QualType T = mptr->getPointeeType(); |
| 7688 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7689 | T.isVolatileQualified()) |
| 7690 | continue; |
| 7691 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7692 | T.isRestrictQualified()) |
| 7693 | continue; |
| 7694 | T = Q1.apply(S.Context, T); |
| 7695 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7696 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7697 | } |
| 7698 | } |
| 7699 | } |
| 7700 | |
| 7701 | // Note that we don't consider the first argument, since it has been |
| 7702 | // contextually converted to bool long ago. The candidates below are |
| 7703 | // therefore added as binary. |
| 7704 | // |
| 7705 | // C++ [over.built]p25: |
| 7706 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7707 | // enumeration type, there exist candidate operator functions of the form |
| 7708 | // |
| 7709 | // T operator?(bool, T, T); |
| 7710 | // |
| 7711 | void addConditionalOperatorOverloads() { |
| 7712 | /// Set of (canonical) types that we've already handled. |
| 7713 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7714 | |
| 7715 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7716 | for (BuiltinCandidateTypeSet::iterator |
| 7717 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7718 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7719 | Ptr != PtrEnd; ++Ptr) { |
| 7720 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7721 | continue; |
| 7722 | |
| 7723 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7724 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7725 | } |
| 7726 | |
| 7727 | for (BuiltinCandidateTypeSet::iterator |
| 7728 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7729 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7730 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7731 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7732 | continue; |
| 7733 | |
| 7734 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7735 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7738 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7739 | for (BuiltinCandidateTypeSet::iterator |
| 7740 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7741 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7742 | Enum != EnumEnd; ++Enum) { |
| 7743 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7744 | continue; |
| 7745 | |
| 7746 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7747 | continue; |
| 7748 | |
| 7749 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7750 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7751 | } |
| 7752 | } |
| 7753 | } |
| 7754 | } |
| 7755 | }; |
| 7756 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7757 | } // end anonymous namespace |
| 7758 | |
| 7759 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7760 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7761 | /// on the operator @p Op and the arguments given. For example, if the |
| 7762 | /// operator is a binary '+', this routine might add "int |
| 7763 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7764 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7765 | SourceLocation OpLoc, |
| 7766 | ArrayRef<Expr *> Args, |
| 7767 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7768 | // Find all of the types that the arguments can convert to, but only |
| 7769 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7770 | // that make use of these types. Also record whether we encounter non-record |
| 7771 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7772 | Qualifiers VisibleTypeConversionsQuals; |
| 7773 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7774 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7775 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7776 | |
| 7777 | bool HasNonRecordCandidateType = false; |
| 7778 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7779 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7780 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7781 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7782 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7783 | OpLoc, |
| 7784 | true, |
| 7785 | (Op == OO_Exclaim || |
| 7786 | Op == OO_AmpAmp || |
| 7787 | Op == OO_PipePipe), |
| 7788 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7789 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7790 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7791 | HasArithmeticOrEnumeralCandidateType = |
| 7792 | HasArithmeticOrEnumeralCandidateType || |
| 7793 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7794 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7795 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7796 | // Exit early when no non-record types have been added to the candidate set |
| 7797 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7798 | // |
| 7799 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7800 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7801 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7802 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7803 | return; |
| 7804 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7805 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7806 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7807 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7808 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7809 | CandidateTypes, CandidateSet); |
| 7810 | |
| 7811 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7812 | switch (Op) { |
| 7813 | case OO_None: |
| 7814 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7815 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7816 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7817 | case OO_New: |
| 7818 | case OO_Delete: |
| 7819 | case OO_Array_New: |
| 7820 | case OO_Array_Delete: |
| 7821 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7822 | llvm_unreachable( |
| 7823 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7824 | |
| 7825 | case OO_Comma: |
| 7826 | case OO_Arrow: |
| 7827 | // C++ [over.match.oper]p3: |
| 7828 | // -- For the operator ',', the unary operator '&', or the |
| 7829 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7830 | break; |
| 7831 | |
| 7832 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7833 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7834 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7835 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7836 | |
| 7837 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7838 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7839 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7840 | } else { |
| 7841 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7842 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7843 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7844 | break; |
| 7845 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7846 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7847 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7848 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7849 | else |
| 7850 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7851 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7852 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7853 | case OO_Slash: |
| 7854 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7855 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7856 | |
| 7857 | case OO_PlusPlus: |
| 7858 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7859 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7860 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7861 | break; |
| 7862 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7863 | case OO_EqualEqual: |
| 7864 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7865 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7866 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7867 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7868 | case OO_Less: |
| 7869 | case OO_Greater: |
| 7870 | case OO_LessEqual: |
| 7871 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7872 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7873 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7874 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7875 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7876 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7877 | case OO_Caret: |
| 7878 | case OO_Pipe: |
| 7879 | case OO_LessLess: |
| 7880 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7881 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7882 | break; |
| 7883 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7884 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7885 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7886 | // C++ [over.match.oper]p3: |
| 7887 | // -- For the operator ',', the unary operator '&', or the |
| 7888 | // operator '->', the built-in candidates set is empty. |
| 7889 | break; |
| 7890 | |
| 7891 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7892 | break; |
| 7893 | |
| 7894 | case OO_Tilde: |
| 7895 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7896 | break; |
| 7897 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7898 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7899 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7900 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7901 | |
| 7902 | case OO_PlusEqual: |
| 7903 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7904 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7905 | // Fall through. |
| 7906 | |
| 7907 | case OO_StarEqual: |
| 7908 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7909 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7910 | break; |
| 7911 | |
| 7912 | case OO_PercentEqual: |
| 7913 | case OO_LessLessEqual: |
| 7914 | case OO_GreaterGreaterEqual: |
| 7915 | case OO_AmpEqual: |
| 7916 | case OO_CaretEqual: |
| 7917 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7918 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7919 | break; |
| 7920 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7921 | case OO_Exclaim: |
| 7922 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7923 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7924 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7925 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7926 | case OO_PipePipe: |
| 7927 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7928 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7929 | |
| 7930 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7931 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7932 | break; |
| 7933 | |
| 7934 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7935 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7936 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7937 | |
| 7938 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7939 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7940 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7941 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7942 | } |
| 7943 | } |
| 7944 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7945 | /// \brief Add function candidates found via argument-dependent lookup |
| 7946 | /// to the set of overloading candidates. |
| 7947 | /// |
| 7948 | /// This routine performs argument-dependent name lookup based on the |
| 7949 | /// given function name (which may also be an operator name) and adds |
| 7950 | /// all of the overload candidates found by ADL to the overload |
| 7951 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7952 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7953 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7954 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7955 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7956 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7957 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7958 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7959 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7960 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7961 | // FIXME: This approach for uniquing ADL results (and removing |
| 7962 | // redundant candidates from the set) relies on pointer-equality, |
| 7963 | // which means we need to key off the canonical decl. However, |
| 7964 | // always going back to the canonical decl might not get us the |
| 7965 | // right set of default arguments. What default arguments are |
| 7966 | // we supposed to consider on ADL candidates, anyway? |
| 7967 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7968 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7969 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7970 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7971 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7972 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7973 | CandEnd = CandidateSet.end(); |
| 7974 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7975 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7976 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7977 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7978 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7979 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7980 | |
| 7981 | // For each of the ADL candidates we found, add it to the overload |
| 7982 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7983 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7984 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7985 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7986 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7987 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7988 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7989 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7990 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7991 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7992 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7993 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7994 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7995 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7996 | } |
| 7997 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7998 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7999 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8000 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8001 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8002 | const OverloadCandidate &Cand1, |
| 8003 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8004 | SourceLocation Loc, |
| 8005 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8006 | // Define viable functions to be better candidates than non-viable |
| 8007 | // functions. |
| 8008 | if (!Cand2.Viable) |
| 8009 | return Cand1.Viable; |
| 8010 | else if (!Cand1.Viable) |
| 8011 | return false; |
| 8012 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8013 | // C++ [over.match.best]p1: |
| 8014 | // |
| 8015 | // -- if F is a static member function, ICS1(F) is defined such |
| 8016 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 8017 | // any function G, and, symmetrically, ICS1(G) is neither |
| 8018 | // better nor worse than ICS1(F). |
| 8019 | unsigned StartArg = 0; |
| 8020 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 8021 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8022 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8023 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8024 | // A viable function F1 is defined to be a better function than another |
| 8025 | // 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] | 8026 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8027 | unsigned NumArgs = Cand1.NumConversions; |
| 8028 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8029 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8030 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8031 | switch (CompareImplicitConversionSequences(S, |
| 8032 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8033 | Cand2.Conversions[ArgIdx])) { |
| 8034 | case ImplicitConversionSequence::Better: |
| 8035 | // Cand1 has a better conversion sequence. |
| 8036 | HasBetterConversion = true; |
| 8037 | break; |
| 8038 | |
| 8039 | case ImplicitConversionSequence::Worse: |
| 8040 | // Cand1 can't be better than Cand2. |
| 8041 | return false; |
| 8042 | |
| 8043 | case ImplicitConversionSequence::Indistinguishable: |
| 8044 | // Do nothing. |
| 8045 | break; |
| 8046 | } |
| 8047 | } |
| 8048 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8049 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8050 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8051 | if (HasBetterConversion) |
| 8052 | return true; |
| 8053 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8054 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8055 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 8056 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8057 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 8058 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8059 | |
| 8060 | // -- F1 and F2 are function template specializations, and the function |
| 8061 | // template for F1 is more specialized than the template for F2 |
| 8062 | // 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] | 8063 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 8064 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8065 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8066 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8067 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 8068 | Cand2.Function->getPrimaryTemplate(), |
| 8069 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8070 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 8071 | : TPOC_Call, |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 8072 | Cand1.ExplicitCallArguments, |
| 8073 | Cand2.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8074 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8076 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8077 | // -- the context is an initialization by user-defined conversion |
| 8078 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 8079 | // from the return type of F1 to the destination type (i.e., |
| 8080 | // the type of the entity being initialized) is a better |
| 8081 | // conversion sequence than the standard conversion sequence |
| 8082 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8083 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8084 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8085 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 8086 | // First check whether we prefer one of the conversion functions over the |
| 8087 | // other. This only distinguishes the results in non-standard, extension |
| 8088 | // cases such as the conversion from a lambda closure type to a function |
| 8089 | // pointer or block. |
| 8090 | ImplicitConversionSequence::CompareKind FuncResult |
| 8091 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 8092 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 8093 | return FuncResult; |
| 8094 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8095 | switch (CompareStandardConversionSequences(S, |
| 8096 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8097 | Cand2.FinalConversion)) { |
| 8098 | case ImplicitConversionSequence::Better: |
| 8099 | // Cand1 has a better conversion sequence. |
| 8100 | return true; |
| 8101 | |
| 8102 | case ImplicitConversionSequence::Worse: |
| 8103 | // Cand1 can't be better than Cand2. |
| 8104 | return false; |
| 8105 | |
| 8106 | case ImplicitConversionSequence::Indistinguishable: |
| 8107 | // Do nothing |
| 8108 | break; |
| 8109 | } |
| 8110 | } |
| 8111 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8112 | return false; |
| 8113 | } |
| 8114 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8115 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8116 | /// within an overload candidate set. |
| 8117 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8118 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8119 | /// which overload resolution occurs. |
| 8120 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8121 | /// \param Best If overload resolution was successful or found a deleted |
| 8122 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8123 | /// |
| 8124 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8125 | OverloadingResult |
| 8126 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8127 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8128 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8129 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8130 | Best = end(); |
| 8131 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8132 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8133 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8134 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8135 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8139 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8140 | return OR_No_Viable_Function; |
| 8141 | |
| 8142 | // Make sure that this function is better than every other viable |
| 8143 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8144 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8145 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8146 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8147 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8148 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8149 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8150 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8151 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8153 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8154 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8155 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8156 | (Best->Function->isDeleted() || |
| 8157 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8158 | return OR_Deleted; |
| 8159 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8160 | return OR_Success; |
| 8161 | } |
| 8162 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8163 | namespace { |
| 8164 | |
| 8165 | enum OverloadCandidateKind { |
| 8166 | oc_function, |
| 8167 | oc_method, |
| 8168 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8169 | oc_function_template, |
| 8170 | oc_method_template, |
| 8171 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8172 | oc_implicit_default_constructor, |
| 8173 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8174 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8175 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8176 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8177 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8178 | }; |
| 8179 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8180 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8181 | FunctionDecl *Fn, |
| 8182 | std::string &Description) { |
| 8183 | bool isTemplate = false; |
| 8184 | |
| 8185 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8186 | isTemplate = true; |
| 8187 | Description = S.getTemplateArgumentBindingsText( |
| 8188 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8189 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8190 | |
| 8191 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8192 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8193 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8194 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8195 | if (Ctor->getInheritedConstructor()) |
| 8196 | return oc_implicit_inherited_constructor; |
| 8197 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8198 | if (Ctor->isDefaultConstructor()) |
| 8199 | return oc_implicit_default_constructor; |
| 8200 | |
| 8201 | if (Ctor->isMoveConstructor()) |
| 8202 | return oc_implicit_move_constructor; |
| 8203 | |
| 8204 | assert(Ctor->isCopyConstructor() && |
| 8205 | "unexpected sort of implicit constructor"); |
| 8206 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8207 | } |
| 8208 | |
| 8209 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8210 | // This actually gets spelled 'candidate function' for now, but |
| 8211 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8212 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8213 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8214 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8215 | if (Meth->isMoveAssignmentOperator()) |
| 8216 | return oc_implicit_move_assignment; |
| 8217 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8218 | if (Meth->isCopyAssignmentOperator()) |
| 8219 | return oc_implicit_copy_assignment; |
| 8220 | |
| 8221 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8222 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8223 | } |
| 8224 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8225 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8226 | } |
| 8227 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8228 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8229 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8230 | if (!Ctor) return; |
| 8231 | |
| 8232 | Ctor = Ctor->getInheritedConstructor(); |
| 8233 | if (!Ctor) return; |
| 8234 | |
| 8235 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8236 | } |
| 8237 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8238 | } // end anonymous namespace |
| 8239 | |
| 8240 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8241 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8242 | std::string FnDesc; |
| 8243 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8244 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8245 | << (unsigned) K << FnDesc; |
| 8246 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8247 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8248 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8249 | } |
| 8250 | |
Nick Lewycky | 199e370 | 2013-09-22 10:06:01 +0000 | [diff] [blame] | 8251 | // Notes the location of all overload candidates designated through |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8252 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8253 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8254 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8255 | |
| 8256 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8257 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8258 | |
| 8259 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8260 | IEnd = OvlExpr->decls_end(); |
| 8261 | I != IEnd; ++I) { |
| 8262 | if (FunctionTemplateDecl *FunTmpl = |
| 8263 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8264 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8265 | } else if (FunctionDecl *Fun |
| 8266 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8267 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8268 | } |
| 8269 | } |
| 8270 | } |
| 8271 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8272 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8273 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8274 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8275 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8276 | Sema &S, |
| 8277 | SourceLocation CaretLoc, |
| 8278 | const PartialDiagnostic &PDiag) const { |
| 8279 | S.Diag(CaretLoc, PDiag) |
| 8280 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8281 | // FIXME: The note limiting machinery is borrowed from |
| 8282 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8283 | // refactoring here. |
| 8284 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8285 | unsigned CandsShown = 0; |
| 8286 | AmbiguousConversionSequence::const_iterator I, E; |
| 8287 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8288 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8289 | break; |
| 8290 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8291 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8292 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8293 | if (I != E) |
| 8294 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8297 | namespace { |
| 8298 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8299 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8300 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8301 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8302 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8303 | FunctionDecl *Fn = Cand->Function; |
| 8304 | |
| 8305 | // There's a conversion slot for the object argument if this is a |
| 8306 | // non-constructor method. Note that 'I' corresponds the |
| 8307 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8308 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8309 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8310 | if (I == 0) |
| 8311 | isObjectArgument = true; |
| 8312 | else |
| 8313 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8314 | } |
| 8315 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8316 | std::string FnDesc; |
| 8317 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8318 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8319 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8320 | QualType FromTy = Conv.Bad.getFromType(); |
| 8321 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8322 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8323 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8324 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8325 | Expr *E = FromExpr->IgnoreParens(); |
| 8326 | if (isa<UnaryOperator>(E)) |
| 8327 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8328 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8329 | |
| 8330 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8331 | << (unsigned) FnKind << FnDesc |
| 8332 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8333 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8334 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8335 | return; |
| 8336 | } |
| 8337 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8338 | // Do some hand-waving analysis to see if the non-viability is due |
| 8339 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8340 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8341 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8342 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8343 | CToTy = RT->getPointeeType(); |
| 8344 | else { |
| 8345 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8346 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8347 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8348 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8349 | } |
| 8350 | |
| 8351 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8352 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8353 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8354 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8355 | |
| 8356 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8357 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8358 | << (unsigned) FnKind << FnDesc |
| 8359 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8360 | << FromTy |
| 8361 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8362 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8363 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8364 | return; |
| 8365 | } |
| 8366 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8367 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8368 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8369 | << (unsigned) FnKind << FnDesc |
| 8370 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8371 | << FromTy |
| 8372 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8373 | << (unsigned) isObjectArgument << I+1; |
| 8374 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8375 | return; |
| 8376 | } |
| 8377 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8378 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8379 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8380 | << (unsigned) FnKind << FnDesc |
| 8381 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8382 | << FromTy |
| 8383 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8384 | << (unsigned) isObjectArgument << I+1; |
| 8385 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8386 | return; |
| 8387 | } |
| 8388 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8389 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8390 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8391 | |
| 8392 | if (isObjectArgument) { |
| 8393 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8394 | << (unsigned) FnKind << FnDesc |
| 8395 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8396 | << FromTy << (CVR - 1); |
| 8397 | } else { |
| 8398 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8399 | << (unsigned) FnKind << FnDesc |
| 8400 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8401 | << FromTy << (CVR - 1) << I+1; |
| 8402 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8403 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8404 | return; |
| 8405 | } |
| 8406 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8407 | // Special diagnostic for failure to convert an initializer list, since |
| 8408 | // telling the user that it has type void is not useful. |
| 8409 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8410 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8411 | << (unsigned) FnKind << FnDesc |
| 8412 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8413 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8414 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8415 | return; |
| 8416 | } |
| 8417 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8418 | // Diagnose references or pointers to incomplete types differently, |
| 8419 | // since it's far from impossible that the incompleteness triggered |
| 8420 | // the failure. |
| 8421 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8422 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8423 | TempFromTy = PTy->getPointeeType(); |
| 8424 | if (TempFromTy->isIncompleteType()) { |
| 8425 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8426 | << (unsigned) FnKind << FnDesc |
| 8427 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8428 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8429 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8430 | return; |
| 8431 | } |
| 8432 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8433 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8434 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8435 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8436 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8437 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8438 | FromPtrTy->getPointeeType()) && |
| 8439 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8440 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8441 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8442 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8443 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8444 | } |
| 8445 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8446 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8447 | if (const ObjCObjectPointerType *ToPtrTy |
| 8448 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8449 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8450 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8451 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8452 | FromPtrTy->getPointeeType()) && |
| 8453 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8454 | BaseToDerivedConversion = 2; |
| 8455 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8456 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8457 | !FromTy->isIncompleteType() && |
| 8458 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8459 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8460 | BaseToDerivedConversion = 3; |
| 8461 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8462 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8463 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8464 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8465 | << (unsigned) FnKind << FnDesc |
| 8466 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8467 | << (unsigned) isObjectArgument << I + 1; |
| 8468 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8469 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8470 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8472 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8473 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8474 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8475 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8476 | << (unsigned) FnKind << FnDesc |
| 8477 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8478 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8479 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8480 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8481 | return; |
| 8482 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8483 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8484 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8485 | isa<PointerType>(CToTy)) { |
| 8486 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8487 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8488 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8489 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8490 | << (unsigned) FnKind << FnDesc |
| 8491 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8492 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8493 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8494 | return; |
| 8495 | } |
| 8496 | } |
| 8497 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8498 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8499 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8500 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8501 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8502 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8503 | << (unsigned) (Cand->Fix.Kind); |
| 8504 | |
| 8505 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8506 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8507 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8508 | FDiag << *HI; |
| 8509 | S.Diag(Fn->getLocation(), FDiag); |
| 8510 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8511 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8512 | } |
| 8513 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8514 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8515 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8516 | /// over a candidate in any candidate set. |
| 8517 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8518 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8519 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8520 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8521 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8522 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8523 | // 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] | 8524 | // right number of arguments, because only overloaded operators have |
| 8525 | // the weird behavior of overloading member and non-member functions. |
| 8526 | // Just don't report anything. |
| 8527 | if (Fn->isInvalidDecl() && |
| 8528 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8529 | return true; |
| 8530 | |
| 8531 | if (NumArgs < MinParams) { |
| 8532 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8533 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8534 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8535 | } else { |
| 8536 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8537 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8538 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8539 | } |
| 8540 | |
| 8541 | return false; |
| 8542 | } |
| 8543 | |
| 8544 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8545 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8546 | assert(isa<FunctionDecl>(D) && |
| 8547 | "The templated declaration should at least be a function" |
| 8548 | " when diagnosing bad template argument deduction due to too many" |
| 8549 | " or too few arguments"); |
| 8550 | |
| 8551 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8552 | |
| 8553 | // TODO: treat calls to a missing default constructor as a special case |
| 8554 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8555 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8556 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8557 | // at least / at most / exactly |
| 8558 | unsigned mode, modeCount; |
| 8559 | if (NumFormalArgs < MinParams) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8560 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8561 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8562 | mode = 0; // "at least" |
| 8563 | else |
| 8564 | mode = 2; // "exactly" |
| 8565 | modeCount = MinParams; |
| 8566 | } else { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8567 | if (MinParams != FnTy->getNumArgs()) |
| 8568 | mode = 1; // "at most" |
| 8569 | else |
| 8570 | mode = 2; // "exactly" |
| 8571 | modeCount = FnTy->getNumArgs(); |
| 8572 | } |
| 8573 | |
| 8574 | std::string Description; |
| 8575 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8576 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8577 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8578 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8579 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8580 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8581 | else |
| 8582 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8583 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8584 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8585 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8586 | } |
| 8587 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8588 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8589 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8590 | unsigned NumFormalArgs) { |
| 8591 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8592 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8593 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8594 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8595 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8596 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8597 | return FD->getDescribedFunctionTemplate(); |
| 8598 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8599 | return RD->getDescribedClassTemplate(); |
| 8600 | |
| 8601 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8602 | " for bad deduction diagnosis"); |
| 8603 | } |
| 8604 | |
| 8605 | /// Diagnose a failed template-argument deduction. |
| 8606 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8607 | DeductionFailureInfo &DeductionFailure, |
| 8608 | unsigned NumArgs) { |
| 8609 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8610 | NamedDecl *ParamD; |
| 8611 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8612 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8613 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8614 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8615 | case Sema::TDK_Success: |
| 8616 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8617 | |
| 8618 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8619 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8620 | S.Diag(Templated->getLocation(), |
| 8621 | diag::note_ovl_candidate_incomplete_deduction) |
| 8622 | << ParamD->getDeclName(); |
| 8623 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8624 | return; |
| 8625 | } |
| 8626 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8627 | case Sema::TDK_Underqualified: { |
| 8628 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8629 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8630 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8631 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8632 | |
| 8633 | // Param will have been canonicalized, but it should just be a |
| 8634 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8635 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8636 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8637 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8638 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8639 | |
| 8640 | // Arg has also been canonicalized, but there's nothing we can do |
| 8641 | // about that. It also doesn't matter as much, because it won't |
| 8642 | // have any template parameters in it (because deduction isn't |
| 8643 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8644 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8645 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8646 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8647 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8648 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8649 | return; |
| 8650 | } |
| 8651 | |
| 8652 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8653 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8654 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8655 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8656 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8657 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8658 | which = 1; |
| 8659 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8660 | which = 2; |
| 8661 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8662 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8663 | S.Diag(Templated->getLocation(), |
| 8664 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8665 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8666 | << *DeductionFailure.getSecondArg(); |
| 8667 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8668 | return; |
| 8669 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8670 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8671 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8672 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8673 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8674 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8675 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8676 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8677 | else { |
| 8678 | int index = 0; |
| 8679 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8680 | index = TTP->getIndex(); |
| 8681 | else if (NonTypeTemplateParmDecl *NTTP |
| 8682 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8683 | index = NTTP->getIndex(); |
| 8684 | else |
| 8685 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8686 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8687 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8688 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8689 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8690 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8691 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8692 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8693 | case Sema::TDK_TooManyArguments: |
| 8694 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8695 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8696 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8697 | |
| 8698 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8699 | S.Diag(Templated->getLocation(), |
| 8700 | diag::note_ovl_candidate_instantiation_depth); |
| 8701 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8702 | return; |
| 8703 | |
| 8704 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8705 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8706 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8707 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8708 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8709 | TemplateArgString = " "; |
| 8710 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8711 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8712 | } |
| 8713 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8714 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8715 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8716 | if (PDiag && PDiag->second.getDiagID() == |
| 8717 | diag::err_typename_nested_not_found_enable_if) { |
| 8718 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8719 | // name of the enable_if template. These are both present in PDiag. |
| 8720 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8721 | << "'enable_if'" << TemplateArgString; |
| 8722 | return; |
| 8723 | } |
| 8724 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8725 | // Format the SFINAE diagnostic into the argument string. |
| 8726 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8727 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8728 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8729 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8730 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8731 | SFINAEArgString = ": "; |
| 8732 | R = SourceRange(PDiag->first, PDiag->first); |
| 8733 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8734 | } |
| 8735 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8736 | S.Diag(Templated->getLocation(), |
| 8737 | diag::note_ovl_candidate_substitution_failure) |
| 8738 | << TemplateArgString << SFINAEArgString << R; |
| 8739 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8740 | return; |
| 8741 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8742 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8743 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8744 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8745 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8746 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8747 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8748 | return; |
| 8749 | } |
| 8750 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8751 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8752 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8753 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8754 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8755 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8756 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8757 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8758 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8759 | if (FirstTN.getKind() == TemplateName::Template && |
| 8760 | SecondTN.getKind() == TemplateName::Template) { |
| 8761 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8762 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8763 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8764 | // the same. This particular case is a bit difficult since: |
| 8765 | // 1) It is passed as a string to the diagnostic printer. |
| 8766 | // 2) The diagnostic printer only attempts to find a better |
| 8767 | // name for types, not decls. |
| 8768 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8769 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8770 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8771 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8772 | return; |
| 8773 | } |
| 8774 | } |
| 8775 | } |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 8776 | // FIXME: For generic lambda parameters, check if the function is a lambda |
| 8777 | // call operator, and if so, emit a prettier and more informative |
| 8778 | // diagnostic that mentions 'auto' and lambda in addition to |
| 8779 | // (or instead of?) the canonical template type parameters. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8780 | S.Diag(Templated->getLocation(), |
| 8781 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8782 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8783 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8784 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8785 | // TODO: diagnose these individually, then kill off |
| 8786 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8787 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8788 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8789 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8790 | return; |
| 8791 | } |
| 8792 | } |
| 8793 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8794 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8795 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8796 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8797 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8798 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8799 | return; |
| 8800 | } |
| 8801 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8802 | Cand->DeductionFailure, NumArgs); |
| 8803 | } |
| 8804 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8805 | /// CUDA: diagnose an invalid call across targets. |
| 8806 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8807 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8808 | FunctionDecl *Callee = Cand->Function; |
| 8809 | |
| 8810 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8811 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8812 | |
| 8813 | std::string FnDesc; |
| 8814 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8815 | |
| 8816 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8817 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8818 | } |
| 8819 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8820 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8821 | /// already generated a primary error at the call site. |
| 8822 | /// |
| 8823 | /// It really does need to be a single diagnostic with its caret |
| 8824 | /// pointed at the candidate declaration. Yes, this creates some |
| 8825 | /// major challenges of technical writing. Yes, this makes pointing |
| 8826 | /// out problems with specific arguments quite awkward. It's still |
| 8827 | /// better than generating twenty screens of text for every failed |
| 8828 | /// overload. |
| 8829 | /// |
| 8830 | /// It would be great to be able to express per-candidate problems |
| 8831 | /// more richly for those diagnostic clients that cared, but we'd |
| 8832 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8833 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8834 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8835 | FunctionDecl *Fn = Cand->Function; |
| 8836 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8837 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8838 | if (Cand->Viable && (Fn->isDeleted() || |
| 8839 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8840 | std::string FnDesc; |
| 8841 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8842 | |
| 8843 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8844 | << FnKind << FnDesc |
| 8845 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8846 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8847 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8848 | } |
| 8849 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8850 | // We don't really have anything else to say about viable candidates. |
| 8851 | if (Cand->Viable) { |
| 8852 | S.NoteOverloadCandidate(Fn); |
| 8853 | return; |
| 8854 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8855 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8856 | switch (Cand->FailureKind) { |
| 8857 | case ovl_fail_too_many_arguments: |
| 8858 | case ovl_fail_too_few_arguments: |
| 8859 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8860 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8861 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8862 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8863 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8864 | case ovl_fail_trivial_conversion: |
| 8865 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8866 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8867 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8868 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8869 | case ovl_fail_bad_conversion: { |
| 8870 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8871 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8872 | if (Cand->Conversions[I].isBad()) |
| 8873 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8874 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8875 | // FIXME: this currently happens when we're called from SemaInit |
| 8876 | // when user-conversion overload fails. Figure out how to handle |
| 8877 | // those conditions and diagnose them well. |
| 8878 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8879 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8880 | |
| 8881 | case ovl_fail_bad_target: |
| 8882 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8883 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8884 | } |
| 8885 | |
| 8886 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8887 | // Desugar the type of the surrogate down to a function type, |
| 8888 | // retaining as many typedefs as possible while still showing |
| 8889 | // the function type (and, therefore, its parameter types). |
| 8890 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8891 | bool isLValueReference = false; |
| 8892 | bool isRValueReference = false; |
| 8893 | bool isPointer = false; |
| 8894 | if (const LValueReferenceType *FnTypeRef = |
| 8895 | FnType->getAs<LValueReferenceType>()) { |
| 8896 | FnType = FnTypeRef->getPointeeType(); |
| 8897 | isLValueReference = true; |
| 8898 | } else if (const RValueReferenceType *FnTypeRef = |
| 8899 | FnType->getAs<RValueReferenceType>()) { |
| 8900 | FnType = FnTypeRef->getPointeeType(); |
| 8901 | isRValueReference = true; |
| 8902 | } |
| 8903 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8904 | FnType = FnTypePtr->getPointeeType(); |
| 8905 | isPointer = true; |
| 8906 | } |
| 8907 | // Desugar down to a function type. |
| 8908 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8909 | // Reconstruct the pointer/reference as appropriate. |
| 8910 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8911 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8912 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8913 | |
| 8914 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8915 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8916 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8917 | } |
| 8918 | |
| 8919 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8920 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8921 | SourceLocation OpLoc, |
| 8922 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8923 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8924 | std::string TypeStr("operator"); |
| 8925 | TypeStr += Opc; |
| 8926 | TypeStr += "("; |
| 8927 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8928 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8929 | TypeStr += ")"; |
| 8930 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8931 | } else { |
| 8932 | TypeStr += ", "; |
| 8933 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8934 | TypeStr += ")"; |
| 8935 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8936 | } |
| 8937 | } |
| 8938 | |
| 8939 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8940 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8941 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8942 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8943 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8944 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8945 | if (!ICS.isAmbiguous()) continue; |
| 8946 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8947 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8948 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8949 | } |
| 8950 | } |
| 8951 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8952 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8953 | if (Cand->Function) |
| 8954 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8955 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8956 | return Cand->Surrogate->getLocation(); |
| 8957 | return SourceLocation(); |
| 8958 | } |
| 8959 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8960 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8961 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8962 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8963 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8964 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8965 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8966 | case Sema::TDK_Incomplete: |
| 8967 | return 1; |
| 8968 | |
| 8969 | case Sema::TDK_Underqualified: |
| 8970 | case Sema::TDK_Inconsistent: |
| 8971 | return 2; |
| 8972 | |
| 8973 | case Sema::TDK_SubstitutionFailure: |
| 8974 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8975 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8976 | return 3; |
| 8977 | |
| 8978 | case Sema::TDK_InstantiationDepth: |
| 8979 | case Sema::TDK_FailedOverloadResolution: |
| 8980 | return 4; |
| 8981 | |
| 8982 | case Sema::TDK_InvalidExplicitArguments: |
| 8983 | return 5; |
| 8984 | |
| 8985 | case Sema::TDK_TooManyArguments: |
| 8986 | case Sema::TDK_TooFewArguments: |
| 8987 | return 6; |
| 8988 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8989 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8990 | } |
| 8991 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8992 | struct CompareOverloadCandidatesForDisplay { |
| 8993 | Sema &S; |
| 8994 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8995 | |
| 8996 | bool operator()(const OverloadCandidate *L, |
| 8997 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8998 | // Fast-path this check. |
| 8999 | if (L == R) return false; |
| 9000 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9001 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9002 | if (L->Viable) { |
| 9003 | if (!R->Viable) return true; |
| 9004 | |
| 9005 | // TODO: introduce a tri-valued comparison for overload |
| 9006 | // candidates. Would be more worthwhile if we had a sort |
| 9007 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9008 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 9009 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9010 | } else if (R->Viable) |
| 9011 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9012 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9013 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9014 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9015 | // Criteria by which we can sort non-viable candidates: |
| 9016 | if (!L->Viable) { |
| 9017 | // 1. Arity mismatches come after other candidates. |
| 9018 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 9019 | L->FailureKind == ovl_fail_too_few_arguments) |
| 9020 | return false; |
| 9021 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 9022 | R->FailureKind == ovl_fail_too_few_arguments) |
| 9023 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9024 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9025 | // 2. Bad conversions come first and are ordered by the number |
| 9026 | // of bad conversions and quality of good conversions. |
| 9027 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 9028 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 9029 | return true; |
| 9030 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9031 | // The conversion that can be fixed with a smaller number of changes, |
| 9032 | // comes first. |
| 9033 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 9034 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 9035 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 9036 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 9037 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9038 | if (numLFixes < numRFixes) |
| 9039 | return true; |
| 9040 | else |
| 9041 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 9042 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9043 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9044 | // If there's any ordering between the defined conversions... |
| 9045 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9046 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9047 | |
| 9048 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 9049 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9050 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9051 | switch (CompareImplicitConversionSequences(S, |
| 9052 | L->Conversions[I], |
| 9053 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9054 | case ImplicitConversionSequence::Better: |
| 9055 | leftBetter++; |
| 9056 | break; |
| 9057 | |
| 9058 | case ImplicitConversionSequence::Worse: |
| 9059 | leftBetter--; |
| 9060 | break; |
| 9061 | |
| 9062 | case ImplicitConversionSequence::Indistinguishable: |
| 9063 | break; |
| 9064 | } |
| 9065 | } |
| 9066 | if (leftBetter > 0) return true; |
| 9067 | if (leftBetter < 0) return false; |
| 9068 | |
| 9069 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 9070 | return false; |
| 9071 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9072 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 9073 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 9074 | return true; |
| 9075 | |
| 9076 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9077 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 9078 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 9079 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 9080 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 9081 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 9082 | // TODO: others? |
| 9083 | } |
| 9084 | |
| 9085 | // Sort everything else by location. |
| 9086 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9087 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9088 | |
| 9089 | // Put candidates without locations (e.g. builtins) at the end. |
| 9090 | if (LLoc.isInvalid()) return false; |
| 9091 | if (RLoc.isInvalid()) return true; |
| 9092 | |
| 9093 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9094 | } |
| 9095 | }; |
| 9096 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9097 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9098 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9099 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9100 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9101 | assert(!Cand->Viable); |
| 9102 | |
| 9103 | // Don't do anything on failures other than bad conversion. |
| 9104 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9105 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9106 | // We only want the FixIts if all the arguments can be corrected. |
| 9107 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9108 | // Use a implicit copy initialization to check conversion fixes. |
| 9109 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9110 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9111 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9112 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9113 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9114 | while (true) { |
| 9115 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9116 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9117 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9118 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9119 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9120 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9121 | } |
| 9122 | |
| 9123 | if (ConvIdx == ConvCount) |
| 9124 | return; |
| 9125 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9126 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9127 | "remaining conversion is initialized?"); |
| 9128 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9129 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9130 | // operation somehow. |
| 9131 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9132 | |
| 9133 | const FunctionProtoType* Proto; |
| 9134 | unsigned ArgIdx = ConvIdx; |
| 9135 | |
| 9136 | if (Cand->IsSurrogate) { |
| 9137 | QualType ConvType |
| 9138 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9139 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9140 | ConvType = ConvPtrType->getPointeeType(); |
| 9141 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9142 | ArgIdx--; |
| 9143 | } else if (Cand->Function) { |
| 9144 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9145 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9146 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9147 | ArgIdx--; |
| 9148 | } else { |
| 9149 | // Builtin binary operator with a bad first conversion. |
| 9150 | assert(ConvCount <= 3); |
| 9151 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9152 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9153 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9154 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9155 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9156 | /*InOverloadResolution*/ true, |
| 9157 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9158 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9159 | return; |
| 9160 | } |
| 9161 | |
| 9162 | // Fill in the rest of the conversions. |
| 9163 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9164 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9165 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9166 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9167 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9168 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9169 | /*InOverloadResolution=*/true, |
| 9170 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9171 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9172 | // Store the FixIt in the candidate if it exists. |
| 9173 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9174 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9175 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9176 | else |
| 9177 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9178 | } |
| 9179 | } |
| 9180 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9181 | } // end anonymous namespace |
| 9182 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9183 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9184 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9185 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9186 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9187 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9188 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9189 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9190 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9191 | // Sort the candidates by viability and position. Sorting directly would |
| 9192 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9193 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9194 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9195 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9196 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9197 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9198 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9199 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9200 | if (Cand->Function || Cand->IsSurrogate) |
| 9201 | Cands.push_back(Cand); |
| 9202 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9203 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9204 | } |
| 9205 | } |
| 9206 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9207 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9208 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9209 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9210 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9211 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9212 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9213 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9214 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9215 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9216 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9217 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9218 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9219 | // the user with. FIXME: This limit should depend on details of the |
| 9220 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9221 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9222 | break; |
| 9223 | } |
| 9224 | ++CandsShown; |
| 9225 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9226 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9227 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9228 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9229 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9230 | else { |
| 9231 | assert(Cand->Viable && |
| 9232 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9233 | // Generally we only see ambiguities including viable builtin |
| 9234 | // operators if overload resolution got screwed up by an |
| 9235 | // ambiguous user-defined conversion. |
| 9236 | // |
| 9237 | // FIXME: It's quite possible for different conversions to see |
| 9238 | // different ambiguities, though. |
| 9239 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9240 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9241 | ReportedAmbiguousConversions = true; |
| 9242 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9243 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9244 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9245 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9246 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9247 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9248 | |
| 9249 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9250 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9251 | } |
| 9252 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9253 | static SourceLocation |
| 9254 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9255 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9256 | : SourceLocation(); |
| 9257 | } |
| 9258 | |
| 9259 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9260 | Sema &S; |
| 9261 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9262 | |
| 9263 | bool operator()(const TemplateSpecCandidate *L, |
| 9264 | const TemplateSpecCandidate *R) { |
| 9265 | // Fast-path this check. |
| 9266 | if (L == R) |
| 9267 | return false; |
| 9268 | |
| 9269 | // Assuming that both candidates are not matches... |
| 9270 | |
| 9271 | // Sort by the ranking of deduction failures. |
| 9272 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9273 | return RankDeductionFailure(L->DeductionFailure) < |
| 9274 | RankDeductionFailure(R->DeductionFailure); |
| 9275 | |
| 9276 | // Sort everything else by location. |
| 9277 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9278 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9279 | |
| 9280 | // Put candidates without locations (e.g. builtins) at the end. |
| 9281 | if (LLoc.isInvalid()) |
| 9282 | return false; |
| 9283 | if (RLoc.isInvalid()) |
| 9284 | return true; |
| 9285 | |
| 9286 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9287 | } |
| 9288 | }; |
| 9289 | |
| 9290 | /// Diagnose a template argument deduction failure. |
| 9291 | /// We are treating these failures as overload failures due to bad |
| 9292 | /// deductions. |
| 9293 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9294 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9295 | DeductionFailure, /*NumArgs=*/0); |
| 9296 | } |
| 9297 | |
| 9298 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9299 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9300 | i->DeductionFailure.Destroy(); |
| 9301 | } |
| 9302 | } |
| 9303 | |
| 9304 | void TemplateSpecCandidateSet::clear() { |
| 9305 | destroyCandidates(); |
| 9306 | Candidates.clear(); |
| 9307 | } |
| 9308 | |
| 9309 | /// NoteCandidates - When no template specialization match is found, prints |
| 9310 | /// diagnostic messages containing the non-matching specializations that form |
| 9311 | /// the candidate set. |
| 9312 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9313 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9314 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9315 | // Sort the candidates by position (assuming no candidate is a match). |
| 9316 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9317 | // and sort those. |
| 9318 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9319 | Cands.reserve(size()); |
| 9320 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9321 | if (Cand->Specialization) |
| 9322 | Cands.push_back(Cand); |
| 9323 | // Otherwise, this is a non matching builtin candidate. We do not, |
| 9324 | // in general, want to list every possible builtin candidate. |
| 9325 | } |
| 9326 | |
| 9327 | std::sort(Cands.begin(), Cands.end(), |
| 9328 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9329 | |
| 9330 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9331 | // for generalization purposes (?). |
| 9332 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9333 | |
| 9334 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9335 | unsigned CandsShown = 0; |
| 9336 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9337 | TemplateSpecCandidate *Cand = *I; |
| 9338 | |
| 9339 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9340 | // the user with. FIXME: This limit should depend on details of the |
| 9341 | // candidate list. |
| 9342 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9343 | break; |
| 9344 | ++CandsShown; |
| 9345 | |
| 9346 | assert(Cand->Specialization && |
| 9347 | "Non-matching built-in candidates are not added to Cands."); |
| 9348 | Cand->NoteDeductionFailure(S); |
| 9349 | } |
| 9350 | |
| 9351 | if (I != E) |
| 9352 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9353 | } |
| 9354 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9355 | // [PossiblyAFunctionType] --> [Return] |
| 9356 | // NonFunctionType --> NonFunctionType |
| 9357 | // R (A) --> R(A) |
| 9358 | // R (*)(A) --> R (A) |
| 9359 | // R (&)(A) --> R (A) |
| 9360 | // R (S::*)(A) --> R (A) |
| 9361 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9362 | QualType Ret = PossiblyAFunctionType; |
| 9363 | if (const PointerType *ToTypePtr = |
| 9364 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9365 | Ret = ToTypePtr->getPointeeType(); |
| 9366 | else if (const ReferenceType *ToTypeRef = |
| 9367 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9368 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9369 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9370 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9371 | Ret = MemTypePtr->getPointeeType(); |
| 9372 | Ret = |
| 9373 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9374 | return Ret; |
| 9375 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9376 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9377 | // A helper class to help with address of function resolution |
| 9378 | // - allows us to avoid passing around all those ugly parameters |
| 9379 | class AddressOfFunctionResolver |
| 9380 | { |
| 9381 | Sema& S; |
| 9382 | Expr* SourceExpr; |
| 9383 | const QualType& TargetType; |
| 9384 | QualType TargetFunctionType; // Extracted function type from target type |
| 9385 | |
| 9386 | bool Complain; |
| 9387 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9388 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9389 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9390 | bool TargetTypeIsNonStaticMemberFunction; |
| 9391 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9392 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9393 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9394 | OverloadExpr::FindResult OvlExprInfo; |
| 9395 | OverloadExpr *OvlExpr; |
| 9396 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9397 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9398 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9399 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9400 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9401 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9402 | const QualType &TargetType, bool Complain) |
| 9403 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9404 | Complain(Complain), Context(S.getASTContext()), |
| 9405 | TargetTypeIsNonStaticMemberFunction( |
| 9406 | !!TargetType->getAs<MemberPointerType>()), |
| 9407 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9408 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9409 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9410 | OvlExpr(OvlExprInfo.Expression), |
| 9411 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9412 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9413 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9414 | if (TargetFunctionType->isFunctionType()) { |
| 9415 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9416 | if (!UME->isImplicitAccess() && |
| 9417 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9418 | StaticMemberFunctionFromBoundPointer = true; |
| 9419 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9420 | DeclAccessPair dap; |
| 9421 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9422 | OvlExpr, false, &dap)) { |
| 9423 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9424 | if (!Method->isStatic()) { |
| 9425 | // If the target type is a non-function type and the function found |
| 9426 | // is a non-static member function, pretend as if that was the |
| 9427 | // target, it's the only possible type to end up with. |
| 9428 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9429 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9430 | // And skip adding the function if its not in the proper form. |
| 9431 | // We'll diagnose this due to an empty set of functions. |
| 9432 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9433 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9436 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9437 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9438 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9439 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9440 | |
| 9441 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9442 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9443 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9444 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9445 | // C++ [over.over]p4: |
| 9446 | // If more than one function is selected, [...] |
| 9447 | if (Matches.size() > 1) { |
| 9448 | if (FoundNonTemplateFunction) |
| 9449 | EliminateAllTemplateMatches(); |
| 9450 | else |
| 9451 | EliminateAllExceptMostSpecializedTemplate(); |
| 9452 | } |
| 9453 | } |
| 9454 | } |
| 9455 | |
| 9456 | private: |
| 9457 | bool isTargetTypeAFunction() const { |
| 9458 | return TargetFunctionType->isFunctionType(); |
| 9459 | } |
| 9460 | |
| 9461 | // [ToType] [Return] |
| 9462 | |
| 9463 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9464 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9465 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9466 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9467 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9468 | } |
| 9469 | |
| 9470 | // return true if any matching specializations were found |
| 9471 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9472 | const DeclAccessPair& CurAccessFunPair) { |
| 9473 | if (CXXMethodDecl *Method |
| 9474 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9475 | // Skip non-static function templates when converting to pointer, and |
| 9476 | // static when converting to member pointer. |
| 9477 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9478 | return false; |
| 9479 | } |
| 9480 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9481 | return false; |
| 9482 | |
| 9483 | // C++ [over.over]p2: |
| 9484 | // If the name is a function template, template argument deduction is |
| 9485 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9486 | // resulting template argument list is used to generate a single |
| 9487 | // function template specialization, which is added to the set of |
| 9488 | // overloaded functions considered. |
| 9489 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9490 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9491 | if (Sema::TemplateDeductionResult Result |
| 9492 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9493 | &OvlExplicitTemplateArgs, |
| 9494 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9495 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9496 | // Make a note of the failed deduction for diagnostics. |
| 9497 | FailedCandidates.addCandidate() |
| 9498 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9499 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9500 | return false; |
| 9501 | } |
| 9502 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9503 | // Template argument deduction ensures that we have an exact match or |
| 9504 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9505 | // This function template specicalization works. |
| 9506 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9507 | assert(S.isSameOrCompatibleFunctionType( |
| 9508 | Context.getCanonicalType(Specialization->getType()), |
| 9509 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9510 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9511 | return true; |
| 9512 | } |
| 9513 | |
| 9514 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9515 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9516 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9517 | // Skip non-static functions when converting to pointer, and static |
| 9518 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9519 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9520 | return false; |
| 9521 | } |
| 9522 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9523 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9524 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9525 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9526 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9527 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9528 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9529 | return false; |
| 9530 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9531 | // If any candidate has a placeholder return type, trigger its deduction |
| 9532 | // now. |
| 9533 | if (S.getLangOpts().CPlusPlus1y && |
| 9534 | FunDecl->getResultType()->isUndeducedType() && |
| 9535 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9536 | return false; |
| 9537 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9538 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9539 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9540 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9541 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9542 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9543 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9544 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9545 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9546 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9548 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9549 | |
| 9550 | return false; |
| 9551 | } |
| 9552 | |
| 9553 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9554 | bool Ret = false; |
| 9555 | |
| 9556 | // If the overload expression doesn't have the form of a pointer to |
| 9557 | // member, don't try to convert it to a pointer-to-member type. |
| 9558 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9559 | return false; |
| 9560 | |
| 9561 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9562 | E = OvlExpr->decls_end(); |
| 9563 | I != E; ++I) { |
| 9564 | // Look through any using declarations to find the underlying function. |
| 9565 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9566 | |
| 9567 | // C++ [over.over]p3: |
| 9568 | // Non-member functions and static member functions match |
| 9569 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9570 | // Nonstatic member functions match targets of |
| 9571 | // type "pointer-to-member-function." |
| 9572 | // Note that according to DR 247, the containing class does not matter. |
| 9573 | if (FunctionTemplateDecl *FunctionTemplate |
| 9574 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9575 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9576 | Ret = true; |
| 9577 | } |
| 9578 | // If we have explicit template arguments supplied, skip non-templates. |
| 9579 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9580 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9581 | Ret = true; |
| 9582 | } |
| 9583 | assert(Ret || Matches.empty()); |
| 9584 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9585 | } |
| 9586 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9587 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9588 | // [...] and any given function template specialization F1 is |
| 9589 | // eliminated if the set contains a second function template |
| 9590 | // specialization whose function template is more specialized |
| 9591 | // than the function template of F1 according to the partial |
| 9592 | // ordering rules of 14.5.5.2. |
| 9593 | |
| 9594 | // The algorithm specified above is quadratic. We instead use a |
| 9595 | // two-pass algorithm (similar to the one used to identify the |
| 9596 | // best viable function in an overload set) that identifies the |
| 9597 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9598 | |
| 9599 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9600 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9601 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9602 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9603 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9604 | // here, since the no_viable diagnostic has index 0. |
| 9605 | UnresolvedSetIterator Result = S.getMostSpecialized( |
Richard Smith | 4ad09e6 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 9606 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9607 | SourceExpr->getLocStart(), S.PDiag(), |
| 9608 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9609 | .second->getDeclName(), |
| 9610 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9611 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9612 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9613 | if (Result != MatchesCopy.end()) { |
| 9614 | // Make it the first and only element |
| 9615 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9616 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9617 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9618 | } |
| 9619 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9620 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9621 | void EliminateAllTemplateMatches() { |
| 9622 | // [...] any function template specializations in the set are |
| 9623 | // eliminated if the set also contains a non-template function, [...] |
| 9624 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9625 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9626 | ++I; |
| 9627 | else { |
| 9628 | Matches[I] = Matches[--N]; |
| 9629 | Matches.set_size(N); |
| 9630 | } |
| 9631 | } |
| 9632 | } |
| 9633 | |
| 9634 | public: |
| 9635 | void ComplainNoMatchesFound() const { |
| 9636 | assert(Matches.empty()); |
| 9637 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9638 | << OvlExpr->getName() << TargetFunctionType |
| 9639 | << OvlExpr->getSourceRange(); |
Richard Smith | 72a36a1 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 9640 | if (FailedCandidates.empty()) |
| 9641 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 9642 | else { |
| 9643 | // We have some deduction failure messages. Use them to diagnose |
| 9644 | // the function templates, and diagnose the non-template candidates |
| 9645 | // normally. |
| 9646 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9647 | IEnd = OvlExpr->decls_end(); |
| 9648 | I != IEnd; ++I) |
| 9649 | if (FunctionDecl *Fun = |
| 9650 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 9651 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 9652 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 9653 | } |
| 9654 | } |
| 9655 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9656 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9657 | return TargetTypeIsNonStaticMemberFunction && |
| 9658 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9659 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9660 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9661 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9662 | // TODO: Should we condition this on whether any functions might |
| 9663 | // have matched, or is it more appropriate to do that in callers? |
| 9664 | // TODO: a fixit wouldn't hurt. |
| 9665 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9666 | << TargetType << OvlExpr->getSourceRange(); |
| 9667 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9668 | |
| 9669 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9670 | return StaticMemberFunctionFromBoundPointer; |
| 9671 | } |
| 9672 | |
| 9673 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9674 | S.Diag(OvlExpr->getLocStart(), |
| 9675 | diag::err_invalid_form_pointer_member_function) |
| 9676 | << OvlExpr->getSourceRange(); |
| 9677 | } |
| 9678 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9679 | void ComplainOfInvalidConversion() const { |
| 9680 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9681 | << OvlExpr->getName() << TargetType; |
| 9682 | } |
| 9683 | |
| 9684 | void ComplainMultipleMatchesFound() const { |
| 9685 | assert(Matches.size() > 1); |
| 9686 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9687 | << OvlExpr->getName() |
| 9688 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9689 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9690 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9691 | |
| 9692 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9693 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9694 | int getNumMatches() const { return Matches.size(); } |
| 9695 | |
| 9696 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9697 | if (Matches.size() != 1) return 0; |
| 9698 | return Matches[0].second; |
| 9699 | } |
| 9700 | |
| 9701 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9702 | if (Matches.size() != 1) return 0; |
| 9703 | return &Matches[0].first; |
| 9704 | } |
| 9705 | }; |
| 9706 | |
| 9707 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9708 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9709 | /// expression with overloaded function type and @p ToType is the type |
| 9710 | /// we're trying to resolve to. For example: |
| 9711 | /// |
| 9712 | /// @code |
| 9713 | /// int f(double); |
| 9714 | /// int f(int); |
| 9715 | /// |
| 9716 | /// int (*pfd)(double) = f; // selects f(double) |
| 9717 | /// @endcode |
| 9718 | /// |
| 9719 | /// This routine returns the resulting FunctionDecl if it could be |
| 9720 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9721 | /// routine will emit diagnostics if there is an error. |
| 9722 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9723 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9724 | QualType TargetType, |
| 9725 | bool Complain, |
| 9726 | DeclAccessPair &FoundResult, |
| 9727 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9728 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9729 | |
| 9730 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9731 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9732 | int NumMatches = Resolver.getNumMatches(); |
| 9733 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9734 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9735 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9736 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9737 | else |
| 9738 | Resolver.ComplainNoMatchesFound(); |
| 9739 | } |
| 9740 | else if (NumMatches > 1 && Complain) |
| 9741 | Resolver.ComplainMultipleMatchesFound(); |
| 9742 | else if (NumMatches == 1) { |
| 9743 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9744 | assert(Fn); |
| 9745 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9746 | if (Complain) { |
| 9747 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9748 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9749 | else |
| 9750 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9751 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9752 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9753 | |
| 9754 | if (pHadMultipleCandidates) |
| 9755 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9756 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9757 | } |
| 9758 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9759 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9760 | /// resolve that overloaded function expression down to a single function. |
| 9761 | /// |
| 9762 | /// This routine can only resolve template-ids that refer to a single function |
| 9763 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9764 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9765 | /// as described in C++0x [temp.arg.explicit]p3. |
Alp Toker | 8b40772 | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 9766 | /// |
| 9767 | /// If no template-ids are found, no diagnostics are emitted and NULL is |
| 9768 | /// returned. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9769 | FunctionDecl * |
| 9770 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9771 | bool Complain, |
| 9772 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9773 | // C++ [over.over]p1: |
| 9774 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9775 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9776 | // C++ [over.over]p1: |
| 9777 | // [...] The overloaded function name can be preceded by the & |
| 9778 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9779 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9780 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9781 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9782 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9783 | |
| 9784 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9785 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9786 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9787 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9788 | // Look through all of the overloaded functions, searching for one |
| 9789 | // whose type matches exactly. |
| 9790 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9791 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9792 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9793 | // C++0x [temp.arg.explicit]p3: |
| 9794 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9795 | // where deduction is not done, if a template argument list is |
| 9796 | // specified and it, along with any default template arguments, |
| 9797 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9798 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9799 | FunctionTemplateDecl *FunctionTemplate |
| 9800 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9801 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9802 | // C++ [over.over]p2: |
| 9803 | // If the name is a function template, template argument deduction is |
| 9804 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9805 | // resulting template argument list is used to generate a single |
| 9806 | // function template specialization, which is added to the set of |
| 9807 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9808 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9809 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9810 | if (TemplateDeductionResult Result |
| 9811 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9812 | Specialization, Info, |
| 9813 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9814 | // Make a note of the failed deduction for diagnostics. |
| 9815 | // TODO: Actually use the failed-deduction info? |
| 9816 | FailedCandidates.addCandidate() |
| 9817 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9818 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9819 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9820 | } |
| 9821 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9822 | assert(Specialization && "no specialization and no error?"); |
| 9823 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9824 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9825 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9826 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9827 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9828 | << ovl->getName(); |
| 9829 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9830 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9831 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9832 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9833 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9834 | Matched = Specialization; |
| 9835 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9836 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9837 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9838 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9839 | Matched->getResultType()->isUndeducedType() && |
| 9840 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9841 | return 0; |
| 9842 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9843 | return Matched; |
| 9844 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9845 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9846 | |
| 9847 | |
| 9848 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9849 | // Resolve and fix an overloaded expression that can be resolved |
| 9850 | // because it identifies a single function template specialization. |
| 9851 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9852 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9853 | // |
| 9854 | // Return true if it was logically possible to so resolve the |
| 9855 | // expression, regardless of whether or not it succeeded. Always |
| 9856 | // returns true if 'complain' is set. |
| 9857 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9858 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9859 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9860 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9861 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9862 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9863 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9864 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9865 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9866 | DeclAccessPair found; |
| 9867 | ExprResult SingleFunctionExpression; |
| 9868 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9869 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9870 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9871 | SrcExpr = ExprError(); |
| 9872 | return true; |
| 9873 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9874 | |
| 9875 | // It is only correct to resolve to an instance method if we're |
| 9876 | // resolving a form that's permitted to be a pointer to member. |
| 9877 | // Otherwise we'll end up making a bound member expression, which |
| 9878 | // is illegal in all the contexts we resolve like this. |
| 9879 | if (!ovl.HasFormOfMemberPointer && |
| 9880 | isa<CXXMethodDecl>(fn) && |
| 9881 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9882 | if (!complain) return false; |
| 9883 | |
| 9884 | Diag(ovl.Expression->getExprLoc(), |
| 9885 | diag::err_bound_member_function) |
| 9886 | << 0 << ovl.Expression->getSourceRange(); |
| 9887 | |
| 9888 | // TODO: I believe we only end up here if there's a mix of |
| 9889 | // static and non-static candidates (otherwise the expression |
| 9890 | // would have 'bound member' type, not 'overload' type). |
| 9891 | // Ideally we would note which candidate was chosen and why |
| 9892 | // the static candidates were rejected. |
| 9893 | SrcExpr = ExprError(); |
| 9894 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9895 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9896 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9897 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9898 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9899 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9900 | |
| 9901 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9902 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9903 | SingleFunctionExpression = |
| 9904 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9905 | if (SingleFunctionExpression.isInvalid()) { |
| 9906 | SrcExpr = ExprError(); |
| 9907 | return true; |
| 9908 | } |
| 9909 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9910 | } |
| 9911 | |
| 9912 | if (!SingleFunctionExpression.isUsable()) { |
| 9913 | if (complain) { |
| 9914 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9915 | << ovl.Expression->getName() |
| 9916 | << DestTypeForComplaining |
| 9917 | << OpRangeForComplaining |
| 9918 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9919 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9920 | |
| 9921 | SrcExpr = ExprError(); |
| 9922 | return true; |
| 9923 | } |
| 9924 | |
| 9925 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9926 | } |
| 9927 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9928 | SrcExpr = SingleFunctionExpression; |
| 9929 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9930 | } |
| 9931 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9932 | /// \brief Add a single candidate to the overload set. |
| 9933 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9934 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9935 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9936 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9937 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9938 | bool PartialOverloading, |
| 9939 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9940 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9941 | if (isa<UsingShadowDecl>(Callee)) |
| 9942 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9943 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9944 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9945 | if (ExplicitTemplateArgs) { |
| 9946 | assert(!KnownValid && "Explicit template arguments?"); |
| 9947 | return; |
| 9948 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9949 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9950 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9951 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9952 | } |
| 9953 | |
| 9954 | if (FunctionTemplateDecl *FuncTemplate |
| 9955 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9956 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9957 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9958 | return; |
| 9959 | } |
| 9960 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9961 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9962 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9963 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9964 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9965 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9966 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9967 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9968 | OverloadCandidateSet &CandidateSet, |
| 9969 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9970 | |
| 9971 | #ifndef NDEBUG |
| 9972 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9973 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9974 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9975 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9976 | // and let Y be the lookup set produced by argument dependent |
| 9977 | // lookup (defined as follows). If X contains |
| 9978 | // |
| 9979 | // -- a declaration of a class member, or |
| 9980 | // |
| 9981 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9982 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9983 | // |
| 9984 | // -- a declaration that is neither a function or a function |
| 9985 | // template |
| 9986 | // |
| 9987 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9988 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9989 | if (ULE->requiresADL()) { |
| 9990 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9991 | E = ULE->decls_end(); I != E; ++I) { |
| 9992 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9993 | assert(isa<UsingShadowDecl>(*I) || |
| 9994 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9995 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9996 | } |
| 9997 | } |
| 9998 | #endif |
| 9999 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10000 | // It would be nice to avoid this copy. |
| 10001 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 10002 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10003 | if (ULE->hasExplicitTemplateArgs()) { |
| 10004 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10005 | ExplicitTemplateArgs = &TABuffer; |
| 10006 | } |
| 10007 | |
| 10008 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 10009 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10010 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 10011 | CandidateSet, PartialOverloading, |
| 10012 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10013 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10014 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10015 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 10016 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10017 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10018 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 10019 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10020 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10021 | /// Determine whether a declaration with the specified name could be moved into |
| 10022 | /// a different namespace. |
| 10023 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 10024 | switch (Name.getCXXOverloadedOperator()) { |
| 10025 | case OO_New: case OO_Array_New: |
| 10026 | case OO_Delete: case OO_Array_Delete: |
| 10027 | return false; |
| 10028 | |
| 10029 | default: |
| 10030 | return true; |
| 10031 | } |
| 10032 | } |
| 10033 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10034 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 10035 | /// template, where the non-dependent name was declared after the template |
| 10036 | /// was defined. This is common in code written for a compilers which do not |
| 10037 | /// correctly implement two-stage name lookup. |
| 10038 | /// |
| 10039 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10040 | static bool |
| 10041 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 10042 | const CXXScopeSpec &SS, LookupResult &R, |
| 10043 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10044 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10045 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 10046 | return false; |
| 10047 | |
| 10048 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 10049 | if (DC->isTransparentContext()) |
| 10050 | continue; |
| 10051 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10052 | SemaRef.LookupQualifiedName(R, DC); |
| 10053 | |
| 10054 | if (!R.empty()) { |
| 10055 | R.suppressDiagnostics(); |
| 10056 | |
| 10057 | if (isa<CXXRecordDecl>(DC)) { |
| 10058 | // Don't diagnose names we find in classes; we get much better |
| 10059 | // diagnostics for these from DiagnoseEmptyLookup. |
| 10060 | R.clear(); |
| 10061 | return false; |
| 10062 | } |
| 10063 | |
| 10064 | OverloadCandidateSet Candidates(FnLoc); |
| 10065 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 10066 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10067 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10068 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10069 | |
| 10070 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10071 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10072 | // No viable functions. Don't bother the user with notes for functions |
| 10073 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10074 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10075 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 10076 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10077 | |
| 10078 | // Find the namespaces where ADL would have looked, and suggest |
| 10079 | // declaring the function there instead. |
| 10080 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 10081 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 10082 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10083 | AssociatedNamespaces, |
| 10084 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10085 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10086 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 10087 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 10088 | for (Sema::AssociatedNamespaceSet::iterator |
| 10089 | it = AssociatedNamespaces.begin(), |
| 10090 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 10091 | // Never suggest declaring a function within namespace 'std'. |
| 10092 | if (Std && Std->Encloses(*it)) |
| 10093 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 10094 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 10095 | // Never suggest declaring a function within a namespace with a |
| 10096 | // reserved name, like __gnu_cxx. |
| 10097 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 10098 | if (NS && |
| 10099 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 10100 | continue; |
| 10101 | |
| 10102 | SuggestedNamespaces.insert(*it); |
| 10103 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10104 | } |
| 10105 | |
| 10106 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 10107 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10108 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10109 | SemaRef.Diag(Best->Function->getLocation(), |
| 10110 | diag::note_not_found_by_two_phase_lookup) |
| 10111 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10112 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10113 | SemaRef.Diag(Best->Function->getLocation(), |
| 10114 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10115 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10116 | } else { |
| 10117 | // FIXME: It would be useful to list the associated namespaces here, |
| 10118 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10119 | // a localized representation of a list of items. |
| 10120 | SemaRef.Diag(Best->Function->getLocation(), |
| 10121 | diag::note_not_found_by_two_phase_lookup) |
| 10122 | << R.getLookupName() << 2; |
| 10123 | } |
| 10124 | |
| 10125 | // Try to recover by calling this function. |
| 10126 | return true; |
| 10127 | } |
| 10128 | |
| 10129 | R.clear(); |
| 10130 | } |
| 10131 | |
| 10132 | return false; |
| 10133 | } |
| 10134 | |
| 10135 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10136 | /// template, where the non-dependent operator was declared after the template |
| 10137 | /// was defined. |
| 10138 | /// |
| 10139 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10140 | static bool |
| 10141 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10142 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10143 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10144 | DeclarationName OpName = |
| 10145 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10146 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10147 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10148 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10149 | } |
| 10150 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10151 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10152 | class BuildRecoveryCallExprRAII { |
| 10153 | Sema &SemaRef; |
| 10154 | public: |
| 10155 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10156 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10157 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10158 | } |
| 10159 | |
| 10160 | ~BuildRecoveryCallExprRAII() { |
| 10161 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10162 | } |
| 10163 | }; |
| 10164 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10165 | } |
| 10166 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10167 | /// Attempts to recover from a call where no functions were found. |
| 10168 | /// |
| 10169 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10170 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10171 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10172 | UnresolvedLookupExpr *ULE, |
| 10173 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10174 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10175 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10176 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10177 | // Do not try to recover if it is already building a recovery call. |
| 10178 | // This stops infinite loops for template instantiations like |
| 10179 | // |
| 10180 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10181 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10182 | // |
| 10183 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10184 | return ExprError(); |
| 10185 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10186 | |
| 10187 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10188 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10189 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10190 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10191 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10192 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10193 | if (ULE->hasExplicitTemplateArgs()) { |
| 10194 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10195 | ExplicitTemplateArgs = &TABuffer; |
| 10196 | } |
| 10197 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10198 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10199 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 761695f | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10200 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
| 10201 | ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10202 | NoTypoCorrectionCCC RejectAll; |
| 10203 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10204 | (CorrectionCandidateCallback*)&Validator : |
| 10205 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10206 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10207 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10208 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10209 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10210 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10211 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10212 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10213 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10214 | |
| 10215 | // Build an implicit member call if appropriate. Just drop the |
| 10216 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10217 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10218 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10219 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10220 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10221 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10222 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10223 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10224 | else |
| 10225 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10226 | |
| 10227 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10228 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10229 | |
| 10230 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10231 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10232 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10233 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10234 | MultiExprArg(Args.data(), Args.size()), |
| 10235 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10236 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10237 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10238 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10239 | /// the given function. |
| 10240 | /// \returns true when an the ExprResult output parameter has been set. |
| 10241 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10242 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10243 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10244 | SourceLocation RParenLoc, |
| 10245 | OverloadCandidateSet *CandidateSet, |
| 10246 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10247 | #ifndef NDEBUG |
| 10248 | if (ULE->requiresADL()) { |
| 10249 | // To do ADL, we must have found an unqualified name. |
| 10250 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10251 | |
| 10252 | // We don't perform ADL for implicit declarations of builtins. |
| 10253 | // Verify that this was correctly set up. |
| 10254 | FunctionDecl *F; |
| 10255 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10256 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10257 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10258 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10259 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10260 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10261 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10262 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10263 | #endif |
| 10264 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10265 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10266 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10267 | *Result = ExprError(); |
| 10268 | return true; |
| 10269 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10270 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10271 | // Add the functions denoted by the callee to the set of candidate |
| 10272 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10273 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10274 | |
| 10275 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10276 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10277 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10278 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10279 | // In Microsoft mode, if we are inside a template class member function then |
| 10280 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10281 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10282 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10283 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10284 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10285 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10286 | Context.DependentTy, VK_RValue, |
| 10287 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10288 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10289 | *Result = Owned(CE); |
| 10290 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10291 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10292 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10293 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10294 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10295 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10296 | return false; |
| 10297 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10298 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10299 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10300 | /// the completed call expression. If overload resolution fails, emits |
| 10301 | /// diagnostics and returns ExprError() |
| 10302 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10303 | UnresolvedLookupExpr *ULE, |
| 10304 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10305 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10306 | SourceLocation RParenLoc, |
| 10307 | Expr *ExecConfig, |
| 10308 | OverloadCandidateSet *CandidateSet, |
| 10309 | OverloadCandidateSet::iterator *Best, |
| 10310 | OverloadingResult OverloadResult, |
| 10311 | bool AllowTypoCorrection) { |
| 10312 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10313 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10314 | RParenLoc, /*EmptyLookup=*/true, |
| 10315 | AllowTypoCorrection); |
| 10316 | |
| 10317 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10318 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10319 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10320 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10321 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10322 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10323 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10324 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10325 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10326 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10327 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10328 | case OR_No_Viable_Function: { |
| 10329 | // Try to recover by looking for viable functions which the user might |
| 10330 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10331 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10332 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10333 | /*EmptyLookup=*/false, |
| 10334 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10335 | if (!Recovery.isInvalid()) |
| 10336 | return Recovery; |
| 10337 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10338 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10339 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10340 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10341 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10342 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10343 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10344 | |
| 10345 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10346 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10347 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10348 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10349 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10350 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10351 | case OR_Deleted: { |
| 10352 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10353 | << (*Best)->Function->isDeleted() |
| 10354 | << ULE->getName() |
| 10355 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10356 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10357 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10358 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10359 | // We emitted an error for the unvailable/deleted function call but keep |
| 10360 | // the call in the AST. |
| 10361 | FunctionDecl *FDecl = (*Best)->Function; |
| 10362 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10363 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10364 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10365 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10366 | } |
| 10367 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10368 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10369 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10370 | } |
| 10371 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10372 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10373 | /// (which eventually refers to the declaration Func) and the call |
| 10374 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10375 | /// to a specific function. If overload resolution succeeds, returns |
| 10376 | /// the call expression produced by overload resolution. |
| 10377 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10378 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10379 | UnresolvedLookupExpr *ULE, |
| 10380 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10381 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10382 | SourceLocation RParenLoc, |
| 10383 | Expr *ExecConfig, |
| 10384 | bool AllowTypoCorrection) { |
| 10385 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10386 | ExprResult result; |
| 10387 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10388 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10389 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10390 | return result; |
| 10391 | |
| 10392 | OverloadCandidateSet::iterator Best; |
| 10393 | OverloadingResult OverloadResult = |
| 10394 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10395 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10396 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10397 | RParenLoc, ExecConfig, &CandidateSet, |
| 10398 | &Best, OverloadResult, |
| 10399 | AllowTypoCorrection); |
| 10400 | } |
| 10401 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10402 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10403 | return Functions.size() > 1 || |
| 10404 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10405 | } |
| 10406 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10407 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10408 | /// operator. |
| 10409 | /// |
| 10410 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10411 | /// |
| 10412 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10413 | /// operator. |
| 10414 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10415 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10416 | /// considered by overload resolution. The caller needs to build this |
| 10417 | /// set based on the context using, e.g., |
| 10418 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10419 | /// set should not contain any member functions; those will be added |
| 10420 | /// by CreateOverloadedUnaryOp(). |
| 10421 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10422 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10423 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10424 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10425 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10426 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10427 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10428 | |
| 10429 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10430 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10431 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10432 | // TODO: provide better source location info. |
| 10433 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10434 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10435 | if (checkPlaceholderForOverload(*this, Input)) |
| 10436 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10437 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10438 | Expr *Args[2] = { Input, 0 }; |
| 10439 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10440 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10441 | // For post-increment and post-decrement, add the implicit '0' as |
| 10442 | // the second argument, so that we know this is a post-increment or |
| 10443 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10444 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10445 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10446 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10447 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10448 | NumArgs = 2; |
| 10449 | } |
| 10450 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10451 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10452 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10453 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10454 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10455 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10456 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10457 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10458 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10459 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10460 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10461 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10462 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10463 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10464 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10465 | /*ADL*/ true, IsOverloaded(Fns), |
| 10466 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10467 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10468 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10469 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10470 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
| 10473 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10474 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10475 | |
| 10476 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10477 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10478 | |
| 10479 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10480 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10481 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10482 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10483 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10484 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10485 | CandidateSet); |
| 10486 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10487 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10488 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10489 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10490 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10491 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10492 | // Perform overload resolution. |
| 10493 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10494 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10495 | case OR_Success: { |
| 10496 | // We found a built-in operator or an overloaded operator. |
| 10497 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10498 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10499 | if (FnDecl) { |
| 10500 | // We matched an overloaded operator. Build a call to that |
| 10501 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10502 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10503 | // Convert the arguments. |
| 10504 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10505 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10506 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10507 | ExprResult InputRes = |
| 10508 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10509 | Best->FoundDecl, Method); |
| 10510 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10511 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10512 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10513 | } else { |
| 10514 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10515 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10516 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10517 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10518 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10519 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10520 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10521 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10522 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10523 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10524 | } |
| 10525 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10526 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10527 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10528 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10529 | if (FnExpr.isInvalid()) |
| 10530 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10531 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame^] | 10532 | // Determine the result type. |
| 10533 | QualType ResultTy = FnDecl->getResultType(); |
| 10534 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10535 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10536 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10537 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10538 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10539 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10540 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10541 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10542 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10543 | FnDecl)) |
| 10544 | return ExprError(); |
| 10545 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10546 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10547 | } else { |
| 10548 | // We matched a built-in operator. Convert the arguments, then |
| 10549 | // break out so that we will build the appropriate built-in |
| 10550 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10551 | ExprResult InputRes = |
| 10552 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10553 | Best->Conversions[0], AA_Passing); |
| 10554 | if (InputRes.isInvalid()) |
| 10555 | return ExprError(); |
| 10556 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10557 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10558 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10559 | } |
| 10560 | |
| 10561 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10562 | // This is an erroneous use of an operator which can be overloaded by |
| 10563 | // a non-member function. Check for non-member operators which were |
| 10564 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10565 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10566 | // FIXME: Recover by calling the found function. |
| 10567 | return ExprError(); |
| 10568 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10569 | // No viable function; fall through to handling this as a |
| 10570 | // built-in operator, which will produce an error message for us. |
| 10571 | break; |
| 10572 | |
| 10573 | case OR_Ambiguous: |
| 10574 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10575 | << UnaryOperator::getOpcodeStr(Opc) |
| 10576 | << Input->getType() |
| 10577 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10578 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10579 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10580 | return ExprError(); |
| 10581 | |
| 10582 | case OR_Deleted: |
| 10583 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10584 | << Best->Function->isDeleted() |
| 10585 | << UnaryOperator::getOpcodeStr(Opc) |
| 10586 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10587 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10588 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10589 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10590 | return ExprError(); |
| 10591 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10592 | |
| 10593 | // Either we found no viable overloaded operator or we matched a |
| 10594 | // built-in operator. In either case, fall through to trying to |
| 10595 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10596 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10597 | } |
| 10598 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10599 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10600 | /// operator. |
| 10601 | /// |
| 10602 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10603 | /// |
| 10604 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10605 | /// operator. |
| 10606 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10607 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10608 | /// considered by overload resolution. The caller needs to build this |
| 10609 | /// set based on the context using, e.g., |
| 10610 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10611 | /// set should not contain any member functions; those will be added |
| 10612 | /// by CreateOverloadedBinOp(). |
| 10613 | /// |
| 10614 | /// \param LHS Left-hand argument. |
| 10615 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10616 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10617 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10618 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10619 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10620 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10621 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10622 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10623 | |
| 10624 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10625 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10626 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10627 | |
| 10628 | // If either side is type-dependent, create an appropriate dependent |
| 10629 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10630 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10631 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10632 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10633 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10634 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10635 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10636 | Context.DependentTy, |
| 10637 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10638 | OpLoc, |
| 10639 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10640 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10641 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10642 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10643 | VK_LValue, |
| 10644 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10645 | Context.DependentTy, |
| 10646 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10647 | OpLoc, |
| 10648 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10649 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10650 | |
| 10651 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10652 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10653 | // TODO: provide better source location info in DNLoc component. |
| 10654 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10655 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10656 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10657 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10658 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10659 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10660 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10661 | Context.DependentTy, VK_RValue, |
| 10662 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10663 | } |
| 10664 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10665 | // Always do placeholder-like conversions on the RHS. |
| 10666 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10667 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10668 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10669 | // Do placeholder-like conversion on the LHS; note that we should |
| 10670 | // not get here with a PseudoObject LHS. |
| 10671 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10672 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10673 | return ExprError(); |
| 10674 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10675 | // If this is the assignment operator, we only perform overload resolution |
| 10676 | // if the left-hand side is a class or enumeration type. This is actually |
| 10677 | // a hack. The standard requires that we do overload resolution between the |
| 10678 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10679 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10680 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10681 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10682 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10683 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10684 | // If this is the .* operator, which is not overloadable, just |
| 10685 | // create a built-in binary operator. |
| 10686 | if (Opc == BO_PtrMemD) |
| 10687 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10688 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10689 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10690 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10691 | |
| 10692 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10693 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10694 | |
| 10695 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10696 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10697 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10698 | // Add candidates from ADL. |
| 10699 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10700 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10701 | /*ExplicitTemplateArgs*/ 0, |
| 10702 | CandidateSet); |
| 10703 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10704 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10705 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10706 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10707 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10708 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10709 | // Perform overload resolution. |
| 10710 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10711 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10712 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10713 | // We found a built-in operator or an overloaded operator. |
| 10714 | FunctionDecl *FnDecl = Best->Function; |
| 10715 | |
| 10716 | if (FnDecl) { |
| 10717 | // We matched an overloaded operator. Build a call to that |
| 10718 | // operator. |
| 10719 | |
| 10720 | // Convert the arguments. |
| 10721 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10722 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10723 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10724 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10725 | ExprResult Arg1 = |
| 10726 | PerformCopyInitialization( |
| 10727 | InitializedEntity::InitializeParameter(Context, |
| 10728 | FnDecl->getParamDecl(0)), |
| 10729 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10730 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10731 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10732 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10733 | ExprResult Arg0 = |
| 10734 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10735 | Best->FoundDecl, Method); |
| 10736 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10737 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10738 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10739 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10740 | } else { |
| 10741 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10742 | ExprResult Arg0 = PerformCopyInitialization( |
| 10743 | InitializedEntity::InitializeParameter(Context, |
| 10744 | FnDecl->getParamDecl(0)), |
| 10745 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10746 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10747 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10748 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10749 | ExprResult Arg1 = |
| 10750 | PerformCopyInitialization( |
| 10751 | InitializedEntity::InitializeParameter(Context, |
| 10752 | FnDecl->getParamDecl(1)), |
| 10753 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10754 | if (Arg1.isInvalid()) |
| 10755 | return ExprError(); |
| 10756 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10757 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10758 | } |
| 10759 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10760 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10761 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10762 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10763 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10764 | if (FnExpr.isInvalid()) |
| 10765 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10766 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame^] | 10767 | // Determine the result type. |
| 10768 | QualType ResultTy = FnDecl->getResultType(); |
| 10769 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10770 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10771 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10772 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10773 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10774 | Args, ResultTy, VK, OpLoc, |
| 10775 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10776 | |
| 10777 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10778 | FnDecl)) |
| 10779 | return ExprError(); |
| 10780 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10781 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10782 | // Cut off the implicit 'this'. |
| 10783 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10784 | ArgsArray = ArgsArray.slice(1); |
| 10785 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10786 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10787 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10788 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10789 | } else { |
| 10790 | // We matched a built-in operator. Convert the arguments, then |
| 10791 | // break out so that we will build the appropriate built-in |
| 10792 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10793 | ExprResult ArgsRes0 = |
| 10794 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10795 | Best->Conversions[0], AA_Passing); |
| 10796 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10797 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10798 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10799 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10800 | ExprResult ArgsRes1 = |
| 10801 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10802 | Best->Conversions[1], AA_Passing); |
| 10803 | if (ArgsRes1.isInvalid()) |
| 10804 | return ExprError(); |
| 10805 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10806 | break; |
| 10807 | } |
| 10808 | } |
| 10809 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10810 | case OR_No_Viable_Function: { |
| 10811 | // C++ [over.match.oper]p9: |
| 10812 | // If the operator is the operator , [...] and there are no |
| 10813 | // viable functions, then the operator is assumed to be the |
| 10814 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10815 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10816 | break; |
| 10817 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10818 | // For class as left operand for assignment or compound assigment |
| 10819 | // operator do not fall through to handling in built-in, but report that |
| 10820 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10821 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10822 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10823 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10824 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10825 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10826 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 3f93d4c | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 10827 | if (Args[0]->getType()->isIncompleteType()) { |
| 10828 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 10829 | << Args[0]->getType() |
| 10830 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10831 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10832 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10833 | // This is an erroneous use of an operator which can be overloaded by |
| 10834 | // a non-member function. Check for non-member operators which were |
| 10835 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10836 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10837 | // FIXME: Recover by calling the found function. |
| 10838 | return ExprError(); |
| 10839 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10840 | // No viable function; try to create a built-in operation, which will |
| 10841 | // produce an error. Then, show the non-viable candidates. |
| 10842 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10843 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10844 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10845 | "C++ binary operator overloading is missing candidates!"); |
| 10846 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10847 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10848 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10849 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10850 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10851 | |
| 10852 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10853 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10854 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10855 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10856 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10857 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10858 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10859 | return ExprError(); |
| 10860 | |
| 10861 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10862 | if (isImplicitlyDeleted(Best->Function)) { |
| 10863 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10864 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10865 | << Context.getRecordType(Method->getParent()) |
| 10866 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10867 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10868 | // The user probably meant to call this special member. Just |
| 10869 | // explain why it's deleted. |
| 10870 | NoteDeletedFunction(Method); |
| 10871 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10872 | } else { |
| 10873 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10874 | << Best->Function->isDeleted() |
| 10875 | << BinaryOperator::getOpcodeStr(Opc) |
| 10876 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10877 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10878 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10879 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10880 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10881 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10882 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10883 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10884 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10885 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10886 | } |
| 10887 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10888 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10889 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10890 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10891 | Expr *Base, Expr *Idx) { |
| 10892 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10893 | DeclarationName OpName = |
| 10894 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10895 | |
| 10896 | // If either side is type-dependent, create an appropriate dependent |
| 10897 | // expression. |
| 10898 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10899 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10900 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10901 | // CHECKME: no 'operator' keyword? |
| 10902 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10903 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10904 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10905 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10906 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10907 | /*ADL*/ true, /*Overloaded*/ false, |
| 10908 | UnresolvedSetIterator(), |
| 10909 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10910 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10911 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10912 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10913 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10914 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10915 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10916 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10917 | } |
| 10918 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10919 | // Handle placeholders on both operands. |
| 10920 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10921 | return ExprError(); |
| 10922 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10923 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10924 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10925 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10926 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10927 | |
| 10928 | // Subscript can only be overloaded as a member function. |
| 10929 | |
| 10930 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10931 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10932 | |
| 10933 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10934 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10935 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10936 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10937 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10938 | // Perform overload resolution. |
| 10939 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10940 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10941 | case OR_Success: { |
| 10942 | // We found a built-in operator or an overloaded operator. |
| 10943 | FunctionDecl *FnDecl = Best->Function; |
| 10944 | |
| 10945 | if (FnDecl) { |
| 10946 | // We matched an overloaded operator. Build a call to that |
| 10947 | // operator. |
| 10948 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10949 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10950 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10951 | // Convert the arguments. |
| 10952 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10953 | ExprResult Arg0 = |
| 10954 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10955 | Best->FoundDecl, Method); |
| 10956 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10957 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10958 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10959 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10960 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10961 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10962 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10963 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10964 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10965 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10966 | Owned(Args[1])); |
| 10967 | if (InputInit.isInvalid()) |
| 10968 | return ExprError(); |
| 10969 | |
| 10970 | Args[1] = InputInit.takeAs<Expr>(); |
| 10971 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10972 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10973 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10974 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10975 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10976 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10977 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10978 | OpLocInfo.getLoc(), |
| 10979 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10980 | if (FnExpr.isInvalid()) |
| 10981 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10982 | |
Richard Smith | f93ec76 | 2013-11-15 02:58:23 +0000 | [diff] [blame^] | 10983 | // Determine the result type |
| 10984 | QualType ResultTy = FnDecl->getResultType(); |
| 10985 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10986 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10987 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10988 | CXXOperatorCallExpr *TheCall = |
| 10989 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10990 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10991 | ResultTy, VK, RLoc, |
| 10992 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10993 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10994 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10995 | FnDecl)) |
| 10996 | return ExprError(); |
| 10997 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10998 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10999 | } else { |
| 11000 | // We matched a built-in operator. Convert the arguments, then |
| 11001 | // break out so that we will build the appropriate built-in |
| 11002 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11003 | ExprResult ArgsRes0 = |
| 11004 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 11005 | Best->Conversions[0], AA_Passing); |
| 11006 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11007 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11008 | Args[0] = ArgsRes0.take(); |
| 11009 | |
| 11010 | ExprResult ArgsRes1 = |
| 11011 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 11012 | Best->Conversions[1], AA_Passing); |
| 11013 | if (ArgsRes1.isInvalid()) |
| 11014 | return ExprError(); |
| 11015 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11016 | |
| 11017 | break; |
| 11018 | } |
| 11019 | } |
| 11020 | |
| 11021 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11022 | if (CandidateSet.empty()) |
| 11023 | Diag(LLoc, diag::err_ovl_no_oper) |
| 11024 | << Args[0]->getType() << /*subscript*/ 0 |
| 11025 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 11026 | else |
| 11027 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 11028 | << Args[0]->getType() |
| 11029 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11030 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11031 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11032 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11033 | } |
| 11034 | |
| 11035 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11036 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11037 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11038 | << Args[0]->getType() << Args[1]->getType() |
| 11039 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11040 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11041 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11042 | return ExprError(); |
| 11043 | |
| 11044 | case OR_Deleted: |
| 11045 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 11046 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11047 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11048 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11049 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11050 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11051 | return ExprError(); |
| 11052 | } |
| 11053 | |
| 11054 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11055 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 11056 | } |
| 11057 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11058 | /// BuildCallToMemberFunction - Build a call to a member |
| 11059 | /// function. MemExpr is the expression that refers to the member |
| 11060 | /// function (and includes the object parameter), Args/NumArgs are the |
| 11061 | /// arguments to the function call (not including the object |
| 11062 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11063 | /// expression refers to a non-static member function or an overloaded |
| 11064 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11065 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11066 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11067 | SourceLocation LParenLoc, |
| 11068 | MultiExprArg Args, |
| 11069 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11070 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 11071 | MemExprE->getType() == Context.OverloadTy); |
| 11072 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11073 | // Dig out the member expression. This holds both the object |
| 11074 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11075 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11076 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11077 | // Determine whether this is a call to a pointer-to-member function. |
| 11078 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 11079 | assert(op->getType() == Context.BoundMemberTy); |
| 11080 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 11081 | |
| 11082 | QualType fnType = |
| 11083 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 11084 | |
| 11085 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 11086 | QualType resultType = proto->getCallResultType(Context); |
| 11087 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 11088 | |
| 11089 | // Check that the object type isn't more qualified than the |
| 11090 | // member function we're calling. |
| 11091 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 11092 | |
| 11093 | QualType objectType = op->getLHS()->getType(); |
| 11094 | if (op->getOpcode() == BO_PtrMemI) |
| 11095 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 11096 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 11097 | |
| 11098 | Qualifiers difference = objectQuals - funcQuals; |
| 11099 | difference.removeObjCGCAttr(); |
| 11100 | difference.removeAddressSpace(); |
| 11101 | if (difference) { |
| 11102 | std::string qualsString = difference.getAsString(); |
| 11103 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 11104 | << fnType.getUnqualifiedType() |
| 11105 | << qualsString |
| 11106 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 11107 | } |
| 11108 | |
| 11109 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11110 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11111 | resultType, valueKind, RParenLoc); |
| 11112 | |
| 11113 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11114 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11115 | call, 0)) |
| 11116 | return ExprError(); |
| 11117 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11118 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11119 | return ExprError(); |
| 11120 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11121 | if (CheckOtherCall(call, proto)) |
| 11122 | return ExprError(); |
| 11123 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11124 | return MaybeBindToTemporary(call); |
| 11125 | } |
| 11126 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11127 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11128 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11129 | return ExprError(); |
| 11130 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11131 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11132 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11133 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11134 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11135 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11136 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11137 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11138 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11139 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11140 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11141 | } else { |
| 11142 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11143 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11144 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11145 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11146 | Expr::Classification ObjectClassification |
| 11147 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11148 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11149 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11150 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11151 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11152 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11153 | // FIXME: avoid copy. |
| 11154 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11155 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11156 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11157 | TemplateArgs = &TemplateArgsBuffer; |
| 11158 | } |
| 11159 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11160 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11161 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11162 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11163 | NamedDecl *Func = *I; |
| 11164 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11165 | if (isa<UsingShadowDecl>(Func)) |
| 11166 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11167 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11168 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11169 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11170 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11171 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11172 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11173 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11174 | // If explicit template arguments were provided, we can't call a |
| 11175 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11176 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11177 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11178 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11179 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11180 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11181 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11182 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11183 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11184 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11185 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11186 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11187 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11188 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11190 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11191 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11192 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11193 | UnbridgedCasts.restore(); |
| 11194 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11195 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11196 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11197 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11198 | case OR_Success: |
| 11199 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11200 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11201 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11202 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11203 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11204 | // If FoundDecl is different from Method (such as if one is a template |
| 11205 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11206 | // called on both. |
| 11207 | // FIXME: This would be more comprehensively addressed by modifying |
| 11208 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11209 | // being used. |
| 11210 | if (Method != FoundDecl.getDecl() && |
| 11211 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11212 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11213 | break; |
| 11214 | |
| 11215 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11216 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11217 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11218 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11219 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11220 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11221 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11222 | |
| 11223 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11224 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11225 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11226 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11227 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11228 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11229 | |
| 11230 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11231 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11232 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11233 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11234 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11235 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11236 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11237 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11238 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11239 | } |
| 11240 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11241 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11242 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11243 | // If overload resolution picked a static member, build a |
| 11244 | // non-member call based on that function. |
| 11245 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11246 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11247 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11248 | } |
| 11249 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11250 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11251 | } |
| 11252 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11253 | QualType ResultType = Method->getResultType(); |
| 11254 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11255 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11256 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11257 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11258 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11259 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11260 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11261 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11262 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11263 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11264 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11265 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11266 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11267 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11268 | // We only need to do this if there was actually an overload; otherwise |
| 11269 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11270 | if (!Method->isStatic()) { |
| 11271 | ExprResult ObjectArg = |
| 11272 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11273 | FoundDecl, Method); |
| 11274 | if (ObjectArg.isInvalid()) |
| 11275 | return ExprError(); |
| 11276 | MemExpr->setBase(ObjectArg.take()); |
| 11277 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11278 | |
| 11279 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11280 | const FunctionProtoType *Proto = |
| 11281 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11282 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11283 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11284 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11285 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11286 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11287 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11288 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11289 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11290 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11291 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11292 | isa<CXXDestructorDecl>(CurContext)) && |
| 11293 | TheCall->getMethodDecl()->isPure()) { |
| 11294 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11295 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11296 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11297 | Diag(MemExpr->getLocStart(), |
| 11298 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11299 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11300 | << MD->getParent()->getDeclName(); |
| 11301 | |
| 11302 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11303 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11304 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11305 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11306 | } |
| 11307 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11308 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11309 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11310 | /// overloaded function call operator (@c operator()) or performing a |
| 11311 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11312 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11313 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11314 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11315 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11316 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11317 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11318 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11319 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11320 | |
| 11321 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11322 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11323 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11324 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11325 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11326 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11327 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11328 | // C++ [over.call.object]p1: |
| 11329 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11330 | // 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] | 11331 | // candidate functions includes at least the function call |
| 11332 | // operators of T. The function call operators of T are obtained by |
| 11333 | // ordinary lookup of the name operator() in the context of |
| 11334 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11335 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11336 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11337 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11338 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11339 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11340 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11341 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11342 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11343 | LookupQualifiedName(R, Record->getDecl()); |
| 11344 | R.suppressDiagnostics(); |
| 11345 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11346 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11347 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11348 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11349 | Object.get()->Classify(Context), |
| 11350 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11351 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11352 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11353 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11354 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11355 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11356 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11357 | // |
| 11358 | // operator conversion-type-id () cv-qualifier; |
| 11359 | // |
| 11360 | // where cv-qualifier is the same cv-qualification as, or a |
| 11361 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11362 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11363 | // R", or the type "reference to pointer to function of |
| 11364 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11365 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11366 | // is also considered as a candidate function. Similarly, |
| 11367 | // surrogate call functions are added to the set of candidate |
| 11368 | // functions for each conversion function declared in an |
| 11369 | // accessible base class provided the function is not hidden |
| 11370 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11371 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11372 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11373 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11374 | for (CXXRecordDecl::conversion_iterator |
| 11375 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11376 | NamedDecl *D = *I; |
| 11377 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11378 | if (isa<UsingShadowDecl>(D)) |
| 11379 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11380 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11381 | // Skip over templated conversion functions; they aren't |
| 11382 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11383 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11384 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11385 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11386 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11387 | if (!Conv->isExplicit()) { |
| 11388 | // Strip the reference type (if any) and then the pointer type (if |
| 11389 | // any) to get down to what might be a function type. |
| 11390 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11391 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11392 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11393 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11394 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11395 | { |
| 11396 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11397 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11398 | } |
| 11399 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11400 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11401 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11402 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11403 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11404 | // Perform overload resolution. |
| 11405 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11406 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11407 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11408 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11409 | // Overload resolution succeeded; we'll build the appropriate call |
| 11410 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11411 | break; |
| 11412 | |
| 11413 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11414 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11415 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11416 | << Object.get()->getType() << /*call*/ 1 |
| 11417 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11418 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11419 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11420 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11421 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11422 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11423 | break; |
| 11424 | |
| 11425 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11426 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11427 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11428 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11429 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11430 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11431 | |
| 11432 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11433 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11434 | diag::err_ovl_deleted_object_call) |
| 11435 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11436 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11437 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11438 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11439 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11440 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11441 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11442 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11443 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11444 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11445 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11446 | UnbridgedCasts.restore(); |
| 11447 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11448 | if (Best->Function == 0) { |
| 11449 | // Since there is no function declaration, this is one of the |
| 11450 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11451 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11452 | = cast<CXXConversionDecl>( |
| 11453 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11454 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11455 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11456 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11457 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11458 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11459 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11460 | // We selected one of the surrogate functions that converts the |
| 11461 | // object parameter to a function pointer. Perform the conversion |
| 11462 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11463 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11464 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11465 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11466 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11467 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11468 | if (Call.isInvalid()) |
| 11469 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11470 | // Record usage of conversion in an implicit cast. |
| 11471 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11472 | CK_UserDefinedConversion, |
| 11473 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11474 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11475 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11476 | } |
| 11477 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11478 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11479 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11480 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11481 | // that calls this method, using Object for the implicit object |
| 11482 | // parameter and passing along the remaining arguments. |
| 11483 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11484 | |
| 11485 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11486 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11487 | return ExprError(); |
| 11488 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11489 | const FunctionProtoType *Proto = |
| 11490 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11491 | |
| 11492 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11493 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11494 | DeclarationNameInfo OpLocInfo( |
| 11495 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11496 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11497 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11498 | HadMultipleCandidates, |
| 11499 | OpLocInfo.getLoc(), |
| 11500 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11501 | if (NewFn.isInvalid()) |
| 11502 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11503 | |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11504 | // Build the full argument list for the method call (the implicit object |
| 11505 | // parameter is placed at the beginning of the list). |
| 11506 | llvm::OwningArrayPtr<Expr *> MethodArgs(new Expr*[Args.size() + 1]); |
| 11507 | MethodArgs[0] = Object.get(); |
| 11508 | std::copy(Args.begin(), Args.end(), &MethodArgs[1]); |
| 11509 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11510 | // Once we've built TheCall, all of the expressions are properly |
| 11511 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11512 | QualType ResultTy = Method->getResultType(); |
| 11513 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11514 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11515 | |
Benjamin Kramer | 7034fae | 2013-09-25 13:10:11 +0000 | [diff] [blame] | 11516 | CXXOperatorCallExpr *TheCall = new (Context) |
| 11517 | CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
| 11518 | llvm::makeArrayRef(MethodArgs.get(), Args.size() + 1), |
| 11519 | ResultTy, VK, RParenLoc, false); |
| 11520 | MethodArgs.reset(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11521 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11522 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11523 | Method)) |
| 11524 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11525 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11526 | // We may have default arguments. If so, we need to allocate more |
| 11527 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11528 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11529 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11530 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11531 | bool IsError = false; |
| 11532 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11533 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11534 | ExprResult ObjRes = |
| 11535 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11536 | Best->FoundDecl, Method); |
| 11537 | if (ObjRes.isInvalid()) |
| 11538 | IsError = true; |
| 11539 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11540 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11541 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11542 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11543 | // Check the argument types. |
Benjamin Kramer | 83372f8 | 2013-10-05 10:03:01 +0000 | [diff] [blame] | 11544 | for (unsigned i = 0; i != NumArgsInProto; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11545 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11546 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11547 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11548 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11549 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11550 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11551 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11552 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11553 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11554 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11555 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11556 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11557 | IsError |= InputInit.isInvalid(); |
| 11558 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11559 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11560 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11561 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11562 | if (DefArg.isInvalid()) { |
| 11563 | IsError = true; |
| 11564 | break; |
| 11565 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11566 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11567 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11568 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11569 | |
| 11570 | TheCall->setArg(i + 1, Arg); |
| 11571 | } |
| 11572 | |
| 11573 | // If this is a variadic call, handle args passed through "...". |
| 11574 | if (Proto->isVariadic()) { |
| 11575 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11576 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11577 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11578 | IsError |= Arg.isInvalid(); |
| 11579 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11580 | } |
| 11581 | } |
| 11582 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11583 | if (IsError) return true; |
| 11584 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11585 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11586 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11587 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11588 | return true; |
| 11589 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11590 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11591 | } |
| 11592 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11593 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11594 | /// (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] | 11595 | /// @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] | 11596 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11597 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11598 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11599 | assert(Base->getType()->isRecordType() && |
| 11600 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11601 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11602 | if (checkPlaceholderForOverload(*this, Base)) |
| 11603 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11604 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11605 | SourceLocation Loc = Base->getExprLoc(); |
| 11606 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11607 | // C++ [over.ref]p1: |
| 11608 | // |
| 11609 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11610 | // for a class object x of type T if T::operator->() exists and if |
| 11611 | // the operator is selected as the best match function by the |
| 11612 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11613 | DeclarationName OpName = |
| 11614 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11615 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11616 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11617 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11618 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11619 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11620 | return ExprError(); |
| 11621 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11622 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11623 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11624 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11625 | |
| 11626 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11627 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11628 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11629 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11630 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11631 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11632 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11633 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11634 | // Perform overload resolution. |
| 11635 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11636 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11637 | case OR_Success: |
| 11638 | // Overload resolution succeeded; we'll build the call below. |
| 11639 | break; |
| 11640 | |
| 11641 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11642 | if (CandidateSet.empty()) { |
| 11643 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11644 | if (NoArrowOperatorFound) { |
| 11645 | // Report this specific error to the caller instead of emitting a |
| 11646 | // diagnostic, as requested. |
| 11647 | *NoArrowOperatorFound = true; |
| 11648 | return ExprError(); |
| 11649 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11650 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11651 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11652 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11653 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11654 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11655 | } |
| 11656 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11657 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11658 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11659 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11660 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11661 | |
| 11662 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11663 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11664 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11665 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11666 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11667 | |
| 11668 | case OR_Deleted: |
| 11669 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11670 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11671 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11672 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11673 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11674 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11675 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11676 | } |
| 11677 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11678 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11679 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11680 | // Convert the object parameter. |
| 11681 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11682 | ExprResult BaseResult = |
| 11683 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11684 | Best->FoundDecl, Method); |
| 11685 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11686 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11687 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11688 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11689 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11690 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11691 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11692 | if (FnExpr.isInvalid()) |
| 11693 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11694 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11695 | QualType ResultTy = Method->getResultType(); |
| 11696 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11697 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11698 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11699 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11700 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11701 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11702 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11703 | Method)) |
| 11704 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11705 | |
| 11706 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11707 | } |
| 11708 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11709 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11710 | /// a literal operator described by the provided lookup results. |
| 11711 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11712 | DeclarationNameInfo &SuffixInfo, |
| 11713 | ArrayRef<Expr*> Args, |
| 11714 | SourceLocation LitEndLoc, |
| 11715 | TemplateArgumentListInfo *TemplateArgs) { |
| 11716 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11717 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11718 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11719 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11720 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11721 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11722 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11723 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11724 | // Perform overload resolution. This will usually be trivial, but might need |
| 11725 | // to perform substitutions for a literal operator template. |
| 11726 | OverloadCandidateSet::iterator Best; |
| 11727 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11728 | case OR_Success: |
| 11729 | case OR_Deleted: |
| 11730 | break; |
| 11731 | |
| 11732 | case OR_No_Viable_Function: |
| 11733 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11734 | << R.getLookupName(); |
| 11735 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11736 | return ExprError(); |
| 11737 | |
| 11738 | case OR_Ambiguous: |
| 11739 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11740 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11741 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11742 | } |
| 11743 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11744 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11745 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11746 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11747 | SuffixInfo.getLoc(), |
| 11748 | SuffixInfo.getInfo()); |
| 11749 | if (Fn.isInvalid()) |
| 11750 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11751 | |
| 11752 | // Check the argument types. This should almost always be a no-op, except |
| 11753 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11754 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11755 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11756 | ExprResult InputInit = PerformCopyInitialization( |
| 11757 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11758 | SourceLocation(), Args[ArgIdx]); |
| 11759 | if (InputInit.isInvalid()) |
| 11760 | return true; |
| 11761 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11762 | } |
| 11763 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11764 | QualType ResultTy = FD->getResultType(); |
| 11765 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11766 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11767 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11768 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11769 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11770 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11771 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11772 | |
| 11773 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11774 | return ExprError(); |
| 11775 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11776 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11777 | return ExprError(); |
| 11778 | |
| 11779 | return MaybeBindToTemporary(UDL); |
| 11780 | } |
| 11781 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11782 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11783 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11784 | /// will be invoked. Otherwise, the function will be found via argument |
| 11785 | /// dependent lookup. |
| 11786 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11787 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11788 | /// is returned. |
| 11789 | Sema::ForRangeStatus |
| 11790 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11791 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11792 | BeginEndFunction BEF, |
| 11793 | const DeclarationNameInfo &NameInfo, |
| 11794 | LookupResult &MemberLookup, |
| 11795 | OverloadCandidateSet *CandidateSet, |
| 11796 | Expr *Range, ExprResult *CallExpr) { |
| 11797 | CandidateSet->clear(); |
| 11798 | if (!MemberLookup.empty()) { |
| 11799 | ExprResult MemberRef = |
| 11800 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11801 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11802 | /*TemplateKWLoc=*/SourceLocation(), |
| 11803 | /*FirstQualifierInScope=*/0, |
| 11804 | MemberLookup, |
| 11805 | /*TemplateArgs=*/0); |
| 11806 | if (MemberRef.isInvalid()) { |
| 11807 | *CallExpr = ExprError(); |
| 11808 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11809 | << RangeLoc << BEF << Range->getType(); |
| 11810 | return FRS_DiagnosticIssued; |
| 11811 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11812 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11813 | if (CallExpr->isInvalid()) { |
| 11814 | *CallExpr = ExprError(); |
| 11815 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11816 | << RangeLoc << BEF << Range->getType(); |
| 11817 | return FRS_DiagnosticIssued; |
| 11818 | } |
| 11819 | } else { |
| 11820 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11821 | UnresolvedLookupExpr *Fn = |
| 11822 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11823 | NestedNameSpecifierLoc(), NameInfo, |
| 11824 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11825 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11826 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11827 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11828 | CandidateSet, CallExpr); |
| 11829 | if (CandidateSet->empty() || CandidateSetError) { |
| 11830 | *CallExpr = ExprError(); |
| 11831 | return FRS_NoViableFunction; |
| 11832 | } |
| 11833 | OverloadCandidateSet::iterator Best; |
| 11834 | OverloadingResult OverloadResult = |
| 11835 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11836 | |
| 11837 | if (OverloadResult == OR_No_Viable_Function) { |
| 11838 | *CallExpr = ExprError(); |
| 11839 | return FRS_NoViableFunction; |
| 11840 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11841 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11842 | Loc, 0, CandidateSet, &Best, |
| 11843 | OverloadResult, |
| 11844 | /*AllowTypoCorrection=*/false); |
| 11845 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11846 | *CallExpr = ExprError(); |
| 11847 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11848 | << RangeLoc << BEF << Range->getType(); |
| 11849 | return FRS_DiagnosticIssued; |
| 11850 | } |
| 11851 | } |
| 11852 | return FRS_Success; |
| 11853 | } |
| 11854 | |
| 11855 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11856 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11857 | /// a C++ overloaded function (possibly with some parentheses and |
| 11858 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11859 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11860 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11861 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11862 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11863 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11864 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11865 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11866 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11867 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11868 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11869 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11870 | } |
| 11871 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11872 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11873 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11874 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11875 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11876 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11877 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11878 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11879 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11880 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11881 | |
| 11882 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11883 | ICE->getCastKind(), |
| 11884 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11885 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11886 | } |
| 11887 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11888 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11889 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11890 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11891 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11892 | if (Method->isStatic()) { |
| 11893 | // Do nothing: static member functions aren't any different |
| 11894 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11895 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11896 | // Fix the sub expression, which really has to be an |
| 11897 | // UnresolvedLookupExpr holding an overloaded member function |
| 11898 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11899 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11900 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11901 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11902 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11903 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11904 | assert(isa<DeclRefExpr>(SubExpr) |
| 11905 | && "fixed to something other than a decl ref"); |
| 11906 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11907 | && "fixed to a member ref with no nested name qualifier"); |
| 11908 | |
| 11909 | // We have taken the address of a pointer to member |
| 11910 | // function. Perform the computation here so that we get the |
| 11911 | // appropriate pointer to member type. |
| 11912 | QualType ClassType |
| 11913 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11914 | QualType MemPtrType |
| 11915 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11916 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11917 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11918 | VK_RValue, OK_Ordinary, |
| 11919 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11920 | } |
| 11921 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11922 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11923 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11924 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11925 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11926 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11927 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11928 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11929 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11930 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11931 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11932 | |
| 11933 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11934 | // FIXME: avoid copy. |
| 11935 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11936 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11937 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11938 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11939 | } |
| 11940 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11941 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11942 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11943 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11944 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11945 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11946 | ULE->getNameLoc(), |
| 11947 | Fn->getType(), |
| 11948 | VK_LValue, |
| 11949 | Found.getDecl(), |
| 11950 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11951 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11952 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11953 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11954 | } |
| 11955 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11956 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11957 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11958 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11959 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11960 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11961 | TemplateArgs = &TemplateArgsBuffer; |
| 11962 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11963 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11964 | Expr *Base; |
| 11965 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11966 | // If we're filling in a static method where we used to have an |
| 11967 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11968 | if (MemExpr->isImplicitAccess()) { |
| 11969 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11970 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11971 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11972 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11973 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11974 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11975 | MemExpr->getMemberLoc(), |
| 11976 | Fn->getType(), |
| 11977 | VK_LValue, |
| 11978 | Found.getDecl(), |
| 11979 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11980 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11981 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11982 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11983 | } else { |
| 11984 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11985 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11986 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11987 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11988 | Base = new (Context) CXXThisExpr(Loc, |
| 11989 | MemExpr->getBaseType(), |
| 11990 | /*isImplicit=*/true); |
| 11991 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11992 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11993 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11994 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11995 | ExprValueKind valueKind; |
| 11996 | QualType type; |
| 11997 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11998 | valueKind = VK_LValue; |
| 11999 | type = Fn->getType(); |
| 12000 | } else { |
| 12001 | valueKind = VK_RValue; |
| 12002 | type = Context.BoundMemberTy; |
| 12003 | } |
| 12004 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12005 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 12006 | MemExpr->isArrow(), |
| 12007 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 12008 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12009 | Fn, |
| 12010 | Found, |
| 12011 | MemExpr->getMemberNameInfo(), |
| 12012 | TemplateArgs, |
| 12013 | type, valueKind, OK_Ordinary); |
| 12014 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 12015 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 12016 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 12017 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12018 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 12019 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 12020 | } |
| 12021 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 12022 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 12023 | DeclAccessPair Found, |
| 12024 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 12025 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 12026 | } |
| 12027 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 12028 | } // end namespace clang |